SharePoint Online: Create a Content Type using PowerShell

What is Content Type in SharePoint?

Content types are a set of columns grouped together to serve a specific purpose. Think about an Invoice template! Instead of you and your users ending up creating different templates each time from scratch, you can define an Invoice template once, and your users can start consuming it at any number of lists or document libraries! In this way, you force standardization in your collaboration environment.

Think of a “Project Tracking” list with columns required to track projects for your organization. Instead of creating and adding each column for the Project tracking list every time, you can utilize the project tracking content type template in any number of lists in a few clicks. In this blog post, we will dive into the world of content types in SharePoint Online and explore two methods for creating them: using the web browser interface and leveraging PowerShell.

How to Create a Content Type in SharePoint Online?

SharePoint Online lists or document libraries can contain multiple content types. E.g., You can add multiple content types, such as “Invoice”, “Proposal”, “Purchase Order”, etc., to a library to organize everything related to a project as a single entity. Once you associate a content type with the SharePoint Online list or library, SharePoint lets you create new items of that content type from the New Item command in that list or library. In the case of a document content type, you can also define a document template that will be the base for all the documents created from this particular content type.

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

  1. Go to Site Settings >> Click on “Site Content Types” under the “Web Designer Galleries” group.
  2. On the site content types page, you’ll see a list of default content types, such as Items, Tasks, Documents, etc., grouped by section. Click on the “Create” link at the top.
  3. Provide a name for your custom content type. Optionally, you can enter the description for your new content type to make it clear.
  4. Select the parent content type such as “Item” on which your content type is based on. You can either create a new content type group or select an existing one. sharepoint online powershell create content type
  5. Click OK to complete adding the content type. Once the content type is created, the next step is to add the required Site columns to make metadata available from it.

Now, let’s see the PowerShell script to create content type programmatically in SharePoint. As a best practice, content types should be ideally defined at the top site, independent of any list or library. It can be reused at any SharePoint site underneath or even across SharePoint site collections with a content type hub.

SharePoint Online: Create Content Type using PowerShell

Creating a custom content type can be automated with the help of PowerShell. Here is how to create a content type in SharePoint Online using PowerShell.  

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#function to turn ON Content Type in SharePoint Online list or library
Function Create-SPOContentType()
{ 
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $CTypeName,
        [Parameter(Mandatory=$true)] [string] $CTypeDesc,
        [Parameter(Mandatory=$true)] [string] $ParentCTypeName,
        [Parameter(Mandatory=$true)] [string] $CTypeGroup
    )

    Try {
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
        
        #Get all content types from the site
        $ContentTypeColl = $Ctx.web.ContentTypes
        $Ctx.Load($ContentTypeColl)
        $Ctx.ExecuteQuery()

        #Get the parent content type
        $ParentCType = $ContentTypeColl| Where {$_.Name -eq $ParentCTypeName}

        #Check if content type exists already
        $ContentType = $ContentTypeColl| Where {$_.Name -eq $CTypeName}


        If($ContentType -ne $Null)
        {
            Write-host "Content type '$CTypeName' already exists!" -ForegroundColor Yellow
        }
        else
        {
            #Specify properties for the new content type
            $CTypeCreationInfo=New-Object Microsoft.SharePoint.Client.ContentTypeCreationInformation
            $CTypeCreationInfo.Name=$CTypeName
            $CTypeCreationInfo.Description=$CTypeDesc
            $CTypeCreationInfo.Group=$CTypeGroup
            $CTypeCreationInfo.ParentContentType=$ParentCType

            # sharepoint online powershell create content type
            $ContentType=$ContentTypeColl.Add($CTypeCreationInfo)
            $Ctx.ExecuteQuery()
           
            Write-host "Content Type '$CTypeName' Created Successfully!" -ForegroundColor Green
        }
    }
    Catch {
        write-host -f Red "Error Creating Content Type!" $_.Exception.Message
    } 
}

#Set parameter values
$SiteURL="https://crescent.sharepoint.com"
$CTypeName="Projects"
$CTypeDesc="Content type for Project template"
$ParentCTypeName="Item"
$CTypeGroup="Crescent Projects"

#Call the function
Create-SPOContentType -SiteURL $SiteURL -CTypeName $CTypeName -CTypeDesc $CTypeDesc -ParentCTypeName $ParentCTypeName -CTypeGroup $CTypeGroup

This script creates a content type in SharePoint Online from the specified parent content type. Here in this example, I’ve created the “Project” content type from the “Item” content type, which has “Title” as its default field. You may want to add additional fields to the content type. Here is how you can add columns to a content type in SharePoint Online with PowerShell: How to Add a Site Column to Content Type in SharePoint Online using PowerShell?

Create Content Type in SharePoint Online using PnP PowerShell

Here is the PnP PowerShell script to create a content type in SharePoint Online using Add-PnPContentType cmdlet:

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ContentTypeName ="Crescent Projects V3"
$ContentTypeDescription ="Base Content Type for Crescent Projects Template"
$ContentTypeGroupName = "Crescent Content Types"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Create Content Type
Add-PnPContentType -Name $ContentTypeName -Description $ContentTypeDescription -Group $ContentTypeGroupName

This creates a new content type based on the default parent “Item”.

create content type in sharepoint online using powershell

You can specify the parent content type as:

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ContentTypeName ="Crescent Project Proposal V1"
$ContentTypeDescription ="Base Content Type for Crescent Project Prposal Template"
$ContentTypeGroupName = "Crescent Content Types"
$ParentContentTypeName ="Document" 

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the Parent content type
$ParentContentType = Get-PnPContentType -Identity $ParentContentTypeName

#Create Content Type
Add-PnPContentType -Name $ContentTypeName -Description $ContentTypeDescription -Group $ContentTypeGroupName -ParentContentType $ParentContentType

Once the content type is ready, you can add the content type to lists and libraries: How to Add a Content type to a List in SharePoint Online?

Wrapping up

In this blog post, we explored two methods for creating content types in SharePoint Online: using the web browser interface and leveraging PowerShell. The web browser approach provides a user-friendly, point-and-click experience for creating and managing content types. On the other hand, the PowerShell approach offers a powerful and efficient way to create content types programmatically. By using the CSOM and the PnP PowerShell cmdlet New-PnPContentType, You can automate the process of creating content types, making it faster and more scalable.

Regardless of the approach you choose, creating content types in SharePoint Online provides numerous benefits. Content types help maintain consistency, improve searchability, and streamline content management processes. By defining metadata columns, document templates, and workflows as needed, content types enable organizations to structure and categorize their content effectively.

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 *