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> " + "<label id='qts'>" + qst + "</label>" + "</br>"
+ "<label>A: </label> " + "<input id='one' class='rdBtn' type='radio' name = '" + qNo+ "' value = 'A'/> " + optionA + " </br>"
+ "<label>B: </label> " + "<input id='two' class='rdBtn' type='radio' name = '" + qNo+ "' value = 'B'/> " + optionB + "</br>"
+ "<label>C: </label> " + "<input id='three' class='rdBtn' type='radio' name = '" + qNo+ "' value = 'C'/> " + optionC + "</br>"
+ "<label>D: </label> " + "<input id='four' class='rdBtn' type='radio' name = '" + qNo+ "' value = 'D'/> " + 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();
retriveListItem();
this line from end of code and try again.ExecuteOrDelayUntilScriptLoaded(retriveListItem, "sp.js");
will call your function when SP.js is loaded