0

I write some codes to reach library with SOAP. I can reach the list items.But I cannot process data. Actually I can do it with REST. But I don't know how to do this. I need detailed code sample.

1 Answer 1

1

Retrieve list items using below:

 function getListItems(webUrl,listTitle, queryText) 
{
var viewXml = '<View><Query>' + queryText + '</Query></View>';
var url = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/getitems"; 
var queryPayload = {  
           'query' : {
                  '__metadata': { 'type': 'SP.CamlQuery' }, 
                  'ViewXml' : viewXml  
           }
};
return executeJson(url,"POST",null,queryPayload);
}


 function getListViewItems(webUrl,listTitle,viewTitle)
 {
 var url = webUrl + "/_api/web/lists/getByTitle('" + listTitle + "')/Views/getbytitle('" + viewTitle + "')/ViewQuery";
 return executeJson(url).then(
     function(data){         
         var viewQuery = data.d.ViewQuery;
         return getListItems(webUrl,listTitle,viewQuery); 
     });
  }

where

function executeJson(url,method,headers,payload) 
{
method = method || 'GET';
headers = headers || {};
headers["Accept"] = "application/json;odata=verbose";
if(method == "POST") {
    headers["X-RequestDigest"] = $("#__REQUESTDIGEST").val();
}      
var ajaxOptions = 
{       
   url: url,   
   type: method,  
   contentType: "application/json;odata=verbose",
   headers: headers
};
if (typeof payload != 'undefined') {
  ajaxOptions.data = JSON.stringify(payload);
}  
return $.ajax(ajaxOptions);
 }

Usage:

  getListViewItems(_spPageContextInfo.webAbsoluteUrl,'ListName','ViewName')
 .done(function(data)
 {
 var items = data.d.results;
 for(var i = 0; i < items.length;i++) {
     console.log(items[i].Title);//Process items
 }    
 })
  .fail(
   function(error){
     console.log(JSON.stringify(error));
    });
2
  • Forgive me for this basic question. Where can i write "mylistname" or GUID to fetch data ?
    – f_gulay
    Dec 5, 2017 at 10:46
  • here: getListViewItems(_spPageContextInfo.webAbsoluteUrl,'ListName','ViewName')
    – Deepmala
    Dec 5, 2017 at 10:48

Your Answer

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

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