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?


EDIT 2

I changed it to this now

'<view>
    <query>
        <where>
            <contains>
                <fieldref name="ObjectID" />
                <value type="Hyperlink or Picture">' + spapp.curPageUrl + '</value>
            </contains>
        </where>
    </query>
</view>'

'<view>
    <query>
        <where>
            <and>
                <and>
                    <contains>
                        <fieldref Name="GroupGuid" />
                        <value type="guid">' + groupId + '</value>
                    </contains>
                    <contains>
                        <fieldref name="ObjectID" />
                        <value type="Hyperlink or Picture">'+spapp.languageHome+'</value>
                    </contains>
                </and>
                <eq>
                    <fieldref name="Deleted" />
                    <value type="boolean">false</value>
                </eq>
            </and>
        </where>
    </query>
</view>'

Thanks

  • It seems the caml query tags were hidden on that page. Need to inspect the code to see it. – omega Jul 2 '15 at 14:42
0

Maybe you can simply take a look to the getPeerUrl() method in the SP.Publishing.Variations namespace: https://msdn.microsoft.com/en-us/library/office/jj954403.aspx

Try this

jQuery.getScript("_layouts/15/sp.publishing.js",null)
var clientContext = SP.ClientContext.get_current();
var peerUrl = SP.Publishing.Variations.getPeerUrl(clientContext, "/fr/Pages/MyPage.aspx","en")

clientContext.executeQueryAsync(function() {
    alert(peerUrl.get_value())}
);
  • How do I use this, I can't find any example code. – omega Jul 2 '15 at 17:36
  • 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
  • Is it the relative URL of the current aspx page you passed? According to your code, currently you pass the root web URL which is wrong. You can try with an hardcoded value like my example on any page to be sure. Check if the the label title is correct too. – Franck Cornu Jul 3 '15 at 15:35
  • Solution is here sharepoint.stackexchange.com/questions/148078/… – omega Jul 3 '15 at 16:21

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.