Sunday, October 16, 2016

SharePoint Online: Get Content Type ID using PowerShell-CSOM

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.
To get the content type ID, 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
}



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!


You might also like:

Related Posts Plugin for WordPress, Blogger...