0

Hi I am trying to retrieve list items using JSOM. But, the problem occurring is that SP.js is not loading correctly. It shoul load before the function gets executed. I am confused about where to use ExecuteOrDelayUntilScriptLoaded and SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function). I am not getting where to use these functions in my javascript code. Please help me out to resolve this issue. Getting some errors as well: 1."https://*****.sharepoint.com/sites/NewWorkSite/_vti_bin/client.svc/ProcessQuery 403" 2."Uncaught TypeError: Cannot read property 'requestUnexpectedResponseWithStatus' of undefined"

$(document).ready(function() 
{ ExecuteOrDelayUntilScriptLoaded(retriveListItem, "sp.js"); 
});


function retriveListItem()  
{      
    var siteUrl = '/sites/NewWorkSite';
    var context = new SP.ClientContext(siteUrl);  
    var list = context.get_web().get_lists().getByTitle('Questions1List');  
    var caml = new SP.CamlQuery();  
    caml.set_viewXml("<View><Query><OrderBy><FieldRef Name='QuestionNumber' Ascending='TRUE' /></OrderBy></Query></View>");  
    returnedItems = list.getItems(caml);  
    context.load(returnedItems);  
    context.executeQueryAsync(onSucceededCallback, onFailedCallback);  
}  

function onSucceededCallback(sender, args)  
{  
    var enumerator = returnedItems.getEnumerator();  
    //Formulate HTML from the list items   
    var MainResult = 'Items in the Divisions list: <br><br>';  
    //Loop through all the items   
    while (enumerator.moveNext())  
    {  


        var listItem = enumerator.get_current();  
        var qNo= listItem.get_item("QuestionNumber");   
        var qst = listItem.get_item("Question");   
        var optionA = listItem.get_item("OptionA");         
        var optionB = listItem.get_item("OptionB");
        var optionC = listItem.get_item("OptionC");
        var optionD = listItem.get_item("OptionD");



                $('#qDiv').append("<label>" + qNo+ "." + "</label>&nbsp" + "<label id='qts'>" + qst + "</label>" + "</br>" 
                + "<label>A: </label>&nbsp" + "<input id='one' class='rdBtn' type='radio' name = '" + qNo+ "' value = 'A'/>&nbsp" + optionA  + "&nbsp</br>"
                + "<label>B: </label>&nbsp" + "<input id='two' class='rdBtn' type='radio' name = '" + qNo+ "' value = 'B'/>&nbsp" + optionB + "</br>"
                + "<label>C: </label>&nbsp" + "<input id='three' class='rdBtn' type='radio' name = '" + qNo+ "' value = 'C'/>&nbsp" + optionC + "</br>"
                + "<label>D: </label>&nbsp" + "<input id='four' class='rdBtn' type='radio' name = '" + qNo+ "' value = 'D'/>&nbsp" + optionD + "</br>" + "</br>");



    }  


}  
        //This function fires when the query fails   
function onFailedCallback(sender, args)  
{  
        //Formulate HTML to display details of the error   
        var markup = '<p>The request failed: <br>';  
        markup1 = 'Message: ' + args.get_message() + '<br>';  
        //Display the details   
        alert(markup1);
} 

retriveListItem();
12
  • remove retriveListItem(); this line from end of code and try again. Apr 8, 2019 at 4:21
  • When I remove that line, errors get vanished. But, this function needs to be called for it's execution. Apr 8, 2019 at 4:23
  • ExecuteOrDelayUntilScriptLoaded(retriveListItem, "sp.js"); will call your function when SP.js is loaded Apr 8, 2019 at 4:25
  • OK Let me check. Apr 8, 2019 at 4:25
  • I am getting 2 errors as I mentioned in my question description. Apr 8, 2019 at 4:27

1 Answer 1

1
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
    SP.SOD.registerSod("Your javascript file name i.e xye.js", "Your js file path i.e /Style Library/xyz.js");
    SP.SOD.executeFunc("Your javascript file name i.e xye.js", null, null);
    ExecuteOrDelayUntilScriptLoaded(function () {
        //Your function to execute
       //In your case  
        retriveListItem();
    },"Your javascript file name i.e xye.js");
    SP.SOD.notifyEventAndExecuteWaitingJobs("Your javascript file name i.e xye.js");
});
5
  • What to put in place of "null, null"? Apr 8, 2019 at 4:36
  • as it is "null" Apr 8, 2019 at 4:37
  • Ok. I try it and let you know what happens. Apr 8, 2019 at 4:42
  • Actually, I was using the file reference path in a wrong way. I was using <script src="/_layouts/.....js"></script> . I used <script src="/_layouts/15/***.js"></script> That fixed the issue. Apr 8, 2019 at 4:50
  • Thank you Bhavik for your answer. Apr 8, 2019 at 4:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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