SharePoint 2013 – Getting Content Type by Name – CSOM

Following scenario is using PowerShell and the Client Side Object Model..

Task: Get a specific Content Type by name rather than ID – as we don’t know what the ID is!

# Get the web context
$web = $ctx.Web
# Gets a collection of available content types
$contentTypesColl = $web.AvailableContentTypes
$load = [Microsoft.SharePoint.Client.ClientContext].GetMethod("Load")
$listLoad = $load.MakeGenericMethod([Microsoft.SharePoint.Client.ContentTypeCollection])
# Iterate through the content types matching the name
# Once match is found, save content type ID to a variable
$LegalDocId = 0;
foreach($ctype in $contentTypesColl)
$i = 0
if($ctype[$i].Name -eq "Legal Document")
Write-Host $ctype[$i].Name -ForegroundColor Cyan
$LegalDocId = $ctype[$i].Id

# Once content type ID is found, apply content type to a document library
if($LegalDocId -ne '0')
# Get the library
$lib = $web.Lists.GetByTitle("Documents")

# Enable content types
$lib.ContentTypesEnabled = $true
# Apply the content type
$contentType = $web.ContentTypes.GetById($LegalDocId.ToString())
Add-LogMessage -message "Adding content type to library"

# Update the library

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s