Thursday, May 22, 2014

SharePoint 2013 add a column to a list via PowerShell

When the requirement to add a new column to a SharePoint list after it's initial creation comes up, using a PowerShell script is a great way of completing the task. It takes away the possibility of user error (naming the field incorrectly across multiple environments for example) and the script can even be checked into source control and used in deployment packages etc.

The following PowerShell code will add a new Boolean field named "Field Name" to the list called "List Name" on the "http://siteurl/" SharePoint installation. The script can of course be modified to add multiple columns, to multiple lists and the various other field types.

Add-PSSnapin Microsoft.SharePoint.PowerShell

        $site = SPSite("http://siteurl/")
        $web = $site.RootWeb
        $spList = $web.Lists["List Name"]
        $spFieldType = [Microsoft.SharePoint.SPFieldType]::Boolean
        $spList.Fields.Add("Field Name",$spFieldType,$false)

No comments:

Post a Comment