How do I parse the response of ReadItemsRequestWithSitecoreQuery?

65 views Asked by At

I have the following code snippet. A slightly modified version of the sample code provided with the Xamarin Sitecore Extension.

 var requesttwo = ItemWebApiRequestBuilder.ReadItemsRequestWithSitecoreQuery("select * from /sitecore/content/Home//*[@@TemplateName='Article']")
 .AddFieldsToRead(new string[3] { "Title", "Author", "Content" })
 .AddScope(ScopeType.Self)
 .Build();

 ScItemsResponse response = null;
 try
 {
     // And execute it on a session asynchronously
     response = await session.ReadItemAsync(requesttwo);
 }
 catch (Exception e)
 {
     String error = e.Message;
 }

It works as expected when I replace

ReadItemsRequestWithSitecoreQuery("select * from /sitecore/content/Home//*[@@TemplateName='Article']")

with

ReadItemsRequestWithPath("/sitecore/content/home")

but the above block throws "[Sitecore Mobile SDK] Data from the internet has unexpected format." I found this question and I do recognize that sitecore queries use "@" but I added the below snippet as it seems to suggest and I get the same response.

string format = HttpUtility.HtmlEncode("select * from /sitecore/content/Home//*[@@TemplateName='Article']");
1

There are 1 answers

0
Reafexus On

I figured out the problem. The below code works as expected.

var requesttwo = ItemWebApiRequestBuilder.ReadItemsRequestWithSitecoreQuery("/sitecore/content/Home//*[@@TemplateName='Article']")
.AddFieldsToRead(new string[3] { "Title", "Author", "Content" })
.Build();

ScItemsResponse response = null;
try
{
    // And execute it on a session asynchronously
    response = await session.ReadItemAsync(requesttwo);
}
catch (Exception e)
{
    String error = e.Message;
}

I just had to remove the "select * from". I imagine AddFieldsToRead is appending its own select statement which was making the request "select foo bar from select * from" or something of the like. Just removing that part from the query gets me a proper response.

The clue was in this document. The bottom example only shows a sitecore path as opposed to a full query.

string query = "/sitecore/media library/images/*";

var request = ItemWebApiRequestBuilder.ReadItemsRequestWithSitecoreQuery(query)
.AddScope(ScopeType.Self)
.PageNumber(0)
.ItemsPerPage(2)
.Build();

var response = await this.session.ReadItemAsync(request);