Search This Blog

Monday, June 20, 2011

Creating site column using powershell script : SharePoint 2010

Quite often during deployment in sharepoint projects, we come across a requirement wherein we have to create a no. of site columns and content types on the root site of the site collection.

If done through the UI,this task takes some time and is also prone to errors. This can be done in a repetitive fashion easily using powershell scripts.

This powershell script shows how to create a site column using the field xml for the site column.

The same script can also be used to create site column in a content type hub site from where the site columns
can be propagated to all site collections.

Add-PSSnapIn "Microsoft.SharePoint.Powershell"
#Get the site collection and web object
$siteColl = Get-SPSite -Identity "http://br-pc-341:2222/"
$rootWeb = $siteColl.RootWeb
#Assign fieldXMLString variable with field XML for site column
$fieldXMLString = '<Field Type="Text"
Description="This is a single line of text column created with powershell script."
DisplayName="Text Column Powershell"
Group="Test Custom Columns"
#See field XML on console
write-host $fieldXMLString
#Create site column from XML string

This will create a single line of text column with the name 'TextColPowershell' in the root site of the specified site collection as is shown in the screenshot:

No comments:

Post a Comment