SharePoint 2013 - Get Variation URL using JSOM
From the variation information on this blog,
http://blogs.technet.com/b/stefan_gossner/archive/2011/11/21/sharepoint-variations-the-complete-guide-part-6-configuration-internals.aspx, I used the following code to get the corresponding variation URL to create the toggling button for a publishing site.
Type.registerNamespace('spapp');
var context;
spapp.url = "https://rchen.sp.com";
spapp.language;
spapp.languageHome;
spapp.curPageUrl;
spapp.relationshipList;
spapp.relationshipItems;
$(document).ready(function () {
var toggleHtml = 'Toggle';
$('#RibbonContainer-TabRowRight').prepend(toggleHtml);
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', spapp.init);
});
spapp.init = function(){
context = new SP.ClientContext(spapp.url);
};
spapp.toggleLanguage = function(){
spapp.curPageUrl = _spPageContextInfo.serverRequestPath;
spapp.language = _spPageContextInfo.currentCultureName;
spapp.relationshipList = context.get_web().get_lists().getByTitle("Relationships List");
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(
'
spapp.relationshipItems = spapp.relationshipList.getItems(camlQuery);
context.load(spapp.relationshipItems);
context.executeQueryAsync(
Function.createDelegate(this, spapp.onGetGroupId),
Function.createDelegate(this, spapp.onQueryFailed)
);
};
spapp.onGetGroupId = function (sender, args) {
var listItemInfo = '';
var listItemEnumerator = spapp.relationshipItems.getEnumerator();
spapp.languageHome = "/fr";
if (spapp.language.toLowerCase().indexOf("fr-") > -1){
spapp.languageHome = "/en";
}
if (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
var groupId = oListItem.get_item('GroupGuid');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('
spapp.relationshipItems = spapp.relationshipList.getItems(camlQuery);
context.load(spapp.relationshipItems);
context.executeQueryAsync(
Function.createDelegate(this, spapp.onGetVariationUrl),
Function.createDelegate(this, spapp.onQueryFailed)
);
}
else{
document.location = spapp.languageHome;
}
};
spapp.onGetVariationUrl = function (sender, args) {
var listItemInfo = '';
var listItemEnumerator = spapp.relationshipItems.getEnumerator();
if (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
var objectID = oListItem.get_item('ObjectID');
document.location = objectID.get_url(); //redirect to the variation.
}
else{
document.location = spapp.languageHome;
}
}
spapp.onQueryFailed = function (sender, args) {
alert('Request failed. ' + args.get_message() +
'\n' + args.get_stackTrace());
};
Comments