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"
Name="TextColPowershell"
Description="This is a single line of text column created with powershell script."
DisplayName="Text Column Powershell"
StaticName="TextColPowershell"
Group="Test Custom Columns"
Hidden="FALSE"
Required="FALSE"
Sealed="FALSE"
ShowInDisplayForm="TRUE"
ShowInEditForm="TRUE"
ShowInListSettings="TRUE"
ShowInNewForm="TRUE"></Field>'
#See field XML on console
write-host $fieldXMLString
#Create site column from XML string
$rootWeb.Fields.AddFieldAsXml($fieldXMLString)
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:
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"
Name="TextColPowershell"
Description="This is a single line of text column created with powershell script."
DisplayName="Text Column Powershell"
StaticName="TextColPowershell"
Group="Test Custom Columns"
Hidden="FALSE"
Required="FALSE"
Sealed="FALSE"
ShowInDisplayForm="TRUE"
ShowInEditForm="TRUE"
ShowInListSettings="TRUE"
ShowInNewForm="TRUE"></Field>'
#See field XML on console
write-host $fieldXMLString
#Create site column from XML string
$rootWeb.Fields.AddFieldAsXml($fieldXMLString)
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