Activate The SharePoint Site Features Using SharePoint Hosted App

In SharePoint, we can have the option to activate the feature, using SharePoint manage site feature. At the time of site provisioning, we need to activate the features, using Code.

Use the code given below to activate the one of the manage site features, using SharePoint hosted app with JSOM.

  1. //declare variables:  
  2. var hostWebUrl = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));  
  3. var appWebUrl = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'));  
  4. var context = SP.ClientContext.get_current();  
  5. var finalSiteURL;  
  6. // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model  
  7. $(document).ready(function() {  
  8.     CreateSite();  
  9. });  
  10.   
  11. function CreateDeptSite() {  
  12.     var scriptBase = hostWebUrl + "/_layouts/15/";  
  13.     $.getScript(scriptBase + "sp.publishing.js");  
  14.     appCtxSite = new SP.AppContextSite(context, hostWebUrl);  
  15.     var siteMasterPageURL = "",  
  16.         systemMasterPageURL = "";  
  17.     var oWebsite = appCtxSite.get_web();  
  18.     context.load(oWebsite);  
  19.     var webCreationInfo = new SP.WebCreationInformation();  
  20.     finalSiteURL = hostWebUrl + '/' + siteURL;  
  21.     webCreationInfo.set_title(“SiteName”);  
  22.     webCreationInfo.set_description("Test Site Provisioning");  
  23.     webCreationInfo.set_language(1033);  
  24.     webCreationInfo.set_url(“siteURLName”);  
  25.     webCreationInfo.set_useSamePermissionsAsParentSite(false);  
  26.     webCreationInfo.set_webTemplate("STS#0");  
  27.     oWebsite.get_webs().add(webCreationInfo);  
  28.     oWebsite.update();  
  29.     context.load(oWebsite);  
  30.     context.executeQueryAsync(function() {  
  31.         console.log("Site has been created successfully");  
  32.         //Getting Master page URL from top level site  
  33.         var siteMasterPageURL = oWebsite.get_customMasterUrl();  
  34.         var systemMasterPageURL = oWebsite.get_masterUrl();  
  35.         ActivateFeature(siteMasterPageURL, systemMasterPageURL);  
  36.     }, function(sender, args) {  
  37.         alert('site Creation failed : ' + args.get_message());  
  38.         console.log('site Creation failed : ' + args.get_message() + '\n' + args.get_stackTrace());  
  39.     });  
  40. }  
  41. //Activate the SharePoint publishing feature  
  42. function ActivateFeature(siteMasterPageURL, systemMasterPageURL) {  
  43.     var newAppCtxSite = new SP.AppContextSite(context, finalSiteURL);  
  44.     var collWeb = newAppCtxSite.get_web();  
  45.     context.load(collWeb);  
  46.     collWeb.get_features().add(new SP.Guid('94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb'), true, SP.FeatureDefinitionScope.none);  
  47.     collWeb.update();  
  48.     console.log("feature is activating and masterpage is applying...");  
  49.     context.executeQueryAsync(function() {  
  50.         console.log("Publishing feature activated & Masterpage applied successfully !!");  
  51.     }, function(sender, args) {  
  52.         alert('Feature Activation is failed : ' + args.get_message());  
  53.         console.log('Feature Activation is failed : ' + args.get_message() + '\n' + args.get_stackTrace());  
  54.     });  
  55. }  

On the page, load site will be created and the feature will be activated on the site.

The code given below is used to set the Custom master page after creating the site. 

  1. collWeb.set_customMasterUrl(siteMasterPageURL);  
  2. collWeb.set_masterUrl(systemMasterPageURL);   

Site Master Page (set_customMasterUrl)

The site master page will be used by all the publishing pages - the pages that the visitors to your Website will see. You can have a different master page for each Device Channel. If you don't see the master page you're looking for; go to the Master Page Gallery in Site Settings and make sure that it has an approved version.

You may inherit these settings from the parent site or select unique settings for this site only.

System Master Page (set_masterURL)

The system master page will be used by the administrative pages, lists and document library views on this site. If the desired master page does not appear, go to the Master Page Gallery in Site Settings and make sure that the master page has an approved version.

You may inherit these settings from the parent site or select unique settings for this site only.

Summary

In this blog, we explored how to activate SharePoint features, using SharePoint hosted app with JavaScript Object Model.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now