Quantcast
Channel: SharePoint Diary
Viewing all articles
Browse latest Browse all 1058

SharePoint Online: Get Content Type ID using PowerShell-CSOM

$
0
0
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
}

Viewing all articles
Browse latest Browse all 1058

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>