SharePoint Online: How to Get Content Type ID using PowerShell?

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 script to get content type by id programmatically for SharePoint online.

How to Find Content Type ID in SharePoint Online?
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 "Ctype" parameter with the content type ID! sharepoint online get 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 code.
#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
Lets get 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 
#Config Variables
$SiteURL = "https://crescenttech.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, To get the content type ID at list level, use: -List parameter. E.g.
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com"
$ListName = "Project Documents"
$ContentTypeName ="Crescent Invoice Template V2"

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

#Get the content type from List
$ContentType = Get-PnPContentType -Identity $ContentTypeName -List $ListName

#Get Content Type ID
$ContentType.Id.ToString() 
SharePoint Online: How to Get Content Type ID using PowerShell? SharePoint Online: How to Get Content Type ID using PowerShell? Reviewed by Salaudeen Rajack on October 16, 2016 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.