SharePoint Online - Working With List Content Types Using PowerShell

In this article, we will discuss the configuration of some of the important operations on Content Type for SharePoint Online Lists. Though I have taken only a handful of operations to showcase in this demo, remember there are a lot more to explore.

Operation - How to enable “Content Types” for List

We can see this setting available under “Advanced Settings” for the list as shown below,

Working With List Content Types Using PowerShell

We can play with this setting using PowerShell as described below,

Working With List Content Types Using PowerShell

In Step 1 we will get the object reference to the respective list by calling “GetByTitle” method.

In Step 2 we will set “ContentTypesEnabled” to True.

In Step 3 we will update the listed property by calling “Update” method.

In Step 4 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method.

In Step 5 we will display a success message to the Users if Step 4 executes successfully.

In Step 6 we will call the function that we have explained in Step 1-5.

Working With List Content Types Using PowerShell

Once this script executes successfully we can see list setting updated by navigating to “Advanced Settings” of the list as shown below,

Working With List Content Types Using PowerShell
Operation - How to “Add Existing Content Type To List”

We can see this setting available under “List Settings” for the list as shown below,

Working With List Content Types Using PowerShell

We can play with this setting using PowerShell as described below,

Working With List Content Types Using PowerShell

In Step 1 we will get the object reference to the respective Web.

In Step 2 we will get the object reference to the respective list by calling “GetByTitle” method.

In Step 3 we will get the object reference to the respective list by calling “GetById” method on Content Types collection.

In Step 4 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method.

In Step 5 we will display a success message to the Users if Step 4 executes successfully.

In Step 6 we will call a function that we have explained in Step 1-5.

Working With List Content Types Using PowerShell

Once this script executes successfully we can see a new content type added by navigating “List Settings” of the list as shown below-

We can notice the new content type added to the List Content Types Collection.

Working With List Content Types Using PowerShell
Operation - How to “Get List of All Content Types”

We can see this setting available under “List Settings” for the list as shown below,

Working With List Content Types Using PowerShell

We can play with this setting using PowerShell as described below,

Working With List Content Types Using PowerShell

In Step 1 we will get the object reference to the respective Web in the context in which this code is executing.

In Step 2 we will get the object reference to the respective list by calling “GetByTitle” method.

In Step 3 we will get List Content Types Collection.

In Step 4 we call the “Load” function to retrieve List Content Types Collection properties from the server.

In Step 5 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method.

In Step 6 & 7 we will loop through the Content Types Collection and display relevant properties (ex. Name, ID and so on) from the collection.

In Step 8 we will call the function that we have explained in Step 1-7.

Working With List Content Types Using PowerShell

Once this script executes successfully we can see the list of content types by navigating “List Settings” of the list as shown below-

Working With List Content Types Using PowerShell
Operation - How to “Delete Existing Content Types” From List

We can see the List of Content Types already added to List under “List Settings” as shown below-

Working With List Content Types Using PowerShell

We can delete any Content Type from the list by using PowerShell by using PowerShell as described below-

Working With List Content Types Using PowerShell

In Step 1 we will get the object reference to the respective Web in the context in which this code is executing.

In Step 2 we will get the object reference to the respective list by calling “GetByTitle” method.

In Step 3 we will get List Content Types Collection.

In Step 4 we call the “Load” function to retrieve List Content Types Collection properties from the server.

In Step 5 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method.

In Step 6 we will loop through the Content Types Collection.

In Step 7 we will look for required content types that are intended to delete. In this example, we will compare the Content Type Name to ensure that only intended content type is deleted.

In Step 8 we will call “DeleteObject” method to delete the respective content type from Content Types Collection of List.

In Step 9 we will send the batch request to SharePoint Server for processing by calling “ExecuteQuery” method.

In Step 10 we will display the success message after Step 9 got executed successfully.

In Step 11 we will call a function that we have explained in Step 1-10.

Working With List Content Types Using PowerShell

Once this script is executed successfully the respective content type will be deleted from Content Types List and this can be verified from the UI by navigating through “List Settings”.

Working With List Content Types Using PowerShell

 

Code Snippet

  1. function EnableListContentTypes() {  
  2.     $docLib = $clientContext.Web.Lists.GetByTitle("ProductsDocuments");  
  3.     $docLib.ContentTypesEnabled = $true;  
  4.     $docLib.Update();  
  5.     $clientContext.ExecuteQuery();  
  6.     Write - Host - ForegroundColor Blue "Content Types Enabled"  
  7. }  
  8.   
  9. function AddExistingContetTypeToList() {  
  10.     #Custom Content Types by PowerShell - 0x010080D5D9F6B5124249BDCE44D1265E90BF  
  11.     $web = $clientContext.Web;  
  12.     $list = $clientContext.Web.Lists.GetByTitle("ProductsDocuments")  
  13.     $ct = $web.ContentTypes.GetById("0x010080D5D9F6B5124249BDCE44D1265E90BF");  
  14.     $addedCt = $list.ContentTypes.AddExistingContentType($ct);  
  15.     $clientContext.ExecuteQuery();  
  16. }  
  17.   
  18. function GetListContentTypes() {  
  19.     $web = $clientContext.Web;  
  20.     $list = $web.Lists.GetByTitle("ProductsDocuments");  
  21.     $ctColl = $list.ContentTypes;  
  22.     $clientContext.Load($ctColl);  
  23.     $clientContext.ExecuteQuery();  
  24.     foreach($ct in $ctColl) {  
  25.         write - host - ForegroundColor blue $ct.Name "-"  
  26.         $ct.ID  
  27.     }  
  28. }  
  29.   
  30. function DeleteListContentType() {  
  31.     $web = $clientContext.Web;  
  32.     $list = $web.Lists.GetByTitle("ProductsDocuments");  
  33.     $ctColl = $list.ContentTypes;  
  34.     $clientContext.Load($ctColl);  
  35.     $clientContext.ExecuteQuery();  
  36.     foreach($ct in $ctColl) {  
  37.         if ($ct.Name - eq "Folder") {  
  38.             $ct.DeleteObject();  
  39.             break;  
  40.         }  
  41.     }  
  42.     $clientContext.ExecuteQuery();  
  43. }  
That is all for this demo.

Hope you find it helpful.