SharePoint Online: How to Get Content Type ID using PowerShell?
Content types in SharePoint are used to define the metadata and behaviors of a document or item in a list and are identified by a unique ID known as the Content Type ID. My other article speaks on How to find the content type id in SharePoint, using SharePoint UI and PowerShell for SharePoint On-premises; this post provides a script to get content type by id programmatically for SharePoint Online.
How to Find Content Type ID in SharePoint Online?
One way to get the content type ID in SharePoint Online is to use the SharePoint Online user interface. Here’s how:
To get the content type ID in SharePoint Online, you can Go to List/Site Content Types >> Pick your content type >> The URL will have the “Ctype” parameter with the content type ID!
SharePoint Online: Get content type ID using CSOM
Here is the PowerShell script to get content type ID in SharePoint using CSOM:
#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"
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com"
$ContentTypeName="Crescent Project Report"
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred
#Get content types of the web
$ctx.Load($ctx.Web.ContentTypes)
$Ctx.ExecuteQuery()
#Get the content type by its name
$ContentType = $Ctx.Web.ContentTypes | Where {$_.Name -Match $ContentTypeName}
$Ctx.Load($ContentType)
$Ctx.ExecuteQuery()
#sharepoint online get content type id
write-host -f Green "Content Type ID:" $ContentType.Id
}
Catch {
write-host -f Red "Error Getting Content Type ID!" $_.Exception.Message
}
Get Content Type ID from SharePoint Online List
Let’s get the Content Type id for a List content type:
#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"
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/sales/"
$ListName="Projects"
$ContentTypeName="Project Portfolio"
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred
#Get the List and Its content types
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$Ctx.load($List)
$Ctx.load($List.ContentTypes)
$Ctx.ExecuteQuery()
#Get the content type by its name
$ContentType = $List.ContentTypes | Where {$_.Name -Match $ContentTypeName}
$Ctx.Load($ContentType)
$Ctx.ExecuteQuery()
#sharepoint online get content type id
write-host -f Green "Content Type ID:" $ContentType.Id
}
Catch {
write-host -f Red "Error Getting Content Type ID!" $_.Exception.Message
}
This script gets Content Type ID from Name using PowerShell.
PnP PowerShell to Get Content Type ID in SharePoint Online
Use the Get-PnPContentType cmdlet to retrieve the content type and its properties.
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ContentTypeName ="Crescent Project Proposal V1"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#Get the site content type
$ContentType = Get-PnPContentType -Identity $ContentTypeName
#Get Content Type ID
$ContentType.Id.ToString()
Similarly, use the “List” parameter to get the content type ID at the list level. E.g.
#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName = "Project Documents"
$ContentTypeName ="Crescent Invoice Template V2"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#Get the content type from List
$ContentType = Get-PnPContentType -Identity $ContentTypeName -List $ListName
#Get Content Type ID
$ContentType.Id.ToString()
Replace the variables with the site URL, List, and content type names of the content type you want to retrieve.
In conclusion, getting the content type ID in SharePoint Online can be done either through the user interface or using PowerShell. The Content Type ID is a unique identifier for a content type and is used to reference it programmatically. By using the above methods, you can easily retrieve the Content Type ID for a specific content type in SharePoint Online.