Querying Search With JavaScript and REST: SharePoint 2013

This example shows how to do a search using the SharePoint 2013 REST API. In this sample we will create a page with a search box that displays results in a grid. To get started, create a new SharePoint-hosted app. Then, you must grant permissions to the App to use a search.

Develop the project using the following Method in the NAPA Tool

On your Developer Site, open the "Napa" Office 365 Development Tools and then choose Add New Project.

  • Choose the App for SharePoint template, name the project Create Site and then choose the Create button
  • Replace APP.js with the following source code below.
  • Publish Your App.


Permission

The following is an important procedure to be done before creating the app. Specify the permissions that your app needs as in the following. Choose the Properties button at the bottom of the page.
  • In the Properties window, choose Permissions.
  • In the Content category, set the Write permissions for the Tenant scope.
  • In the Social category, set the Read permissions for the User Profiles scope.
  • Close the Properties window.


Default ASPX: Now, edit Default.aspx to add the HTML for our search box and button as well as a div tag to hold the results.
  1. <div>  
  2.     <label for="searchTextBox">Search: </label>  
  3.     <input id="searchTextBox" type="text" />  
  4.     <input id="searchButton" type="button" value="Search" />  
  5. </div>  
  6.    
  7. <div id="resultsDiv">  
  8. </div>  
Source Code

Next, edit App.js to include your script to query the search and display the results. Add a click event handle to your document ready function. We'll put our code to do the query search here.
  1. var context = SP.ClientContext.get_current();  
  2.    
  3. // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model  
  4. $(document).ready(function () {  
  5.    
  6.     var spAppWebUrl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl'));  
  7.    
  8.     $("#searchButton").click(function () {  
  9.         var queryUrl = spAppWebUrl + "/_api/search/query?querytext='" + $("#searchTextBox").val() + "'";  
  10.    
  11.         $.ajax({ url: queryUrl, method: "GET", headers: { "Accept""application/json; odata=verbose" }, success: onQuerySuccess, error: onQueryError });  
  12.     });  
  13.    
  14. });  
  15.    
  16. function onQuerySuccess(data) {  
  17.     var results = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;  
  18.    
  19.     $("#resultsDiv").append('<table>');  
  20.    
  21.     $.each(results, function () {  
  22.         $("#resultsDiv").append('<tr>');  
  23.         $.each(this.Cells.results, function () {  
  24.             $("#resultsDiv").append('<td>' + this.Value + '</td>');  
  25.         });  
  26.         $("#resultsDiv").append('</tr>');  
  27.     });  
  28.    
  29.     $("#resultsDiv").append('</table>');  
  30. }  
  31.    
  32. function onQueryError(error) {  
  33.     $("#resultsDiv").append(error.statusText)  
  34. }  
  35.    
  36. //function to get a parameter value by a specific key  
  37. function getQueryStringParameter(urlParameterKey) {  
  38.     var params = document.URL.split('?')[1].split('&');  
  39.     var strParams = '';  
  40.     for (var i = 0; i < params.length; i = i + 1) {  
  41.         var singleParam = params[i].split('=');  
  42.         if (singleParam[0] == urlParameterKey)  
  43.             return decodeURIComponent(singleParam[1]);  
  44.     }  
  45. }  
Publish






Output



Thanks for reading. Cheers!

Reference: msdn

Up Next
    Ebook Download
    View all
    Learn
    View all