0

In sharepoint 2013, I know I can get page or site variations using the getpeerurl method something like this

function GetOtherUrl(link, success, fail) {
    var other_culture = _spPageContextInfo.currentCultureName === "en-US" ? "fr-fr" : "en-us";
    var ctx = new SP.ClientContext(link);
    var object = SP.Publishing.Variations.getPeerUrl(ctx, link, other_culture);
    ctx.executeQueryAsync(Function.createDelegate(object, success), Function.createDelegate(object, fail));
}

however, for items on custom lists which also have variations enabled, how can I get the ID of the other language item? I can't assume it will be the same ID because if I manually create an item in the other language list and delete it, then that messes up the id consistency, however sharepoint still somehow tracks it.

Anyone know a way to find out the correct id?

Thanks

1 Answer 1

0

getPeerUrl() returns the Server-Relative URL of the requested Variation. This is good, because you can then turn around and call 'getFileByServerRelativeUrl()' to get a reference to the File Object. If you need to the metadata, you get use the File Object's get_listItemAllFields() method. Here is an example:

function getOtherUrl(link, success, fail) {
    var other_culture = _spPageContextInfo.currentCultureName === "en-US" ? "fr-fr" : "en-us";
    var ctx = new SP.ClientContext(link);
    var peerUrl = SP.Publishing.Variations.getPeerUrl(ctx, link, other_culture);

    ctx.executeQueryAsync(
      function () {
         success(peerUrl);
      }, 
      fail
   );
}

function getFileProperties(url,success,fail) {

   var ctx = SP.ClientContext.get_current(); 
   var relUrl = url.replace(_spPageContextInfo.siteAbsoluteUrl,'');  //convert to relative url
   var file = ctx.get_web().getFileByServerRelativeUrl(relUrl);   //get file
   ctx.load(file,'ListItemAllFields'); 

   ctx.executeQueryAsync(
      function () {
         success(file);
      }, 
      fail
   );
}

function errorHandler(sender,args) {
    console.log(args.get_message());
}

function doSomethingWithVariationItem(originalLink) {
    getOtherUrl(originalLink, 
        function(peerUrl) {
            getFileProperties(peerUrl, 
                function(file) {
                    var listItem = file.get_listItemAllFields();
                    console.log("The variation's Item ID is " + listItem.Id);
                    // do something with file or listItem or Id here 
                }
            )
        },
        errorHandler
    );

}
2
  • but how does this apply to a custom list item? Your code seems to be still for pages...
    – omega
    Feb 14, 2020 at 18:50
  • Sorry, I did not realize SharePoint supported Variations of list items. I have only ever used it for pages.
    – willman
    Feb 14, 2020 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.