Amsterdam, Netherlands
30 Nov - 03 Dec, 2026
COPENHAGEN, DENMARK
29 JUN - 02 JUL, 2026
BARCELONA, SPAIN
28 SEP - 01 OCT, 2026
ESPC Amsterdam26
RAI, AMSTERDAM, 30 NOV - 03 DEC, 2026
European Power Platform Conference
BELLA CENTER, COPENHAGEN, 29 JUN - 02 JUL 2026
European Microsoft Fabric + SQL Community Conference
BARCELONA, SPAIN, 28 SEP - 01 OCT, 2026
ESPC Amsterdam26
RAI, AMSTERDAM, 30 NOV - 03 DEC, 2026
European Power Platform Conference
BELLA CENTER, COPENHAGEN, 29 JUN - 02 JUL 2026
European Microsoft Fabric + SQL Community Conference
BARCELONA, SPAIN, 28 SEP - 01 OCT, 2026
Let’s assume this: you want to create a custom application (e.g. a webpart) that should be used as a Search driven Application. This custom application should display all documents belonging to a predefined content type and it should use the SharePoint search index only to retrieve all matching documents and some of their metadata. One way to start is to have a look on the KeywordQuery object. With the KeywordQuery object a result table can be requested from SharePoint Search based on a keyword query – almost the same way as you would type in your query to a SharePoint search box manually.
Let’s have a look on a code sample:
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.GetPro(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: ResultTableCollection searchResults = new ResultTableCollection();
16:
17: try
18: {
19: searchResults = query.Execute();
20: }
21: catch (Microsoft.Office.Server.Search.Query.QueryMalformedException ex)
22: {
23: // Something wrong with the query
24: }
25: catch (Microsoft.Office.Server.Search.Query.InvalidPropertyException ex)
26: {
27: // Invalid managed property
28: }
29:
30: if (searchResults.Exists(ResultType.RelevantResults))
31: {
32: ResultTable searchResult = searchResults[ResultType.RelevantResults];
33:
34: DataTable result = new DataTable(“Result”);
35: result.Load(searchResult, LoadOption.OverwriteChanges);
36:
37: retParam = result;
38: }
39: }
40: }
41:
42: return retParam;
43: }
This custom method ExecuteKeywordQuery() takes a query string, starts a query on the SharePoint Search index and returns the results as a DataTable object. Let’s see how the results look like. In my example I have created a new Content Type (called MyContentType) and attached this new Content Type to a library. After that I added some sample documents and started a full crawl.
My sample library looks like this:
Become an ESPC Community Member today to access a wealth of SharePoint, Teams and Azure knowledge for free. New content is added daily to the online Resource Centre, across a variety of topics and formats from Microsoft MVPs and industry experts. With over 2,500 eBooks, webinars, presentations, how-to videos and blogs, there is something to suit everyone’s learning styles and career goals.
Join 30,000+ professionals and stay ahead of the curve. Get the latest conference updates, industry insights and early access across Microsoft 365, Power Platform, Fabric & SQL. Unsubscribe at any time.