top of page
Writer's pictureNakkeeran Natarajan

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


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.

  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. {      

  3. write-host $ct.name  

  4. }  

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,

  1. $ctGroup = "Custom CT Group"$ctName = "CustomCT"foreach($ct in $contentTypes)

  2. {  

  3. if($ct.name -eq $ctName){          

  4. $ctExists = $true         

  5. Write-Host "Content Type $ctName already exists" -ForegroundColor Red  

  6. break     }         

  7. }  

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.

  1. if(!$ctExists){      

  2. Write-Host "Adding Content Type" -ForegroundColor Blue      

  3. $parentCT = $contentTypes.GetById("0x0101")      

  4. $ctx.load($parentCT)      

  5. $ctx.ExecuteQuery()       

  6. $newCT = New-Object Microsoft.SharePoint.Client.ContentTypeCreationInformation      

  7. $newCT. Name = $ctName      

  8. $newCT.ParentContentType = $parentCT      

  9. $newCT. group = $ctGroup      

  10. $newCT = $contentTypes.add($newCT)      

  11. $ctx.load($newCT)      

  12. $ctx.ExecuteQuery()  

  13. }  

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,

  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. }  


d. Then, remove the content type using the delete object method.

e. Using the context, execute the query,

  1. if($ctExists){      

  2. $existingCT.DeleteObject()      

  3. $ctx.ExecuteQuery()  

  4. }  

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.

0 comments

Recent Posts

See All

Commentaires


bottom of page