|
1 | 1 | ---
|
2 | 2 | title: Using the SharePoint search Query APIs
|
3 |
| -ms.date: 09/25/2017 |
| 3 | +ms.date: 01/16/2020 |
4 | 4 | ms.prod: sharepoint
|
5 | 5 | ms.assetid: ae9d73ed-1140-430b-9287-01dbbe8ae7d1
|
6 | 6 | localization_priority: Priority
|
7 | 7 | ---
|
8 | 8 |
|
| 9 | +# Using the SharePoint search Query APIs |
9 | 10 |
|
| 11 | +Learn about the query APIs available in SharePoint that enable you to add search functionality to custom solutions and applications. |
10 | 12 |
|
11 |
| -# Using the SharePoint search Query APIs |
12 |
| -Learn about the query APIs available in SharePoint that enable you to add search functionality to custom solutions and applications. |
13 | 13 | ## SharePoint Query APIs
|
14 |
| -<a name="bk_QueryAPIs"> </a> |
15 | 14 |
|
16 | 15 | Search in SharePoint provides several query APIs, giving you lots of ways to access search results, so that you can return search results in a variety of custom solution types.
|
17 |
| - |
18 |
| - |
19 |
| - |
| 16 | + |
20 | 17 | In addition to the server object model that was available in previous versions of SharePoint, Search in SharePoint also provides the following:
|
21 |
| - |
22 |
| - |
23 |
| - |
24 | 18 |
|
25 | 19 | - Client object model (CSOM)
|
26 |
| - |
27 |
| - |
28 | 20 | - JavaScript object model (JSOM)
|
29 |
| - |
30 |
| - |
31 | 21 | - Representational State Transfer (REST) service
|
32 |
| - |
33 |
| - |
| 22 | + |
34 | 23 | Table 1 shows the APIs that you can use to program search queries and the path to the source file on the server.
|
35 |
| - |
36 |
| - |
37 |
| - |
38 | 24 |
|
39 | 25 | **Table 1. Search APIs**
|
40 | 26 |
|
41 |
| - |
42 | 27 | |**API name**|**Class library or schema and path**|
|
43 | 28 | |:-----|:-----|
|
44 | 29 | |.NET CSOM <br/> |Microsoft.SharePoint.Client.Search.dll <br/>%ProgramFiles%\\Common Files\\Microsoft Shared\\web server extensions\\15\\ISAPI <br/> |
|
45 | 30 | |Silverlight CSOM <br/> |Microsoft.SharePoint.Client.Search.Silverlight.dll <br/>%ProgramFiles%\\Common Files\\Microsoft Shared\\web server extensions\\15\\TEMPLATE\\LAYOUTS\\ClientBin <br/> |
|
46 | 31 | |JavaScript CSOM <br/> |SP.search.js <br/>%ProgramFiles%\\Common Files\\Microsoft Shared\\web server extensions\\15\\TEMPLATE\\LAYOUTS <br/> |
|
47 |
| -|REST service endpoints <br/> |http://server/_api/search/query <br/>http://server/_api/search/suggest <br/> | |
| 32 | +|REST service endpoints <br/> |https://{site_url}/_api/search/query <br/>https://{site_url}/_api/search/suggest <br/> | |
48 | 33 | |Server object model <br/> |Microsoft.Office.Server.Search.dll <br/>%ProgramFiles%\\Common Files\\Microsoft Shared\\web server extensions\\15\\ISAPI <br/> |
|
49 |
| - |
| 34 | + |
50 | 35 | As a best practice in SharePoint development, use client APIs when you can. Client APIs include the .NET, Silverlight, Phone, and JavaScript client object models, and the REST service. For more information about the APIs in SharePoint and when to use them, see [Choose the right API set in SharePoint](choose-the-right-api-set-in-sharepoint.md).
|
51 |
| - |
52 |
| - |
53 |
| - |
54 | 36 |
|
55 | 37 | ### Query using the .NET client object model
|
56 |
| -<a name="bk_QueryNETcsom"> </a> |
57 | 38 |
|
58 | 39 | Search in SharePoint includes a client object model that enables access to search results for online, on-premises, and mobile development. The Search in SharePoint CSOM is built on the SharePoint CSOM. Therefore, your client code first needs to access the SharePoint CSOM and then access the Search in SharePoint CSOM. For more information about the SharePoint CSOM and the [ClientContext](https://msdn.microsoft.com/library/Microsoft.SharePoint.Client.ClientContext.aspx) class, which is the entry point to the CSOM, see [SharePoint Complete Basic Operations Using SharePoint Client Library Code](https://docs.microsoft.com/sharepoint/dev/sp-add-ins/complete-basic-operations-using-sharepoint-client-library-code).
|
59 |
| - |
60 |
| - |
61 |
| - |
62 |
| -For the .NET managed CSOM, get a **ClientContext** instance (located in the [Microsoft.SharePoint.Client](https://msdn.microsoft.com/library/Microsoft.SharePoint.Client.aspx) namespace in the Microsoft.SharePoint.Client.dll). Then use the object model in the **Microsoft.SharePoint.Client.Search.Query** namespace in the Microsoft.SharePoint.Client.Search.dll. |
63 |
| - |
64 |
| - |
65 |
| - |
66 |
| -Here's a basic example. |
67 |
| - |
68 |
| - |
69 |
| - |
70 | 40 |
|
| 41 | +For the .NET managed CSOM, get a **ClientContext** instance (located in the [Microsoft.SharePoint.Client](https://msdn.microsoft.com/library/Microsoft.SharePoint.Client.aspx) namespace in the Microsoft.SharePoint.Client.dll). Then use the object model in the **Microsoft.SharePoint.Client.Search.Query** namespace in the Microsoft.SharePoint.Client.Search.dll. |
71 | 42 |
|
| 43 | +Here's a basic example. |
72 | 44 |
|
73 | 45 | ```cs
|
74 |
| - |
75 | 46 | using (ClientContext clientContext = new ClientContext("http://<serverName>/sites/<siteCollectionPath>"))
|
76 | 47 | {
|
77 |
| - KeywordQuery keywordQuery = new KeywordQuery(clientContext); |
78 |
| - keywordQuery.QueryText = "SharePoint"; |
79 |
| - SearchExecutor searchExecutor = new SearchExecutor(clientContext); |
80 |
| - ClientResult<ResultTableCollection> results = searchExecutor.ExecuteQuery(keywordQuery); |
81 |
| - clientContext.ExecuteQuery(); |
| 48 | + KeywordQuery keywordQuery = new KeywordQuery(clientContext); |
| 49 | + keywordQuery.QueryText = "SharePoint"; |
| 50 | + SearchExecutor searchExecutor = new SearchExecutor(clientContext); |
| 51 | + ClientResult<ResultTableCollection> results = searchExecutor.ExecuteQuery(keywordQuery); |
| 52 | + clientContext.ExecuteQuery(); |
82 | 53 | }
|
83 | 54 | ```
|
84 | 55 |
|
85 |
| -To download an example, see the following code sample posted by SharePoint MVP [Corey Roth](https://mvp.microsoft.com/mvp/Corey%20Roth-4029260): [SharePoint: Query Search with the Managed Client Object Model](https://code.msdn.microsoft.com/Query-Search-with-the-649f1bc1). |
86 |
| - |
87 |
| - |
88 |
| - |
| 56 | +To download an example, see the following code sample posted by SharePoint MVP [Corey Roth](http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2012/09/10/how-to-query-search-with-the-sharepoint-2013-client-object-model.aspx): [SharePoint: Query Search with the Managed Client Object Model](https://github.com/microsoftarchive/msdn-code-gallery-community-s-z/tree/master/SharePoint%202013%20Query%20Search%20with%20the%20Managed%20Client%20Object%20Model). |
89 | 57 |
|
90 | 58 | ### Query using the JavaScript client object model
|
91 |
| -<a name="bk_QueryJSOM"> </a> |
92 | 59 |
|
93 | 60 | For the JavaScript CSOM, get a [ClientContext](https://msdn.microsoft.com/library/Microsoft.SharePoint.Client.ClientContext.aspx) instance, and then use the object model in the SP.Search.js file.
|
94 |
| - |
95 |
| - |
96 |
| - |
97 |
| -Here's a basic example. |
98 |
| - |
99 |
| - |
100 |
| - |
101 | 61 |
|
| 62 | +Here's a basic example. |
102 | 63 |
|
103 |
| - |
104 |
| -``` |
105 |
| -
|
| 64 | +```cs |
106 | 65 | var clientContext = new SP.ClientContext("<serverRelativeUrl>");
|
107 | 66 | var contextSite = clientContext.get_site();
|
108 |
| -var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(clientContext); |
109 |
| -keywordQuery.set_queryText("SharePoint"); |
110 |
| -var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(clientContext); |
111 |
| -var results = searchExecutor.executeQuery(keywordQuery); |
| 67 | +var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(clientContext); |
| 68 | +keywordQuery.set_queryText("SharePoint"); |
| 69 | +var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(clientContext); |
| 70 | +var results = searchExecutor.executeQuery(keywordQuery); |
112 | 71 | context.executeQueryAsync(onQuerySuccess, onQueryError);
|
113 | 72 | ```
|
114 | 73 |
|
115 |
| -To download an example, see the following code sample posted by SharePoint MVP [Corey Roth](https://mvp.microsoft.com/mvp/Corey%20Roth-4029260): [SharePoint: Querying Search with the JavaScript Client Object Model](https://code.msdn.microsoft.com/SharePoint-2013-Querying-a629b53b). |
116 |
| - |
117 |
| - |
118 |
| - |
| 74 | +To download an example, see the following code sample posted by SharePoint MVP [Corey Roth](http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2012/09/10/how-to-query-search-with-the-sharepoint-2013-client-object-model.aspx): [SharePoint: Query Search with the Managed Client Object Model](https://github.com/microsoftarchive/msdn-code-gallery-community-s-z/tree/master/SharePoint%202013%20Query%20Search%20with%20the%20Managed%20Client%20Object%20Model). |
119 | 75 |
|
120 | 76 | ### Query using the REST service
|
121 |
| -<a name="bk_QueryREST"> </a> |
122 | 77 |
|
123 | 78 | SharePoint includes a REST service that enables you to remotely execute queries against the SharePoint Search service from client applications by using any technology that supports REST web requests. The Search REST service exposes two endpoints, **query** and **suggest**, and will support both **GET** and **POST** operations. Results are returned in either XML or JavaScript Object Notation (JSON) format.
|
124 |
| - |
125 |
| - |
126 |
| - |
127 |
| -The following is the access point for the service: `http://server/_api/search/`. You can also specify the site in the URL, as follows: `http://server/site/_api/search/`. The search service returns results from the entire site collection, so the same results are returned for both ways to access the service. |
128 |
| - |
129 |
| - |
130 |
| - |
131 |
| -See [SharePoint Search REST API overview](sharepoint-search-rest-api-overview.md) and [Retrieving query suggestions using the Search REST service](retrieving-query-suggestions-using-the-search-rest-service.md) for more information. |
132 |
| - |
133 |
| - |
134 |
| - |
| 79 | + |
| 80 | +The following is the access point for the service: `https://{site_url}/_api/search/`. You can also specify the site in the URL, as follows: `https://{site_url}/site/_api/search/`. The search service returns results from the entire site collection, so the same results are returned for both ways to access the service. |
| 81 | + |
| 82 | +See [SharePoint Search REST API overview](sharepoint-search-rest-api-overview.md) and [Retrieving query suggestions using the Search REST service](retrieving-query-suggestions-using-the-search-rest-service.md) for more information. |
135 | 83 |
|
136 | 84 | ### Query using the .NET server object model
|
137 |
| -<a name="bk_QuerySOM"> </a> |
138 |
| - |
139 |
| -Applications that use the server object model must run directly on a server that is running SharePoint. The search Query server object model resides in the [Microsoft.Office.Server.Search.Query](https://msdn.microsoft.com/library/Microsoft.Office.Server.Search.Query.aspx) namespace, which is located in Microsoft.Office.Server.Search.dll. |
140 |
| - |
141 |
| - |
142 |
| - |
143 |
| -As in SharePoint Server 2010, you use the **KeywordQuery** class to define the query, and then called the [Execute()](https://msdn.microsoft.com/library/Microsoft.Office.Server.Search.Query.Query.Execute.aspx) method to submit the query. In SharePoint, the **Execute** method is obsolete, and while it will still work, you should use the [SearchExecutor](https://msdn.microsoft.com/library/Microsoft.Office.Server.Search.Query.SearchExecutor.aspx) class instead. To submit the query, call the [ExecuteQuery()](https://msdn.microsoft.com/library/Microsoft.Office.Server.Search.Query.SearchExecutor.ExecuteQuery.aspx) method, passing the instance of the **KeywordQuery** class in the call. |
144 |
| - |
145 |
| - |
146 |
| - |
147 |
| -Here's a basic example. |
148 |
| - |
149 |
| - |
150 |
| - |
151 | 85 |
|
| 86 | +Applications that use the server object model must run directly on a server that is running SharePoint. The search Query server object model resides in the [Microsoft.Office.Server.Search.Query](/previous-versions/office/sharepoint-server/ms559899(v=office.15)) namespace, which is located in Microsoft.Office.Server.Search.dll. |
152 | 87 |
|
| 88 | +As in SharePoint Server 2010, you use the **KeywordQuery** class to define the query, and then called the [Execute()](/previous-versions/office/sharepoint-server/ms562867(v=office.15)) method to submit the query. In SharePoint, the **Execute** method is obsolete, and while it will still work, you should use the [SearchExecutor](/previous-versions/office/sharepoint-server/jj277656(v=office.15)) class instead. To submit the query, call the [ExecuteQuery()](/previous-versions/office/sharepoint-server/jj517659(v=office.15)) method, passing the instance of the **KeywordQuery** class in the call. |
153 | 89 |
|
154 |
| -```cs |
| 90 | +Here's a basic example. |
155 | 91 |
|
| 92 | +```cs |
156 | 93 | using (SPSite siteCollection = new SPSite("<serverRelativeUrl>"))
|
157 | 94 | {
|
158 |
| - KeywordQuery keywordQuery = new KeywordQuery(siteCollection); |
159 |
| - keywordQuery.QueryText = "SharePoint"; |
160 |
| - SearchExecutor searchExecutor = new SearchExecutor(); |
161 |
| - ResultTableCollection resultTableCollection = searchExecutor.ExecuteQuery(keywordQuery); |
162 |
| - resultTableCollection = resultTableCollection.Filter("TableType", KnownTableTypes.RelevantResults); |
163 |
| - ResultTable resultTable = resultTableCollection.FirstOrDefault(); |
164 |
| - DataTable dataTable = resultTable.Table; |
| 95 | + KeywordQuery keywordQuery = new KeywordQuery(siteCollection); |
| 96 | + keywordQuery.QueryText = "SharePoint"; |
| 97 | + SearchExecutor searchExecutor = new SearchExecutor(); |
| 98 | + ResultTableCollection resultTableCollection = searchExecutor.ExecuteQuery(keywordQuery); |
| 99 | + resultTableCollection = resultTableCollection.Filter("TableType", KnownTableTypes.RelevantResults); |
| 100 | + ResultTable resultTable = resultTableCollection.FirstOrDefault(); |
| 101 | + DataTable dataTable = resultTable.Table; |
165 | 102 | }
|
166 | 103 | ```
|
167 | 104 |
|
168 |
| -To download an example, see the following code sample posted by SharePoint MVP [Corey Roth](https://mvp.microsoft.com/mvp/Corey%20Roth-4029260): [SharePoint: Query Search with the KeywordQuery Class](https://code.msdn.microsoft.com/Query-Search-with-the-372139b5). |
169 |
| - |
170 |
| - |
171 |
| - |
| 105 | +To download an example, see the following code sample posted by SharePoint MVP [Corey Roth](http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2012/09/10/how-to-query-search-with-the-sharepoint-2013-client-object-model.aspx): [SharePoint: Query Search with the Managed Client Object Model](https://github.com/microsoftarchive/msdn-code-gallery-community-s-z/tree/master/SharePoint%202013%20Query%20Search%20with%20the%20Managed%20Client%20Object%20Model). |
172 | 106 |
|
173 | 107 | ## See also
|
174 |
| -<a name="bk_addresources"> </a> |
175 |
| - |
176 |
| - |
177 |
| -- [Building search queries in SharePoint](building-search-queries-in-sharepoint.md) |
178 |
| - |
179 |
| - |
180 |
| -- [SharePoint Search REST API overview](sharepoint-search-rest-api-overview.md) |
181 |
| - |
182 |
| - |
183 |
| -- [SharePoint: Using the search REST service from an app for SharePoint](https://code.msdn.microsoft.com/sharepoint/SharePoint-2013-Perform-a-1bf3e87d) |
184 |
| - |
185 |
| - |
186 |
| - |
187 |
| - |
188 |
| - |
189 |
| - |
| 108 | + |
| 109 | +- [Building search queries in SharePoint](building-search-queries-in-sharepoint.md) |
| 110 | +- [SharePoint Search REST API overview](sharepoint-search-rest-api-overview.md) |
0 commit comments