Retrieve, Create Or Delete Site Content Type Using CSOM With PowerShell On Office 365 / SharePoint Online

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 SharePoint Online 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. 
    1. $siteURL = ""  
    2. $userId = ""  
    3. $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.
    1. $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)  
    2. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)  
    3. $ctx.credentials = $creds  
  2. Then access the necessary parameters using the context, load the objects and execute the query
    1. $contentTypes = $ctx.web.contenttypes  
    2. $ctx.load($contentTypes)  
    3. $ctx.executeQuery()  
  3. Loop through the result object and get the necessary content type information,
    1. foreach($ct in $contentTypes){  
    2.     write-host $ct.name  
    3. }  
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.
  1. Follow the first three steps from above section.
  2. Check if the new content type already exists using the below logic. Follow the below steps to add new content type,
    1. $ctGroup = "Custom CT Group"
      $ctName = "CustomCT"
    2. foreach($ct in $contentTypes){  
    3.     if($ct.name -eq $ctName){  
    4.         $ctExists = $true  
    5.         Write-Host "Content Type $ctName already exists" -ForegroundColor Red  
    6.         break  
    7.     }         
    8. }  
  3. Get the parent content type using GetById method. In this case, parent content type is document.
  4. Initialize the ContentTypeCreationInformation object.
  5. Set the required parameters for new content type. The necessary parameters are name, parent content type and content type group.
  6. Add the content type.
  7. Load and execute the query.
    1. if(!$ctExists){  
    2.     Write-Host "Adding Content Type" -ForegroundColor Blue  
    3.     $parentCT = $contentTypes.GetById("0x0101")  
    4.     $ctx.load($parentCT)  
    5.     $ctx.ExecuteQuery()  
    6.   
    7.     $newCT = New-Object Microsoft.SharePoint.Client.ContentTypeCreationInformation  
    8.     $newCT.Name = $ctName  
    9.     $newCT.ParentContentType = $parentCT  
    10.     $newCT.group = $ctGroup  
    11.     $newCT = $contentTypes.add($newCT)  
    12.     $ctx.load($newCT)  
    13.     $ctx.ExecuteQuery()  
    14. }  
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. 
  1. Follow the first three steps from "Retrieve Content Type" section above.
  2. From the available content types, find out the id of the content type to be deleted. The following snippet helps you in identifying the id,
    1. foreach($ct in $contentTypes){  
    2.     if($ct.name -eq $ctName){  
    3.         $ctId = $ct.Id  
    4.         $ctExists = $true  
    5.         $existingCT = $ct  
    6.         Write-Host "Content Type $ctId found" -ForegroundColor Red  
    7.         break  
    8.     }         
    9. }  
  3. Then, remove the content type using the delete object method.

  4. Using the context, execute the query,
    1. if($ctExists){  
    2.     $existingCT.DeleteObject()  
    3.     $ctx.ExecuteQuery()  
    4. }  
  5. 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.

Up Next
    Ebook Download
    View all
    Learn
    View all