Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

How to get Sharepoint 2013 variation URL?

$
0
0
I want to create a toggle button for variations, so that when I am on the `en` or `fr` page, and I click the button it takes me to the other page. But I can't just change the `en` to `fr`, for example, because someone can rename the page. So I need to write code that can get the correct URL. I found this article

http://rwcchen.blogspot.ca/2015/02/sharepoint-2013-get-variation-url-using.html

which says it can do it using the relationships list, but I am having trouble running the script. It always fails with error `Cannot complete this action. Please try again.`. I know the list exists because if I visit

    http://my-page/Relationships%20List/AllItems.aspx



Then I can see the list there with items. This is my code, does anyone know what could be the problem?

Thanks
    $(document).ready(function() {        ExecuteOrDelayUntilScriptLoaded(VariationHandler, "sp.js");    });        function VariationHandler() {            Type.registerNamespace('spapp');                var context;        spapp.url = "http://my-page/";        spapp.language;        spapp.languageHome;        spapp.curPageUrl;        spapp.relationshipList;        spapp.relationshipItems;                context = new SP.ClientContext(spapp.url);            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.curPageUrl + '');        spapp.relationshipItems = spapp.relationshipList.getItems(camlQuery);            context.load(spapp.relationshipItems);        context.executeQueryAsync(            onGetGroupId,            onQueryFailed        );    }                        function onGetGroupId(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('' + groupId + '' + spapp.languageHome + 'false');            spapp.relationshipItems = spapp.relationshipList.getItems(camlQuery);            context.load(spapp.relationshipItems);            context.executeQueryAsync(                Function.createDelegate(this, onGetVariationUrl),                Function.createDelegate(this, onQueryFailed)            );        } else {            document.location = spapp.languageHome;        }    }        function onGetVariationUrl(sender, args) {        var listItemInfo = '';        var listItemEnumerator = spapp.relationshipItems.getEnumerator();            if (listItemEnumerator.moveNext()) {            var oListItem = listItemEnumerator.get_current();            var objectID = oListItem.get_item('ObjectID');                var a = objectID.get_url(); //redirect to the variation.        } else {            var a = spapp.languageHome;        }                console.log(a);        //document.location = a;    }        function onQueryFailed (sender, args) {        alert('Request failed. ' + args.get_message() +            '\n' + args.get_stackTrace());    }


----

**EDIT**

I found the two CAML queries from the article

    '<view><query><where><contains><fieldref objectid="" name="\"><value type="\">'+spapp.curPageUrl+'</value></fieldref></contains></where></query></view>'


    
 
   '<view><query><where><and><and><contains><fieldref name="\\" groupguid=""><value type="\\" guid="">'+groupId+'</value></fieldref></contains><contains><fieldref
 objectid="" name="\\"><value type="\\">'+spapp.languageHome+'</value></fieldref></contains></and><eq><fieldref deleted="" name="\\"><value boolean=""
 type="\\">false</value></fieldref></eq></and></where></query></view>'



But they are messed up. Does anyone know how to fix this?

Thanks

Viewing all articles
Browse latest Browse all 7589

Trending Articles