Creating Content Types (and Site Columns) with PowerShell

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.

1
Add-PSSnapin Microsoft.SharePoint.PowerShell

I then use Start-SPAssignment to ensure that all objects are disposed of correctly.

1
Start-SPAssignment –Global

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:

1
2
$url = "http://sharepoint/"
$web = get-spweb $url

Then, give the new Content Type a name:

1
$ctypeName = "My PowerShell Content Type"

Next, the new Content Type needs a parent. You can get a list of available Content Types by running:

1
$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”:

1
$ctypeParent = $web.availablecontenttypes["Document"]

Now we have the necessary information, we can build the Content Type object. This is done with new-object:

1
$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”.

1
2
$ctype.Group = "My Content Type Group"
$web.contenttypes.add($ctype)

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.

1
$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:

1
2
$field = $web.fields.getfield("My Text Field")
$field.Group = "My Column Group"

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.

1
2
3
$fieldLink = new-object Microsoft.SharePoint.SPFieldLink($field)
$ctype.fieldlinks.add($fieldLink)
$ctype.update()

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:

1
2
3
4
5
6
$web.fields.add("My Number Field", "Number", $false)
$field = $web.fields.getfield("My Number Field")
$field.Group = "My Column Group"
$fieldLink = new-object Microsoft.SharePoint.SPFieldLink($field)
$ctype.fieldlinks.add($fieldLink)
$ctype.update()

And to create a Choice Field:

1
2
3
4
5
6
7
8
9
10
$web.fields.add("My Choice Field", "Choice", $false)
$field = $web.fields.getfield("My Choice Field")
$field.Group = "My Column Group"
$field.Choices.Add("Option 1")
$field.Choices.Add("Option 2")
$field.Update()

$fieldLink = new-object Microsoft.SharePoint.SPFieldLink($field)
$ctype.fieldlinks.add($fieldLink)
$ctype.update()

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.

1
Stop-SPAssignment -Global

You can also download the full script here.