SharePoint: Using the Keyword Query for Search Driven Applications
The KeywordQuery object
The KeywordQuery object exposes a collection namedSelectedProperties. This collection can be used to add additional properties to the resulting DataTable. In my code example I add the managed metadata ‘Devision‘ to the KeywordQuery (line 15) and the code sample now looks like this:
1: DataTable ExecuteKeywordQuery(string queryText)
2: {
3: DataTable retParam = null;
4:
5: if (false == string.IsNullOrEmpty(queryText))
6: {
7: using (new SPMonitoredScope(“ExecuteKeywordQuery”))
8: {
9: var proxy = (SearchServiceApplicationProxy)SearchServiceApplicationProxy. GetProxy(SPServiceContext.GetContext(SPContext.Current.Site));
10: var query = new KeywordQuery(proxy);
11: query.ResultsProvider = Microsoft.Office.Server.Search.Query.SearchProvider.Default;
12: query.QueryText = queryText;
13: query.ResultTypes |= ResultType.RelevantResults;
14:
15: query.SelectProperties.Add(“Devision”);
16:
17: ResultTableCollection searchResults = new ResultTableCollection();
18:
19: try
20: {
21: searchResults = query.Execute();
22: }
23: catch (Microsoft.Office.Server.Search.Query.QueryMalformedException ex)
24: {
25: // Something wrong with the query
26: }
27: catch (Microsoft.Office.Server.Search.Query.InvalidPropertyException ex)
28: {
29: // Invalid managed property
30: }
31:
32: if (searchResults.Exists(ResultType.RelevantResults))
33: {
34: ResultTable searchResult = searchResults[ResultType.RelevantResults];
35:
36: DataTable result = new DataTable(“Result”);
37: result.Load(searchResult, LoadOption.OverwriteChanges);
38:
39: retParam = result;
40: }
41: }
42: }
43:
44: return retParam;
45: }
To be able to use a SelectedProperty I first need to create a new Managed Property in the Search Service Application. So I create a new Managed Property (called Devision) and assign it to the appropriate Crawled Property. After that I start a new full crawl.
After the full crawl is finished it’s time to run the modified code:

As expected the resulting DataTable again has two rows because two documents are matching the query, but the resulting DataTable only has one column now! That was not expected.I just added another property and so I expected the resulting DataTable to have one more column as before now! Why does the resulting DataTable has only one column – and not 17 as expected?
Let’s have a closer look on the column:

The resulting DataTable only has one column now – and it’s the Devision column! Where are the other columns (Title, Path, Autor, …)?
As soon as SelectedProperties are used the ‘standard’ properties are removed from the resulting DataTable. Think of the standard properties as ‘standard selected properties’. Explained in simple words: As soon as you add a SelectedProperty the KeywordQuery ‘removes’ all other SelectedProperties.
But there’s a workaround: just add the ‘standard’ properties as new SelectedProperties. To check this I added ‘Title’ and ‘Author’ as additional selected properties. My code sample now looks like this:
1: DataTable ExecuteKeywordQuery(string queryText)
2: {
3: DataTable retParam = null;
4:
5: if (false == string.IsNullOrEmpty(queryText))
6: {
7: using (new SPMonitoredScope(“ExecuteKeywordQuery”))
8: {
9: var proxy = (SearchServiceApplicationProxy)SearchServiceApplicationProxy. GetProxy(SPServiceContext.GetContext(SPContext.Current.Site));
10: var query = new KeywordQuery(proxy);
11: query.ResultsProvider = Microsoft.Office.Server.Search.Query.SearchProvider.Default;
12: query.QueryText = queryText;
13: query.ResultTypes |= ResultType.RelevantResults;
14:
15: query.SelectProperties.Add(“Title”);
16: query.SelectProperties.Add(“Author”);
17: query.SelectProperties.Add(“Devision”);
18:
19: ResultTableCollection searchResults = new ResultTableCollection();
20:
21: try
22: {
23: searchResults = query.Execute();
24: }
25: catch (Microsoft.Office.Server.Search.Query.QueryMalformedException ex)
26: {
27: // Something wrong with the query
28: }
29: catch (Microsoft.Office.Server.Search.Query.InvalidPropertyException ex)
30: {
31: // Invalid managed property
32: }
33:
34: if (searchResults.Exists(ResultType.RelevantResults))
35: {
36: ResultTable searchResult = searchResults[ResultType.RelevantResults];
37:
38: DataTable result = new DataTable(“Result“);
39: result.Load(searchResult, LoadOption.OverwriteChanges);
40:
41: retParam = result;
42: }
43: }
44: }
45:
46: return retParam;
47: }
You can see my changes in line 15 and line 16.
Let’s see how the resulting DataTable looks now:

Much better! As expected the resulting DataTable now has three columns (because I used three selected properties). And the columns are the expected ones:

With the KeywordQuery object you can create powerful custom Search Driven Applications that get all their properties from the Search Index. With the SelectedProperties collection of the KeywordQuery object you can define the layout of the resulting DataTable according to the requirements.
If you have any questions on the above blog post, please leave a comment below.




