Search code examples
javascriptsharepointodata

Parsing result from async Sharepoint list query in javascript


I'm using Javascript to summarise data from a sharepoint list. I've got the query working and returning OK, and i can see from the console that it is returning everything I need, however I cannot work out how to parse it.

jQuery.fromJSON returns an error, as does the significant number of other things i've tried. Other than doing some regex or something i'm at a loss.

Can anyone suggest how this type of sharepoint response can be parsed so i can use the objects/data in each row.

Thanks

function GetProductInformation(){
                var query = '<View><Query><Where> <And><Geq><FieldRef Name="Data_x0020_Due_x0020_Date"/><Value IncludeTimeValue="FALSE" Type="DateTime">2017-07-01</Value></Geq><Leq><FieldRef Name="Data_x0020_Due_x0020_Date"/><Value IncludeTimeValue="FALSE" Type="DateTime">2018-06-29</Value></Leq></And> </Where><GroupBy Collapse="TRUE"><FieldRef Name="Analyser"/></GroupBy></Query><ViewFields><FieldRef Name="Analyser"/><FieldRef Name="Number_x0020_of_x0020_products"/></ViewFields><Aggregations Value="On"><FieldRef Name="Number_x0020_of_x0020_products" Type ="SUM"/></Aggregations></View>';


                console.log("Query is " + query);   

    var clientContext = new SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('Work tracker - Products');

    console.log("GetProductInformation Async Request") 

    jsonResult = oList.renderListData(query);

    clientContext.executeQueryAsync(Function.createDelegate(this, this.onGetProductQuerySucceeded), Function.createDelegate(this, this.onGetProductQueryFailed));        
                //clientContext.executeQueryAsync(onGetProductQuerySucceeded, onGetProductQueryFailed);
                console.log("GetProductInformation Async Requested")     
}


function onGetProductQuerySucceeded(sender, args) {
                console.log('Query Success');
                var dataFromYou = jsonResult
                console.log(dataFromYou);

                var data = dataFromYou.Row[0]; //This bit doesn't work
                console.log(data)

Find below some example output:

"{ "Row" : 
[{
"Analyser": [{"lookupId":1,"lookupValue":"Accounts Payable 
Analyser","isSecretFieldValue":false}],
"Analyser.urlencoded": "%3B%23Accounts%20Payable%20Analyser%3B%23",
"Analyser.COUNT.group": "363",
"Analyser.newgroup": "1",
"Analyser.groupindex": "1_",
"Number_x0020_of_x0020_products.SUM": "14,694",
"Number_x0020_of_x0020_products.SUM.agg": "658"
}
,{
"Analyser": [{"lookupId":2,"lookupValue":"Accounts Receivable 
Analyser","isSecretFieldValue":false}],
"Analyser.urlencoded": "%3B%23Accounts%20Receivable%20Analyser%3B%23",

Solution

  • In the example:

    var result = list.renderListData(qry.get_viewXml());
    

    SP.List.renderListData method returns SP.Result object and the list data could be retrieved like this:

    var listData = result.get_value();
    

    Since the list data is returned as a JSON string it could be easily parsed like this:

    var jsonVal = JSON.parse(result.get_value());
    if(jsonVal.Row.length > 0)
        console.log(jsonVal.Row[0]);   //get first row data
    

    Example

    var ctx = SP.ClientContext.get_current();
    var list = ctx.get_web().get_lists().getByTitle(listTitle);
    var qry = SP.CamlQuery.createAllItemsQuery();
    var result = list.renderListData(qry.get_viewXml());
    
    ctx.executeQueryAsync(
       function(){
           var jsonVal = JSON.parse(result.get_value());
           if(jsonVal.Row.length > 0)
               console.log(jsonVal.Row[0]);
       },
       function(sender,args){
          console.log(args.get_message()); 
       });