Nderon Hyseni Nderon Hyseni - 1 year ago 87
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
the datatype for
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.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) {

Remember to reference SP.js script tag:

 <SharePoint:ScriptLink Name="SP.js" runat="server" OnDemand="true" Localizable="false" ></SharePoint:ScriptLink>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download