0

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() {
        }
    );  
}
1

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.

  • I added my new code above, but it says .Variations result is undefined. I checked developer tools, and Sp.Publishing does not contain a value for key Variations. – omega Jul 2 '15 at 19:11
  • Is the script SP.publishing.js correctly loaded? – Franck Cornu Jul 2 '15 at 20:28
  • ok so I didnt do that, but now I did, and I got the object from getPeerUrl(), but now how do I extract the url from it? I put my new code above. – omega Jul 2 '15 at 20:44
  • the new code alerts 0. – omega Jul 2 '15 at 20:57
  • I tried the new code, but I get a SP.Result object and if I call .get_value() on it, I get 0 back... – omega Jul 3 '15 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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