SharePoint Pals
 | Sign In
How to do the Batch Search ExecuteQueries in SharePoint 2013 using Client Side Object Model in C#
by Sathish Nadarajan 26 Feb 2016
SharePoint MVP
Today  :   3     Total  :    4622

In one of the Older Article, we saw within a WebPart, how to execute the ExecuteQueries in a Server Side Coding. Now, I met with the same kind of requirement, but the difference is, here I am executing this search from a WebAPI. Already, we saw here how to create a basic WebAPI.

Let me share the piece of code, which is straight forward. Am not explaining this method as it is a Static and does not have any other external dependencies.

 private static List<DocTopic> GetTopicDocumentCountBatch(TermCollection docTopicsTermCollection, string locationTermID, ClientContext clientContext)
 //The List of KeywordQuery which will be converted as an Array later
             List<KeywordQuery> keywordQueriesList = new List<KeywordQuery>();
 //The List of QueryID which will be converted as an Array later
             List<string> queryIdsList = new List<string>();
             string contentSiteURL = Convert.ToString(ConfigurationManager.AppSettings["ContentSiteURL"]);
             Dictionary<string, string> docTopicQueryID = new Dictionary<string, string>();
 //Framing the Queries
             foreach (Term docTopicTerm in docTopicsTermCollection)
                 KeywordQuery keywordQuery = new KeywordQuery(clientContext);
                 keywordQuery.QueryText = string.Format("(IsDocument:True OR contentclass:STS_ListItem)  Tags:#0{0} GVIDoc:[{1}] SPSiteUrl:" + contentSiteURL + " (ContentTypeId:0x010100458DCE3990BC4C658D4AB1D0CA3B9782* OR ContentTypeId:0x0120D520A808* OR ContentType:GVIarticle)", locationTermID, docTopicTerm.Name); ;
                 keywordQuery.IgnoreSafeQueryPropertiesTemplateUrl = true;
                 keywordQuery.SourceId = Guid.NewGuid();
                 docTopicQueryID.Add(Convert.ToString(keywordQuery.SourceId), docTopicTerm.Name);
 //Convert the KeywordQuery and QueryID into array,
             KeywordQuery[] keywordQueries = keywordQueriesList.ToArray();
             string[] queryIds = queryIdsList.ToArray();
 //Initialize the SearchExecutor
             SearchExecutor searchExecutor = new SearchExecutor(clientContext);
 //Actual use of ExecuteQueries method
             var results = searchExecutor.ExecuteQueries(queryIds, keywordQueries, false);
 //Iterating the Result Set.
             List<DocTopic> docTopicsList = new List<DocTopic>();
            if (results.Value.Count > 0)
                 foreach (var result in results.Value)
                     if (result.Value[0].ResultRows.Count() > 0)
                         DocTopic docTopic = new DocTopic();
                         docTopic.Title = Convert.ToString(docTopicQueryID[result.Key]);
                         docTopic.Url = "[" + docTopic.Title + "]";
                         docTopic.TotalCount = result.Value[0].ResultRows.Count();
                         docTopic.VideoCount = Convert.ToString(result.Value[0].ResultRows.SelectMany(m => m).Where(k => k.Key.Equals("ContentTypeId")).Select(m => m.Value).Where(y => y.ToString().Contains("0x0120D520A808")).Count());
                         docTopic.ArticleCount = Convert.ToString(result.Value[0].ResultRows.SelectMany(m => m).Where(k => k.Key.Equals("ContentType")).Select(m => m.Value).Where(y => y.ToString().Contains("GVIarticle")).Count());
                         docTopic.DocumentCount = Convert.ToString(result.Value[0].ResultRows.SelectMany(m => m).Where(k => k.Key.Equals("ContentTypeId")).Select(m => m.Value).Where(y => y.ToString().Contains("0x010100458DCE3990BC4C658D4AB1D0CA3B9782")).Count());
             return docTopicsList;

Happy Coding,

Sathish Nadarajan.

blog comments powered by Disqus

SharePoint Pals

SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile


Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community

SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to in a word document.

Related Resources

Recent Tweets

Twitter October 20, 13:01
How to Add a JS Link Reference to the NewForm and EditForm Programmatically using CSOM PNP in SharePoint Office 365-

Twitter October 12, 12:15
How to Deploy Provider Hosted Apps (Add-Ins) by App Stapling in SharePoint Office 365 -

Twitter October 11, 13:39
How to Deploy Provider HostedApp programmatically using CSOM in SharePoint Office 365 Activating Developer Feature -

Twitter October 10, 09:26
How to Use Angular HTTP Services in SharePoint List -

Twitter October 10, 09:25
How to Get All Items from SharePoint List and Display it in Custom HTML table with Sorting using AngularJS -

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation