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  :   2     Total  :    5653

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 January 15, 00:25
How To Enable Target Value And Actual Value In #D3 Gauge Chart

Twitter January 15, 00:24
How To Open #SharePoint List Hyperlink Column In Modal #Popup Window

Twitter January 15, 00:24
Quick Introduction To #Asp.NetCore And It’s Features

Twitter January 15, 00:22
How To Configure #PerformancePoint Services To Use Secure Store In #SharePoint 2013

Twitter January 15, 00:21
How To Block Or Disable #Office365 Services

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