Get All Content Types From Site in SharePoint using JSOM

The example in this topic show how to use JSOM to Get All Content Types From Site in SharePoint

  • var oSiteContentTypes, clientContext;
  • function GetAllSiteContentTypes() {
  •     // You can optionally specify the Site URL here to get the context
  •     // If you don't specify the URL, the method will get the context of the current site
  •     // var clientContext = new SP.ClientContext("http://MyServer/sites/SiteCollection");
  •     clientContext = new SP.ClientContext();
  •  
  •     var oWeb = clientContext.get_web();
  •  
  •     // Get Site Content Type collection object
  •     oSiteContentTypes = oWeb.get_contentTypes();
  •  
  •     // Load Site Content Type collection to Client Context
  •     clientContext.load(oSiteContentTypes);
  •  
  •     // Execute the query to the server.
  •     clientContext.executeQueryAsync(onsuccess, onfailed);
  • }
  •  
  • function onsuccess() {
  •  
  •     // Iterate through Enumerator
  •     var oEnumerator = oSiteContentTypes.getEnumerator();
  •  
  •     var oContentType;
  •  
  •     while (oEnumerator.moveNext()) {
  •         oContentType = oEnumerator.get_current();
  •  
  •         console.log("Content Type Name: " + oContentType.get_name());
  •     }
  • }
  •  
  • function onfailed(sender, args) {
  •     console.log('Failed' + args.get_message() + '\n' + args.get_stackTrace());
  • }
  •  

Thank you for reading this article. This code was tested in SharePoint 2013


© 2019 Code SharePoint