Automating the process of creating Content Types can help speed up a deployment, and reduce the risk of human-error. Here’s a quick run through of how to create Content Types in PowerShell for SharePoint 2010/2013.
Before I start, I’d like to thank Jaclyn Saito, whose blog on the same topic gave me a start in this, and also is where a lot of the code comes from. Take a look at her blog here: Create a SharePoint 2010 Content Type Using PowerShell. Also, if you’d prefer to just look at the full script yourself, you can skip to the end for a download link.
Now for the code. As always, start by adding the SharePoint module.
I then use Start-SPAssignment to ensure that all objects are disposed of correctly.
The next step is to open the web where you want to add the Content Type to. Replace the URL with the URL of the Web you want to update:
$url = "http://sharepoint/"
Then, give the new Content Type a name:
$ctypeName = "My PowerShell Content Type"
Next, the new Content Type needs a parent. You can get a list of available Content Types by running:
$web.AvailableContentTypes | Select-Object Name
This gives an output of all of the Content Types available on the current web.
Now we need a reference to the parent Content Type. This can be done using the name given from the previous command. Here. I’m creating a content type which inherits from “Document”:
$ctypeParent = $web.availablecontenttypes["Document"]
Now we have the necessary information, we can build the Content Type object. This is done with new-object:
$ctype = new-object Microsoft.SharePoint.SPContentType($ctypeParent, $web.contenttypes, $ctypeName)
We can then optionally give it a group, and add it to the web. If no group is defined, the Content Type will appear under “Custom Content Types”.
$ctype.Group = "My Content Type Group"
At this point, the Content Type has been added to the web, and is available to use:
It’s now time to add some fields to the Content Type. Each field takes 3 parameters; first it needs a display name, then a field type, and finally it needs a Boolean required option. The full documentation for this method can be found on MSDN. The line below creates a Single Line of Text field called “My Text Field”, and sets it to be non-required.
$web.fields.add("My Text Field", "Text", $false)
Again, once this has been ran, the Site Column is available to use through the UI. We can then optionally add it to a custom group by getting the field, and then updating the “Group” property:
$field = $web.fields.getfield("My Text Field")
Once this has been done, it needs to be added to the content type. This can be done by getting a reference to the SPFieldLink, and then adding it to the content type.
$fieldLink = new-object Microsoft.SharePoint.SPFieldLink($field)
At this point, we now have a content type, with a new text field called “My Text Field”. Using the same principles, we can create fields of different types. To create a number field:
$web.fields.add("My Number Field", "Number", $false)
And to create a Choice Field:
$web.fields.add("My Choice Field", "Choice", $false)
You can find a full list of the field types on MSDN, and use the principles above to create different field types.
Don’t forget, at the end of the script, we need to dispose of all of the objects, else they will continue to take memory until the PowerShell prompt is closed.
You can also download the full script here.