Nderon Hyseni Nderon Hyseni - 1 year ago 45
Javascript Question

how to get a value from a Sharepoint list with javascript

i have the following code:

window.onload = function () {
var setTime = /*this should go the number*/
var fiveMinutes = 60 * setTime;
display = document.querySelector('#timer2');
startTimer(fiveMinutes, display);
};


what i want is
var setTime
to get the value or retrieve from list with column named
testval
the datatype for
testval
is number
thanks all!

Answer Source

To communicate with SharePoint you need to use the JavaScript client object model as follows:

 var ctx = new SP.ClientContext.get_current();
 var web = ctx.get_web();
 var listCollection = web.get_lists();
 var list = listCollection.getByTitle(NameOfYourList);
 var listItem = list.getItemById(TheItemID);
 context.load(listItem);

 context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));

and then

 function onQuerySucceeded() {
 var setTime =  listItem.get_item('testval');
 //continue your work
}

 function onQueryFailed(sender, args) {
 alert('failed');
}

Remember to reference SP.js script tag:

 <SharePoint:ScriptLink Name="SP.js" runat="server" OnDemand="true" Localizable="false" ></SharePoint:ScriptLink>