Creating SharePoint Content Types with PowerShell

The following snippet creates a Content Type in SharePoint on-prem. Notice that the code checks for the existence of the content type before creating it – because typically you will be using this in a larger provisioning script, and it makes sense to let your code run more than once – so as you build a solution, you can just add new elements and run it again.

# Connect to SharePoint Site
$web = Get-SPWeb "https://server/sites/sitecollection/subsite"

# Set Content Type name and group
$content_type_name = "My Content Type"
$content_type_group = "My Content Types"

if ($web.ContentTypes[$content_type_name]){
    # get the existing content type
    $content_type = $web.ContentTypes[$content_type_name]

} else {
    # create a new content type
    $content_type_parent = $web.AvailableContentTypes["Item"]
    $content_type = new-object Microsoft.SharePoint.SPContentType($content_type_parent, $web.ContentTypes, $content_type_name)
    $content_type.Group = $content_type_group
    $web.ContentTypes.Add($content_type) > $null
    $content_type = $web.ContentTypes[$content_type_name]

# Release resources

Also notice that the content type will be provisioned in the web you connect to – but must be uniquely named across the entire site collection.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.