Content types in SharePoint can be considered to be a reusable collection of metadata, Workflows, Event Receivers etc.: Thus, content types can be said to govern the behavior of the list or library, it is attached to. We can create the content types at the list level, as well as, the site level. Site Level Content Types can be created out of the box, as well as programmatically. However, the list level content types can be created only by deploying it as a list definition or by programmatic object model.

site

We can create and delete the site content types out of the box from the site content type page, as shown, above in SharePoint 2016. Let’s see, how we can do the same programmatically, using JavaScript object model.

Internal Implementation

Scope of the article is,

All of these operations will be done, using JavaScript object model.

List all existing Content Types

Let’s see, how to list all the existing content types, using JSOM.

Add Content Type

Now, let’s see how to add a new site content type.

Delete the existing Site Content type

We can delete the content type, either by using its ID or by getting the content type collection and deleting the specific content type by looping through it. The content type ID can be obtained from the content type page’s address bar.

Delete

Once we get the content type ID, we can call the ‘deleteObject’ method on the content type object, as shown in the code, given below:

  1. oSiteContentTypeCollection = oWeb.get_contentTypes();
  2. var oSiteCT = oSiteContentTypeCollection.getById("0x0101001E8913A36EB6AA43852EC8AEB40F963D");
  3. //Delete the content type
  4. oSiteCT.deleteObject();
Full Code
  1. <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
  2. <script language="javascript" type="text/javascript">
  3. $(document).ready(function() {
  4. SP.SOD.executeFunc('sp.js', 'SP.ClientContext', deleteSiteCT);
  5. });
  6. function deleteSiteCT() {
  7. //Get Client Context and Web object
  8. clientContext = new SP.ClientContext.get_current();
  9. var oWeb = clientContext.get_web();
  10. //Get Site CT Object to delete
  11. oSiteContentTypeCollection = oWeb.get_contentTypes();
  12. var oSiteCT = oSiteContentTypeCollection.getById("0x0101001E8913A36EB6AA43852EC8AEB40F963D");
  13. //Delete the content type
  14. oSiteCT.deleteObject();
  15. //Execute the batch
  16. clientContext.executeQueryAsync(QuerySuccess, QueryFailure);
  17. }
  18. function QuerySuccess() {
  19. console.log("Site Content Type has been deleted successfully");
  20. }
  21. function QueryFailure(sender, args) {
  22. console.log('Request failed - ' + args.get_message());
  23. }
  24. </script>
Let’s see, how we can implement it in SharePoint. Save the scripts, given above, onto a text file and upload it to Site Assets library.

SharePoint Implementation

Summary

In this article, we saw, how to list out all the site content types. Create a new site content type and delete an existing content type, using JavaScript object model. This has been tried and tested in both SharePoint 2016 and Office 365(SharePoint Online).