Set Site Content Type Read Only in SharePoint using JSOM

The example in this topic show how to use JSOM to Set Site Content Type Read Only in SharePoint

  • var oSiteContentTypes, clientContext;
  •  
  • function SetSiteContentTypeReadOnly() {
  •     // 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();
  •  
  •     // Counter variable to be used to break below loop
  •     var counter = 0;
  •  
  •     while (oEnumerator.moveNext()) {
  •         // This line of code is written to break the loop
  •         if (counter == 1) {
  •             break;
  •         }
  •  
  •         var oContentType = oEnumerator.get_current();
  •  
  •         // Specify the Content Type Name here
  •         if (oContentType.get_name() == "New Title") {
  •  
  •             counter += 1;
  •  
  •             // set Content Type read only
  •             oContentType.set_readOnly(true);
  •  
  •             // Update content type
  •             oContentType.update(false);
  •  
  •             clientContext.executeQueryAsync(FinalQuerySuccess, FinalQueryFailure);
  •         }
  •     }
  • }
  •  
  • function onfailed(sender, args) {
  •     console.log('Failed' + args.get_message() + '\n' + args.get_stackTrace());
  • }
  •  
  • function FinalQuerySuccess(sender, args) {
  •     console.log("success");
  • }
  •  
  • function FinalQueryFailure(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