0

I am using the SharePoint REST API to do a search. I am pulling back the JSON results and reading them as follows:

HttpWebRequest endpointRequest = (HttpWebRequest)HttpWebRequest.Create(querystring);

endpointRequest.Method = "GET";
endpointRequest.Accept = "application/json; odata=verbose";
endpointRequest.UseDefaultCredentials = true;
HttpWebResponse endpointResponse =(HttpWebResponse)endpointRequest.GetResponse();

Stream webStream = endpointResponse.GetResponseStream();
StreamReader responseReader = new StreamReader(webStream);
var results = responseReader.ReadToEnd();

This works fine, I can see the results in JSON format. So I created a class from the JSON in VS 2017 and here is the classes created from the JSON (this was done automatically with File=>Paste Special=>Paste JSON As Classes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class SharePointRESTResults
    {
        public class Rootobject
        {
            public D d { get; set; }
        }

        public class D
        {
            public Query query { get; set; }
        }

        public class Query
        {
            public __Metadata __metadata { get; set; }
            public int ElapsedTime { get; set; }
            public Primaryqueryresult PrimaryQueryResult { get; set; }
            public Properties1 Properties { get; set; }
            public Secondaryqueryresults SecondaryQueryResults { get; set; }
            public string SpellingSuggestion { get; set; }
            public Triggeredrules TriggeredRules { get; set; }
        }

        public class __Metadata
        {
            public string type { get; set; }
        }

        public class Primaryqueryresult
        {
            public __Metadata1 __metadata { get; set; }
            public Customresults CustomResults { get; set; }
            public string QueryId { get; set; }
            public string QueryRuleId { get; set; }
            public object RefinementResults { get; set; }
            public Relevantresults RelevantResults { get; set; }
            public object SpecialTermResults { get; set; }
        }

        public class __Metadata1
        {
            public string type { get; set; }
        }

        public class Customresults
        {
            public __Metadata2 __metadata { get; set; }
            public object[] results { get; set; }
        }

        public class __Metadata2
        {
            public string type { get; set; }
        }

        public class Relevantresults
        {
            public __Metadata3 __metadata { get; set; }
            public object GroupTemplateId { get; set; }
            public object ItemTemplateId { get; set; }
            public Properties Properties { get; set; }
            public object ResultTitle { get; set; }
            public object ResultTitleUrl { get; set; }
            public int RowCount { get; set; }
            public Table Table { get; set; }
            public int TotalRows { get; set; }
            public int TotalRowsIncludingDuplicates { get; set; }
        }

        public class __Metadata3
        {
            public string type { get; set; }
        }

        public class Properties
        {
            public Result[] results { get; set; }
        }

        public class Result
        {
            public __Metadata4 __metadata { get; set; }
            public string Key { get; set; }
            public string Value { get; set; }
            public string ValueType { get; set; }
        }

        public class __Metadata4
        {
            public string type { get; set; }
        }

        public class Table
        {
            public __Metadata5 __metadata { get; set; }
            public Rows Rows { get; set; }
        }

        public class __Metadata5
        {
            public string type { get; set; }
        }

        public class Rows
        {
            public Result1[] results { get; set; }
        }

        public class Result1
        {
            public __Metadata6 __metadata { get; set; }
            public Cells Cells { get; set; }
        }

        public class __Metadata6
        {
            public string type { get; set; }
        }

        public class Cells
        {
            public Result2[] results { get; set; }
        }

        public class Result2
        {
            public __Metadata7 __metadata { get; set; }
            public string Key { get; set; }
            public object Value { get; set; }
            public string ValueType { get; set; }
        }

        public class __Metadata7
        {
            public string type { get; set; }
        }

        public class Properties1
        {
            public Result3[] results { get; set; }
        }

        public class Result3
        {
            public __Metadata8 __metadata { get; set; }
            public string Key { get; set; }
            public string Value { get; set; }
            public string ValueType { get; set; }
        }

        public class __Metadata8
        {
            public string type { get; set; }
        }

        public class Secondaryqueryresults
        {
            public __Metadata9 __metadata { get; set; }
            public object[] results { get; set; }
        }

        public class __Metadata9
        {
            public string type { get; set; }
        }

        public class Triggeredrules
        {
            public __Metadata10 __metadata { get; set; }
            public object[] results { get; set; }
        }

        public class __Metadata10
        {
            public string type { get; set; }
        }

    }
}

So I now am trying to deserialize the results as follows:

 SharePointRESTResults resultX = JsonConvert.DeserializeObject<SharePointRESTResults>(results());

It appears to be serializing, but I am having problems pulling the class properties out. I have tried:

var y = resultX.[d].[query].[Primaryqueryresult].[Relevantresults].[Table].[Rows].[results].[0].[Cells].[results].[0].[Key];

to try to get the results out but I get "The name 'd' does not exist in the current context" Has anyone tried to do this? Any help is much appreciated.

0

Here is an sample example to take a key value in your response.

let responseData:any[]=[];
let filteredArrayList = [];
responseData = result.PrimaryQueryResult.RelevantResults.Table.Rows;
console.log("responseData", responseData);

for (let i = 0; i < responseData.length; i++) {
     let PreferredName = [];
     let WorkEmail = [];

    //here iam geting two keys using key name.
    PreferredName = responseData[i].Cells.filter(response => response.Key === "PreferredName");

    WorkEmail = responseData[i].Cells.filter(response => response.Key === "WorkEmail");


    //here iam created sample object has been contain both value
    let object = { PreferredName: PreferredName[0].Value, WorkEmail: WorkEmail[0].Value }

     filteredArrayList.push(object);  
 }
//here you got All object value as key and value.
console.log("filteredArrayList value ",filteredArrayList);

Tested working fine. if any error facing please let me know.

if solved your problem please upvote and mark as answer.

Thanks, Muthukumar

  • It appears this is JavaScript. I am programming in c#. So although this answer does work for JavaScript, it is not c# specific. That may be my fault for not indicating clearly in my code. I ended up just deserializing to a Json object and using Linq to pull out the data I needed. Thanks. – john Jun 15 '18 at 17:44
  • the rest of this looks good but I am getting errors from that first line: let responseData:any[]=[]; Thank you - Jake – Jake Oct 30 '18 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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