Donnerstag, 26. Januar 2012

Powershell script how to add a field to a list

There might be the need for you to add a field to a list. Here is a script how to manage. Replace the values in %% with your values:

$isRequired = $%enter the boolean value in here%
$listName = "%enter your list-name%"
$siteUrl = "%enter the url of your site%"
$fieldName = "%enter the field-name%"
$fieldtype = "%enter the field-type%"

[void][System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$site = new-object Microsoft.SharePoint.SPSite($siteUrl)
$web = $site.rootweb
$spList = $web.Lists[$listName]
if($spList -ne $null)
{
$spList.Fields.Add($fieldName,$fieldtype,$isRequired)
}
$web.Dispose()
$site.Dispose()

Keine Kommentare:

Kommentar veröffentlichen