SharePoint content types help to standardise the use of business content on the site.
The concept is simple – you define a set of site columns, put them together is a signle or few content types and there you have it, now the content type can be used in many of the lists on the site.
In here, I’d like to demonstrate how you can provision a new content type using PowerShell which will help you with deploying a simple or more complex content types fast.
I assume you have your development environment set up.
For this sample, you can use SharePoint 2010 Fundation or Server depending on your needs.
I will be using a SharePoint 2010 Server with intranet.contoso.com as my intranet site.
Open SharePoint 2010 Management Shell from the start menu of your server and execute the following script:
$SPSite = Get-SPSite | Where-Object
{$_.Url -eq "http://intranet.contoso.com"}
$RootWeb = $SPSite.RootWeb
$NewListTemplate = $RootWeb.ListTemplates["Announcements"]
$NewListInstance = $RootWeb.Lists.Add("Announcements List",
"Announcements List", $NewListTemplate)
$SPList = $RootWeb.Lists[$NewListInstance]
$SPList.OnQuickLaunch = $true
$SPList.Update()
$EventContentType = $RootWeb.AvailableContentTypes["Event"]
$ContentType = New-Object
Microsoft.SharePoint.SPContentType
-ArgumentList @($EventContentType, $RootWeb.ContentTypes, "EventCT")
$RootWeb.ContentTypes.Add($ContentType)
$NewCT = $RootWeb.ContentTypes[$ContentType.Id]
$NewCTFields = $RootWeb.Fields
$CTField = $NewCTFields.Add("NewField",
[Microsoft.SharePoint.SPFieldType]::Boolean, $false)
$CTFieldObject = $NewCTFields.GetField($CTField)
$NewCT.FieldLinks.Add($CTFieldObject)
$NewCT.Update()
$SPList.ContentTypes.Add($NewCT)
$SPList.ContentTypesEnabled = $true
$SPList.Update()
There you have it, the blocks of code are pretty self explanatory.
The first one, creates an announcements list.
Next, we create a content type named EventCT. This content type will have only one bolean field added to it amed NewField, pretty original ![]()
… and lastly, we associate the content type to the list.
If you think this was handy, check out my new book for more handy examples and tips on SharePoint 2010 development.
Enjoy!
Pingback: Automatically provisioning SharePoint 2010 content types … | Mastering Sharepoint