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.SelectProperties.Add("ContentType");
keywordQuery.SelectProperties.Add("ContentTypeId");
keywordQuery.SelectProperties.Add("GVIDoc");
keywordQuery.SourceId = Guid.NewGuid();
keywordQueriesList.Add(keywordQuery);
queryIdsList.Add(Convert.ToString(keywordQuery.SourceId));
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);
clientContext.ExecuteQuery();
//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());
docTopicsList.Add(docTopic);
}
}
}
return docTopicsList;
}
Happy Coding,
Sathish Nadarajan.
Leave a comment