How to Create a Content Type in SharePoint using PowerShell?

A Content Type in SharePoint is a fundamental framework for any item, such as Task, Document, Event, etc. Every content type has its own set of metadata columns and templates, information management policies, and workflows. In SharePoint, we can create our own custom content types from existing content types, such as “Invoice” content type based on a document content type, and add custom columns to it, say “Invoice Date”.

How to Create a Content Type in SharePoint?

To add a content type in SharePoint, follow these steps:

  1. Go to Site Settings >> Click on “Site Content Types” under the “Web Designer Galleries” group.
  2. In Site content types page, You’ll see the list of default content types like Item, Tasks, Document, etc grouped by sections. Click on the “Create” link at the top.
    create content type in sharepoint using powershell
  3. Provide a name for your custom content type. Optionally, you can enter the description for your new content type to make its purpose clear.
  4. Select the parent content type, such as “Item” from which your content type is based on. You can either create a new content type group or select any existing.
    sharepoint powershell create new content type
  5. Click OK to complete creating the content type. Once the content type is created, The next step is to add the required site columns to the content type to make metadata available from the content type.
Content types are hierarchical – So, when you create a content type at root site level, all sub-sites underneath it inherits site columns and content types from the parent site!

Create Content Type in SharePoint using PowerShell

Let’s create a new content type in SharePoint 2016 using PowerShell:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Set Parameters
$SiteURL = "https://intranet.crescent.com/"
$ContentTypeName = "Crescent Projects"

#Get the Root web
$web = Get-SPWeb $SiteURL

#Get parent content type - "Item"
$ParentContentType = $web.AvailableContentTypes["Item"]

#Check if the content type already exists
$ContentType = $web.ContentTypes[$ContentTypeName]
If($ContentType -eq $NULL)
{
    #Create new content type from  "item" Content Type
    $ContentType = New-Object Microsoft.SharePoint.SPContentType($ParentContentType, $web.ContentTypes, $ContentTypeName)
    $ContentType.Description ="Crescent Projects Content Type"
    $ContentType.Group = "Crescent Content Types"
    $web.ContentTypes.Add($ContentType)
    $web.Update()
    Write-Host "Content Type '$ContentTypeName' Created Successfully!" -ForegroundColor Green
}
else
{
    Write-Host "Content Type '$ContentTypeName' Already Exists!" -ForegroundColor Red
}

This PowerShell creates a new content type in SharePoint. Now the next step is to add some columns to it: let’s add a “Project Name” column to the content type.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Set Parameters
$SiteURL = "https://intranet.crescent.com/"
$ContentTypeName = "Crescent Projects"

#Get the Root web
$web = Get-SPWeb $SiteURL

#Get the Content Type
$ContentType = $web.ContentTypes[$ContentTypeName]

#Check if the content type exists
If($ContentType -ne $NULL)
{
    #Create new Site column
    $FieldName="ProjectName"

    #Check if field exists already
    $Field = $web.Fields | where {$_.InternalName -eq $FieldName}
    If($Field -eq $null)
    {
        #Add a Site Column
        $Field = $Web.Fields.Add($FieldName, [Microsoft.SharePoint.SPFieldType]::Text, $false)
    }
    
    #Get a Field Link to Add the site column to content type
    $Field=$web.Fields[$FieldName]
    $FieldLink = new-object Microsoft.SharePoint.SPFieldLink($Field)
    #Add Site column to content type
    $contentType.Fieldlinks.add($FieldLink)
    $contentType.Update()

    Write-Host "Fied Added to Content Type '$ContentTypeName' Successfully!" -ForegroundColor Green
}
else
{
    Write-Host "Content Type '$ContentTypeName' Doesn't Exist!" -ForegroundColor Red
}

To create a content type in SharePoint Online using PowerShell, refer: How to Create a Content Type in SharePoint Online using PowerShell?

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

Your email address will not be published. Required fields are marked *