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, but remember there are 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-
We can play with this setting using PowerShell as described below-
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 list 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 success message to the Users if Step 4 executes successfully
In Step 6 we will call function that we have explained in Step 1-5
Once this script executes successfully we can see list setting updated by navigating “Advanced Settings” of the list as shown below-
Operation: How to “Add Existing Content Type To List”
We can see this setting available under “List Settings” for the list as shown below-
We can play with this setting using PowerShell as described below-
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 success message to the Users if Step 4 executes successfully
In Step 6 we will call function that we have explained in Step 1-5
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
Operation: How to “Get List of All Content Types”
We can see this setting available under “List Settings” for the list as shown below-
We can play with this setting using PowerShell as described below-
In Step 1 we will get the object reference to the respective Web in context of 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 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 function that we have explained in Step 1-7
Once this script executes successfully we can see list of content types by navigating “List Settings” of the list as shown below-
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-
We can delete any Content Type from list by using PowerShell by using PowerShell as described below-
In Step 1 we will get the object reference to the respective Web in context of 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 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 are 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 success message after Step 9 got executed successfully
In Step 11 we will call function that we have explained in Step 1-10
Once this script is executed successfully the respective content type will be deleted from Content Types List and this can we verified from the UI by navigating through “List Settings”
That is all for this demo.
Hope you find it helpful.
About the Author:
Prashant Bansal has over 12 years of extensive experience in Analysis, Design, Development, Testing, Deployment, & Documentation using various Microsoft technologies with excellent communication skills, problem solving and inter-personal skills with an avid interest in learning and adapting to new technologies.
Reference: Bansal, P (2018). SharePoint Online: Working with List Content Types Using PowerShell. Available at: https://howtodowithsharepoint.wordpress.com/2018/02/19/sharepoint-online-working-with-list-content-types-using-powershell/ [Accessed 11 July 2018]