How to use the CSOM SP.Publishing.Variations.getPeerUrl method?
-
04-10-2020 - |
Question
In this code, I am trying to get the corressponding french URL variation. I am using this CSOM api
https://msdn.microsoft.com/en-us/library/office/jj954403.aspx
But I can't figure out how to use it. Does anyone know?
Thanks
<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(VariationHandler, "sp.js");
function VariationHandler() {
var ctx = SP.ClientContext.get_current();
var site = ctx.get_site();
var rootWeb = site.get_rootWeb();
var webProperties = rootWeb.get_allProperties();
ctx.load(site);
ctx.load(rootWeb);
ctx.load(webProperties);
ctx.executeQueryAsync(
function() {
var varLabelsListId = webProperties.get_item('_VarLabelsListId');
var labelsList = rootWeb.get_lists().getById(varLabelsListId);
var labelItems = labelsList.getItems(SP.CamlQuery.createAllItemsQuery());
ctx.load(labelItems);
ctx.executeQueryAsync(
function() {
var object = new SP.Publishing.Variations();
var qq = object.getPeerUrl(ctx);
alert(qq);
},
function() {
}
);
},
function() {
}
);
}
</script>
NEW
var ctx = SP.ClientContext.get_current();
var site = ctx.get_site();
var rootWeb = site.get_rootWeb();
var webProperties = rootWeb.get_allProperties();
ctx.load(site);
ctx.load(rootWeb);
ctx.load(webProperties);
ctx.executeQueryAsync(
function() {
var varLabelsListId = webProperties.get_item('_VarLabelsListId');
var labelsList = rootWeb.get_lists().getById(varLabelsListId);
var labelItems = labelsList.getItems(SP.CamlQuery.createAllItemsQuery());
ctx.load(labelItems);
ctx.executeQueryAsync(
function() {
var url = rootWeb.get_serverRelativeUrl();
var object = new SP.Publishing.Variations.getPeerUrl(url, "en-US");
},
function() {
}
);
},
function() {
}
);
NEW AGAIN
function VariationHandler2() {
var ctx = SP.ClientContext.get_current();
var site = ctx.get_site();
var rootWeb = site.get_rootWeb();
var webProperties = rootWeb.get_allProperties();
ctx.load(site);
ctx.load(rootWeb);
ctx.load(webProperties);
ctx.executeQueryAsync(
function() {
var varLabelsListId = webProperties.get_item('_VarLabelsListId');
var labelsList = rootWeb.get_lists().getById(varLabelsListId);
var labelItems = labelsList.getItems(SP.CamlQuery.createAllItemsQuery());
ctx.load(labelItems);
ctx.executeQueryAsync(
function() {
var url = rootWeb.get_serverRelativeUrl();
var object = SP.Publishing.Variations.getPeerUrl(ctx, url, "en");
alert(object.get_value());
},
function() {
}
);
},
function() {
}
);
}
NEW NEW NEW
$(document).ready(function() {
ExecuteOrDelayUntilScriptLoaded(VariationHandler, "sp.js");
});
function VariationHandler() {
ExecuteOrDelayUntilScriptLoaded(VariationHandler2, "SP.publishing.js");
}
function VariationHandler2() {
var ctx = SP.ClientContext.get_current();
var site = ctx.get_site();
var rootWeb = site.get_rootWeb();
var webProperties = rootWeb.get_allProperties();
ctx.load(site);
ctx.load(rootWeb);
ctx.load(webProperties);
ctx.executeQueryAsync(
function() {
var varLabelsListId = webProperties.get_item('_VarLabelsListId');
var labelsList = rootWeb.get_lists().getById(varLabelsListId);
var labelItems = labelsList.getItems(SP.CamlQuery.createAllItemsQuery());
ctx.load(labelItems);
ctx.executeQueryAsync(
function() {
var url = rootWeb.get_serverRelativeUrl();
var object = SP.Publishing.Variations.getPeerUrl(ctx, "/en/Pages/default.aspx", "fr");
ctx.executeQueryAsync(
function() {
console.log(object.get_value());
alert(object);
alert(object.get_value());
alert(JSON.stringify(object));
}, function() {
}
);
},
function() {
}
);
},
function() {
}
);
}
Solution
The parameter count for getPeerUrl() is wrong. According to the documentation (even if it is ambiguous) you must pass the current URL (server relative) and variation label too.
Check the JS function signature SP.Publishing.Variations.getPeerUrl, it takes 3 parameters.
Try this
ExecuteOrDelayUntilScriptLoaded(VariationHandler2, "SP.publishing.js");
function VariationHandler2() {
var ctx = SP.ClientContext.get_current();
var object = SP.Publishing.Variations.getPeerUrl(ctx, "/en/Pages/default.aspx", "fr");
ctx.executeQueryAsync(
function() {
alert(object.get_value());
}
);
}
Your problem is that you call the getPeerUrl method in the executeQueryAsync function. You must execute it before.
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange