0

When trying to implement the following code in SharePoint 2016, I get the following error:

Unable to get property 'webAbsoluteUrl' of undefined or null reference.

The developer tool points to this line var absoluteURL = _spPageContextInfo.webAbsoluteUrl;


        function getData(lName){
        var absoluteURL = _spPageContextInfo.webAbsoluteUrl;
        if(lName == "Navigation"){
        console.log(_spPageContextInfo.webAbsoluteUrl);
            var endPointUrl = absoluteURL + "/_api/web/lists/getbyTitle('"+lName+"')/items";
        }else{
        console.log(_spPageContextInfo.webAbsoluteUrl);
            var endPointUrl = absoluteURL + "/_api/web/lists/getbyTitle('"+lName+"')/items?$select=parentNav/URL, parentNav/URLNAME,subLink&$expand=parentNav";
            //console.log(endPointUrl);
        }
Ganesh Sanap - MVP
  • 44,918
  • 21
  • 30
  • 61
OLA
  • 231
  • 4
  • 15

1 Answers1

0

Try to call _spPageContextInfo in ExecuteOrDelayUntilScriptLoaded like this:

 ExecuteOrDelayUntilScriptLoaded(Ready, "sp.js");
    function Ready()
    {
       var lNmae="Navigation";
       getData(lName);

    }
    function getData(lName){
        var absoluteURL = _spPageContextInfo.webAbsoluteUrl;
        if(lName == "Navigation"){
        console.log(_spPageContextInfo.webAbsoluteUrl);
            var endPointUrl = absoluteURL + "/_api/web/lists/getbyTitle('"+lName+"')/items";
        }else{
        console.log(_spPageContextInfo.webAbsoluteUrl);
            var endPointUrl = absoluteURL + "/_api/web/lists/getbyTitle('"+lName+"')/items?$select=parentNav/URL, parentNav/URLNAME,subLink&$expand=parentNav";
            //console.log(endPointUrl);
        }
    }
Jerry
  • 2,583
  • 1
  • 11
  • 11
  • Apologies for the late response. Please see my full code which uses jquery document.ready(). https://codepen.io/isogunro/pen/mdyPZdy?editors=0010 Not sure how to implement it here. – OLA Dec 11 '19 at 18:12