1
_spBodyOnLoadFunctionNames.push("FetchURL");
                function FetchURL()
                {
                    // Custom JavaScript methods
                   var ctx = new SP.ClientContext();
        var site = ctx.get_site();
        ctx.load(site);
        ctx.executeQueryAsync(function(s, a){ currURL=SP.Utilities.Utility.getLayoutsPageUrl("");
                                            });
                    }

The above method returns the URL, however, sometimes it returns undefined, which causes errors. How to tackle this?

Code is placed in head of the masterpage... in scripts tag.

8
  • do you need to call get_site for anything? otherwise there is no need to load it with ctx.load; you can call the SP.Utilities.Utility.getLayoutsPageUrl synchronously, without calling executeQueryAsync
    – MdMazzotti
    Apr 4, 2014 at 10:18
  • What returns undefined? The function FeetchURL has no return and will then always return underfined. The global variable currURL will be set asynchronously, as ctx.executeQueryAsync is asynchronous, which means in you code you can't know when it is set
    – eirikb
    Apr 4, 2014 at 10:18
  • @MdMazzotti alert(SP.Utilities.Utility.getLayoutsPageUrl()); doesnot work. can you give me a solution, eirikb.
    – variable
    Apr 4, 2014 at 10:21
  • @variable it's a function, you must call it like SP.Utilities.Utility.getLayoutsPageUrl()
    – MdMazzotti
    Apr 4, 2014 at 10:23
  • If i put the SP.Utilities.Utility.getLayoutsPageUrl in the spBodyOnLoadFunctionNames() then it works, why is this happening? Im confused
    – variable
    Apr 4, 2014 at 10:23

1 Answer 1

3

The SP.Utilities.Utility.getLayoutsPageUrl is contained within the sp.js file. You have to make sure that sp.js has been loaded, before calling the function.

SP.SOD.executeFunc('sp.js', 'SP.Utilities.Utility.getLayoutsPageUrl', getCurrentUrl);

function getCurrentUrl(){
  currentURL = SP.Utilities.Utility.getLayoutsPageUrl(""); 
}
1

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.