SharePoint 2013 search API and parsing search results for data table (Part 2 of 3)

This is part 2 of a 3 part series which is about SharePoint 2013 search API and parsing search results using data table.

SharePoint developers – How can I forget about you while writing my blogs? This post is specifically for SharePoint developers who want to leverage SharePoint’s search API to get results from SharePoint search and convert them to data table and use them in their front-end screens.

Most of you, might already be aware that SharePoint 2013 is equipped with brand new REST based search API which resides at URL: http://sharepointportal/_api/search and you can get search result by passing this URL in browser: http://sharepointportal/_api/search/query?querytext=’muneeb’  –

However, in most of the cases we want to parse these results into data table and attach it with repeaters or data lists. There is no need to reinvent the wheel. I wrote a method which parses the data and converts your query results into data table which you can bind easily with user interface.

DataTable aTable = new DataTable();

private void ParseSearchResults(Uri uri){

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri) as HttpWebRequest;

webRequest.Accept = “application/xml”;

webRequest.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();

 string result = string.Empty;

System.Xml.XmlDocument XMLDoc = new System.Xml.XmlDocument();

 using (StreamReader data = new StreamReader(response.GetResponseStream()))

result = data.ReadToEnd();

 XMLDoc.LoadXml(result); //Load XML in XML Document

 XmlNodeList nodeList;

XmlNode root = XMLDoc.DocumentElement;

nodeList = XMLDoc.GetElementsByTagName(“d:Table”); //Get Table Node.

 XmlNode TempNode = nodeList[0].ChildNodes[0].ChildNodes[0];

 // Adding columns in DataTable.

foreach (XmlNode childNode in TempNode.ChildNodes[0].ChildNodes)



 //Adding rows in DataTable.

foreach (XmlNode aNode in nodeList[0].ChildNodes[0].ChildNodes)


aRow = aTable.NewRow();

 foreach (XmlNode childNode in aNode.ChildNodes[0].ChildNodes)

aRow[childNode.ChildNodes[0].InnerText.ToString()] = childNode.ChildNodes[1].InnerText.ToString();



 //Accept changes to DataTable.



Call to the method:

ParseSearchResults(new Uri(“http://sharepointportalurl/_api/search/query?




I passed these parameters in query to make it more useful for me.

SourceID: I have configured many sources. A single source can point to SharePoint location that can be a document library or folder or even site collection (I will explain in my next post how to configure sources)

RowLimit: To limit the maximum number of results – Consider this where performance is an issue.

Selectproperties: Metadata which I want to return with my search results – otherwise, there are many additional fields which may not be required.

So its easy – just make your URL and pass to this method. It will give you a dataTable. J

Happy SharePointing !!




1 Comment

  1. bbh

    Can you please show how to do this for json output?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: