Intro
Content types in SharePoint are vital for structuring and managing content across sites and libraries. Each content type is associated with a unique Content Type ID, which is necessary for various administrative tasks, including scripting and automation. This article provides a straightforward guide on how to find a Content Type ID using PowerShell.
PnP Powershell
The easiest way to prgrammatically get SharePoint content type ID, is through PnP Powershell. You need to get the content type first, then its ID.
From a list
Option 1
Get all content types, a list of their names and Ids.
# Connect to SharePoint Online
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yoursite" -Interactive
# Specify the list name
$listName = "Your List Name"
# Get all content types from the list
$contentTypes = Get-PnPContentType -List $listName
# Output content type names and IDs
$contentTypes | ForEach-Object { Write-Host "Content Type: $($_.Name), ID: $($_.Id.StringValue)" }
# Disconnect from SharePoint Online
Disconnect-PnPOnline
Option 2
Get all content types, a list of their names and Ids.
# Connect to SharePoint Online
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yoursite" -Interactive
# Specify the list name
$listName = "Your List Name"
# Get all content types from the list
$contentTypes = Get-PnPContentType -List $listName
# Output content type names and IDs
$contentTypes | select name, Id
# Disconnect from SharePoint Online
Disconnect-PnPOnline
Option 3
Get all content types, a list of their names and Ids.
# Connect to SharePoint Online
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yoursite" -Interactive
# Specify the list name
$listName = "Your List Name"
# Get all content types from the list
Get-PnPContentType -List $listName | select name, Id
# Disconnect from SharePoint Online
Disconnect-PnPOnline
Option 4
Get the ID of a single content type in SharePoint list.
# Connect to SharePoint Online
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yoursite" -Interactive
# Specify the name of the content type you want to find
$contentTypeName = "Your Content Type Name"
# Get the content type from the site
$contentType = Get-PnPContentType -Identity $contentTypeName -List $listName
# Output the Content Type ID
Write-Host "Content Type ID: $($contentType.Id.StringValue)"
# Disconnect from SharePoint Online
Disconnect-PnPOnline
From a site
Option 1
Get all content types from a SharePoint site, and list their names and Ids.
# Connect to SharePoint Online
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yoursite" -Interactive
# Get all content types from the list
$contentTypes = Get-PnPContentType
# Output content type names and IDs
$contentTypes | ForEach-Object { Write-Host "Content Type: $($_.Name), ID: $($_.Id.StringValue)" }
# Disconnect from SharePoint Online
Disconnect-PnPOnline
Option 2
Get all content types from a SharePoint site, and list their names and Ids.
# Connect to SharePoint Online
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yoursite" -Interactive
# Get all content types from the list
$contentTypes = Get-PnPContentType
# Output content type names and IDs
$contentTypes | select name, Id
# Disconnect from SharePoint Online
Disconnect-PnPOnline
Option 3
Get all content types from a SharePoint site, and list their names and Ids.
# Connect to SharePoint Online
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yoursite" -Interactive
# Get all content types from the list
Get-PnPContentType | select name, Id
# Disconnect from SharePoint Online
Disconnect-PnPOnline
Option 4
Get the ID of a single content type in SharePoint list.
# Connect to SharePoint Online
Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yoursite" -Interactive
# Specify the name of the content type you want to find
$contentTypeName = "Your Content Type Name"
# Get the content type from the site
$contentType = Get-PnPContentType -Identity $contentTypeName
# Output the Content Type ID
Write-Host "Content Type ID: $($contentType.Id.StringValue)"
# Disconnect from SharePoint Online
Disconnect-PnPOnline
Using REST API
You can also retrieve Content Type ID using Powershell and REST API. I am using here PnP to get access token, but there are other ways.
# Set variables
$siteUrl = "https://yourtenant.sharepoint.com/sites/yoursite"
$contentTypeName = "Your Content Type Name"
$restUrl = "$siteUrl/_api/web/contenttypes?$filter=Name eq '$($contentTypeName)'"
# Get the access token using PnP PowerShell
$token = Get-PnPAccessToken
# Make the REST API request
$response = Invoke-RestMethod -Uri $restUrl -Headers @{
"Authorization" = "Bearer $token"
"Accept" = "application/json; odata=verbose"
} -Method Get
# Check if content type was found and output the ID
if ($response.d.results.Count -gt 0) {
$contentTypeId = $response.d.results[0].Id.StringValue
Write-Host "Content Type ID: $contentTypeId"
} else {
Write-Host "Content Type not found"
}
PowerShell scripting with CSOM
You can also get SharePoint content type ID using CSOM and Powershell:
- Download and install SharePoint Online SDK.
- Open Powershell ISE.
- Add the SDK libraries:
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"
- Save the following information into variables. Bear in mind that you are connecting to any given SharePoint site or subsite, and -admin is not required in the URL:
$Username="t@trial876.onmicrosoft.com"
$Password=Read-Host -Prompt "Password" -AsSecureString
$Url="https://trial876.sharepoint.com"
- Create context:
$ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $Password)
$ctx.ExecuteQuery()
- Load the site and its content types:
$ctx.Load($ctx.Web)
$ctx.Load($ctx.Web.ContentTypes)
$ctx.ExecuteQuery()
- You now have all the necessary data. It’s up to you and your specific requirements to decide what you want to display next.
All content types and their IDs:
foreach($cc in $ctx.Web.ContentTypes)
{
Write-Host $cc.Name " ID: " $cc.ID
}
Content type by a given name:
foreach($cc in $ctx.Web.ContentTypes)
{
if($cc.Name -eq "East Asia Contact")
{
Write-Host $cc.Id
}
}
PowerShell SPOMod
An old favourite of mine, slowly getting deprecated, but I am mentioning it here out of sentiment.
Prerequisites
- Download and install SharePoint Online SDK.
- Download SPOMod available here.
- Open SPOMod current file (in ISE or NotePad) and scroll down to the following lines:
# Paths to SDK. Please verify location on your computer.
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"
- Make sure that the paths correspond to the locations of SDK on your machine. Most often you need to change 16 into 15.
- Save the file.
- Open PowerShell as an Administrator.
- Run Import-Module PathToTheSPOModFile
- Run Connect-SPOCSOM cmdlet.
List Content Type
To see all properties of the content type, enter:
Get-SPOContentType -ListTitle MyListTitle
To see only IDs, enter:
Get-SPOContentType -ListTitle ghgh | select name, id
Site Content Type
To see all content types with all their properties, enter:
Get-SPOContentType
To see only content types and their respective ids:
Get-SPOContentType | select name, id
You can add | ft -autosize to see untruncated results in the Shell: |
Get-SPOContentType | select name, id | ft -AutoSize
From a subsite
Get-SPOContentType -Available | select name, id | ft -Autosize
By name
Get-SPOContentType | where {$_.Name -eq "Task"} | select id