Retrieve BCS Contents In SharePoint Search Programmatically, Using REST API

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:
  1. Create Database, Table, and View; give proper permissions to the DB to access it in External Content Type.
  2. Configure Secure Store Service.
  3. Create the External Content Types.
  4. Configure the Business Data Connectivity Service.
  5. 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.
  1. $.ajax({       
  2.     url: "/_api/search/query?querytext='SEARCHTEXT'&SourceId='8d4aa563-98a2-4e84-974a-2a2532a0550e'",       
  3.     method: "GET",       
  4.     headers: {"accept""application/json;odata=verbose",       
  5.     },       
  6.     success: function(data){     
  7.         var  totalRowsCount = data.d.query.PrimaryQueryResult.RelevantResults.TotalRows;    
  8.         var  rowCount = data.d.query.PrimaryQueryResult.RelevantResults.RowCount;     
  9.         var Results = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;      
  10.         var RestResults = new Array();      
  11.         for(var i=0;i<Results.length;i++){      
  12.             RestResults[i] = [Results[i].Cells.results[3]["Title"]];      
  13.         }      
  14.     },       
  15.     error: function(sender,args){      
  16.     }       
  17. });  
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.

Up Next
    Ebook Download
    View all
    Learn
    View all