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.
-
- var hostWebUrl = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
- var appWebUrl = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'));
- var context = SP.ClientContext.get_current();
- var finalSiteURL;
-
- $(document).ready(function() {
- CreateSite();
- });
-
- function CreateDeptSite() {
- var scriptBase = hostWebUrl + "/_layouts/15/";
- $.getScript(scriptBase + "sp.publishing.js");
- appCtxSite = new SP.AppContextSite(context, hostWebUrl);
- var siteMasterPageURL = "",
- systemMasterPageURL = "";
- var oWebsite = appCtxSite.get_web();
- context.load(oWebsite);
- var webCreationInfo = new SP.WebCreationInformation();
- finalSiteURL = hostWebUrl + '/' + siteURL;
- webCreationInfo.set_title(“SiteName”);
- webCreationInfo.set_description("Test Site Provisioning");
- webCreationInfo.set_language(1033);
- webCreationInfo.set_url(“siteURLName”);
- webCreationInfo.set_useSamePermissionsAsParentSite(false);
- webCreationInfo.set_webTemplate("STS#0");
- oWebsite.get_webs().add(webCreationInfo);
- oWebsite.update();
- context.load(oWebsite);
- context.executeQueryAsync(function() {
- console.log("Site has been created successfully");
-
- var siteMasterPageURL = oWebsite.get_customMasterUrl();
- var systemMasterPageURL = oWebsite.get_masterUrl();
- ActivateFeature(siteMasterPageURL, systemMasterPageURL);
- }, function(sender, args) {
- alert('site Creation failed : ' + args.get_message());
- console.log('site Creation failed : ' + args.get_message() + '\n' + args.get_stackTrace());
- });
- }
-
- function ActivateFeature(siteMasterPageURL, systemMasterPageURL) {
- var newAppCtxSite = new SP.AppContextSite(context, finalSiteURL);
- var collWeb = newAppCtxSite.get_web();
- context.load(collWeb);
- collWeb.get_features().add(new SP.Guid('94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb'), true, SP.FeatureDefinitionScope.none);
- collWeb.update();
- console.log("feature is activating and masterpage is applying...");
- context.executeQueryAsync(function() {
- console.log("Publishing feature activated & Masterpage applied successfully !!");
- }, function(sender, args) {
- alert('Feature Activation is failed : ' + args.get_message());
- console.log('Feature Activation is failed : ' + args.get_message() + '\n' + args.get_stackTrace());
- });
- }
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.
- collWeb.set_customMasterUrl(siteMasterPageURL);
- 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.