Introduction
In this article, you will learn how we can retrieve, create, or delete content type using CSOM with PowerShell. This is mainly focused on using PowerShell scripts for SharePointOnline sites.
Retrieve Content Type
First we will see how we can get the existing content types available on the SharePoint site. The steps followed here are very similar to the steps following CSOM or JSOM programming.
$siteURL = ""
$userId = ""
$pwd = Read-Host -Prompt "Enter password" -AsSecureString
1. Initialize context object with the site URL parameter. Then initialize the SP Online Credentials with the above parameters and set it to the context.
$creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
$ctx.credentials = $creds
2. Then access the necessary parameters using the context, load the objects and execute the query.
$contentTypes = $ctx.web.contenttypes
$ctx.load($contentTypes)
$ctx.executeQuery()
3. Loop through the result object and get the necessary content type information,
foreach($ct in $contentTypes)
{
write-host $ct.name
}
This will get all the content type names available on the site. Next, We will see how we can create content types to the site.
Create Content Type:
Here we will see how we can create a content type on the site. The following steps depict the flow.Follow the first three steps from above section. a. Check if the new content type already exists using the below logic.
b. Follow the below steps to add new content type,
$ctGroup = "Custom CT Group"$ctName = "CustomCT"foreach($ct in $contentTypes)
{
if($ct.name -eq $ctName){
$ctExists = $true
Write-Host "Content Type $ctName already exists" -ForegroundColor Red
break }
}
c. Get the parent content type using GetById method. In this case, parent content type is document. d. Initialize the ContentTypeCreationInformation object.
e. Set the required parameters for new content type.
f. The necessary parameters are name, parent content type and content type group.
g. Add the content type.Load and execute the query.
if(!$ctExists){
Write-Host "Adding Content Type" -ForegroundColor Blue
$parentCT = $contentTypes.GetById("0x0101")
$ctx.load($parentCT)
$ctx.ExecuteQuery()
$newCT = New-Object Microsoft.SharePoint.Client.ContentTypeCreationInformation
$newCT. Name = $ctName
$newCT.ParentContentType = $parentCT
$newCT. group = $ctGroup
$newCT = $contentTypes.add($newCT)
$ctx.load($newCT)
$ctx.ExecuteQuery()
}
The content type will be added at the root site collection level. You can add the columns manually or using script. Try navigating to the site content types page “/_layouts/15/mngctype.aspx”. You will now see the content type. The following data can be found on the content types page.
Next, we will see how to delete content type.
Delete Content Type:
Here we will see how we can delete the content type from the site. Following steps depict you the flow.
a. Follow the first three steps from "Retrieve Content Type" section above.
b. From the available content types, find out the id of the content type to be deleted.
c. The following snippet helps you in identifying the id,
foreach($ct in $contentTypes){
if($ct.name -eq $ctName){
$ctId = $ct.Id
$ctExists = $true
$existingCT = $ct
Write-Host "Content Type $ctId found" -ForegroundColor Red
break }
}
d. Then, remove the content type using the delete object method.
e. Using the context, execute the query,
if($ctExists){
$existingCT.DeleteObject()
$ctx.ExecuteQuery()
}
f. The content type will be removed from root site collection level. Try navigating to the site content types page “/_layouts/15/mngctype.aspx”. You will not see the content type.
Summary:
Thus you have learnt how to retrieve, create or delete content type from/to the SharePoint Online or O365 sites using CSOM with PowerShell commands.
Comments