Monday 22 December 2014

SharePoint 2013: Modify Navigation Settings with JavaScript CSOM

One of the new additions in the JavaScript Client Object Model in SharePoint 2013 is the Publishing Navigation API. Some of the other additions are theUserProfile API and the Taxonomy API which I have already posted about.

In this post, let us look at how to use the Navigation API to change the navigation settings in a SharePoint 2013 site.

The main reason I want to do these posts is that currently there is still no API Reference available on MSDN for them. So this might be helpful to people looking to achieve the same functionality.

Here are the things which you can change from this Navigation API: 

1) You can change the Navigation settings of Publishing Sites as well as Non-Publishing sites.

2) You can set the Navigation Provider of the Current Navigation (Quick Launch) as well as the Global Navigation (Top Navigation Bar)

3) You can set the Navigation Provider out of any of these three:
     a. Inherit from Parent Site
     b. Managed Navigation (Taxonomy) The navigation items will be represented using a Managed Metadata term set.
     c. Structural Navigation: Display the navigation items below the current site.

4) Since all this code is pure JavaScript, you can add it in a variety of places like Apps, WebParts, Pages, Page Layouts, Web Templates etc.

So without much further ado, here is the code to manipulate the Navigation Settings:

First, lets make sure that we have loaded all the required SharePoint JavaScript files on the page. I like to use the jQuery.getScript() for this. The scripts we will need are sp.runtime.js,  sp.js and sp.publishing.js.
Here is a simple way by which I load all the necessary files:

12345678910111213
$(document).ready(function(){
var scriptBase = _spPageContextInfo.webAbsoluteUrl + "/_layouts/15/";
$.getScript(scriptBase + "sp.runtime.js",function(){
$.getScript(scriptBase + "sp.js",function(){
$.getScript(scriptBase + "sp.publishing.js", modifyNavigation);
});
});
});
view rawsp15_nav_scripts.js hosted with ❤ by GitHub


Now, the actual code:

1) Set the (Current/Global) Navigation to either "Inherit from Parent" or "Structural Navigation":


1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
(function($){
$(document).ready(function(){
var scriptBase = _spPageContextInfo.webAbsoluteUrl + "/_layouts/15/";
$.getScript(scriptBase + "sp.runtime.js",function(){
$.getScript(scriptBase + "sp.js",function(){
$.getScript(scriptBase + "sp.publishing.js", modifyNavigation);
});
});
});
function modifyNavigation(){
//Current Context
var context = SP.ClientContext.get_current();
//Current Web
var currentWeb = context.get_web();
//Navigation Provider Settings for Current Web.
var webNavSettings = new SP.Publishing.Navigation.WebNavigationSettings(context, currentWeb);
//Current Navigation (Quick Launch)
var navigation = webNavSettings.get_currentNavigation();
//Global Navigation (Top Navigation Bar)
//var navigation = webNavSettings.get_globalNavigation();
 
/*
unknown: 0,
portalProvider: 1 (Structural Navigation),
taxonomyProvider: 2 (You will need the Term Store Id and Term Set Id too),
inheritFromParentWeb: 3 */
navigation.set_source(1);
webNavSettings.update();
context.executeQueryAsync(function(){
alert("Navigation Modification Successful");
},function(sender,args){
alert(args.get_message());
});
 
}
 
})(jQuery);
view rawsp15_nav.js hosted with ❤ by GitHub


2) Set the (Current/Global) Navigation dependent upon a Term Set in the Term Store:

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
(function($){
$(document).ready(function(){
var scriptBase = _spPageContextInfo.webAbsoluteUrl + "/_layouts/15/";
$.getScript(scriptBase + "sp.runtime.js",function(){
$.getScript(scriptBase + "sp.js",function(){
$.getScript(scriptBase + "sp.publishing.js", modifyNavigation);
});
});
});
function modifyNavigation(){
//Current Context
var context = SP.ClientContext.get_current();
//Current Web
var currentWeb = context.get_web();
//Navigation Provider Settings for Current Web.
var webNavSettings = new SP.Publishing.Navigation.WebNavigationSettings(context, currentWeb);
//Current Navigation (Quick Launch)
var navigation = webNavSettings.get_currentNavigation();
//Global Navigation (Top Navigation Bar)
//var navigation = webNavSettings.get_globalNavigation();
//Term Store GUID (without hyphens '-')
navigation.set_termStoreId("ec8dfb7bcea148a7836f455887ed31db");
//Term Set GUID (without hyphens '-')
navigation.set_termSetId("93e0d69f4181408893bbb5b1bd221c03");
/*
unknown: 0,
portalProvider: 1 (Structural Navigation),
taxonomyProvider: 2 (You will need the Term Store Id and Term Set Id too),
inheritFromParentWeb: 3 */
navigation.set_source(2);
webNavSettings.update();
context.executeQueryAsync(function(){
alert("Navigation Modification Successful");
},function(sender,args){
alert(args.get_message());
});
}
})(jQuery);

No comments:

Post a Comment