SharePoint Online: Create a Content Type using PowerShell
What is Content Type in SharePoint Online?
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. Or 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.
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.
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.
How to Create a Content Type in SharePoint Online?
To add a content type in SharePoint Online, follow these steps:
- Go to Site Settings >> Click on “Site Content Types” under the “Web Designer Galleries” group.
- On the 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.
- Provide a name for your custom content type. Optionally, you can enter the description for your new content type to make it clear.
- 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.
- Click OK to complete adding 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.
Now, let’s see the PowerShell script to create content type programmatically in SharePoint.
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”.
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?