Introduction
In this article, you will learn how to fetch BCS contents in SharePoint Search, using REST API. This will be applicable for all SharePoint platforms.
Prerequisite
Following prerequisite needs to be done for BCS configuration:
- Create Database, Table, and View; give proper permissions to the DB to access it in External Content Type.
- Configure Secure Store Service.
- Create the External Content Types.
- Configure the Business Data Connectivity Service.
- Configure Search Crawl.
Configure Result Source
- Go to Site Settings in SharePoint root site as site collection admin.
- Click on Result source under Search.
- Click New Result Source.
- Give name and build query, as shown below.
- Give name for New Result Source.
- Select Protocol as Local SharePoint.
- Type as SharePoint Search Result.
- Click on Launch Query builder. Give the ContentSource name, as we created, in Search Crawl in Central Admin. I have created ContentSource named “TESTCRAWL”.
- Save the newly created ResultSource. Open the same ResultSource again. Take GUID of this newly created ResultSource, like “8d4aa563-98a2-4e84-974a-2a2532a0550e” from URL.
Get BCS contents in SharePoint Search using REST API
The Search REST API can also be used to fetch the BCS contents. Using SourceID, the search will give the BCS results.
We will build the REST query first. In the query URL, you can set the necessary filters required. The advantage of using REST is that sorting can be done by setting sortlist parameter. Please find the following query URL.
This will fetch all the contents related to BCS and we will filter out the necessary values using the code logic. jQuery Ajax call is used to fetch the results.
The below code snippet shows the implementation.
- $.ajax({
- url: "/_api/search/query?querytext='SEARCHTEXT'&SourceId='8d4aa563-98a2-4e84-974a-2a2532a0550e'",
- method: "GET",
- headers: {"accept": "application/json;odata=verbose",
- },
- success: function(data){
- var totalRowsCount = data.d.query.PrimaryQueryResult.RelevantResults.TotalRows;
- var rowCount = data.d.query.PrimaryQueryResult.RelevantResults.RowCount;
- var Results = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;
- var RestResults = new Array();
- for(var i=0;i<Results.length;i++){
- RestResults[i] = [Results[i].Cells.results[3]["Title"]];
- }
- },
- error: function(sender,args){
- }
- });
In the above code, SourceId is same as we created new ResultSource GUID.
Results[i].Cells.results[3]["Title"] will give the BCS title of the search. Based on the managed property created for search in Central Admin, we can fetch the other result values from BCS, using the above code. Other filtering, like row limits, refiners, or select parameters can be used to filter the results further.
Summary
Thus, you have learned how to retrieve BCS Contents in SharePoint Search Programmatically, using REST API.