How to Search For People as a Search Scope / Content Source Using SharePoint 2013 REST API

Introduction

The SharePoint 2013 Search API supports REST for the first time. I wanted to search for people, not documents. The key learning here is that you specify content sources via its GUID.

This new REST service is the best way to go in a variety of application scenarios. SharePoint Apps built in JavaScript is one good example. Applications external to SharePoint that require search functionality; can also leverage this service as the endpoint for communication into the Search platform.

The old search.asmx SOAP web service is marked as deprecated in SP2013 and the new Search.

new Search

Use the following procedure to create a sample.

Step 1: Navigate to your SharePoint 2013 site.

Step 2: From this page select Site Actions | Edit Page:

Edit the page, go to the "Insert" tab in the Ribbon and click the "Web Part" option. In the "Web Parts" picker area, go to the "Media and Content" category, select the "Script Editor" Web Part and press the "Add button".

Step 3: Once the Web Part is inserted into the page, you will see an "EDIT SNIPPET" link; click it. You can insert the HTML and/or JavaScript as in the following:

  1. <script src="/Scripts/jquery-1.10.1.min.js" type="text/javascript"></script>      
  2. <script type="text/javascript">  
  3. $(document).ready(function () {      
  4.     $("#SearchQuery").click(function() {                          
  5.             $.ajax({  
  6.                 url:window.location.protocol + "//" + window.location.host + "/_api/search/query?querytext='"+$("#search-input").val()+"*'&sourceid='b09a7990-05ea-4af9-81ef-edfab16c4e31'&rowlimit='100'&selectproperties='PictureURL, PreferredName, Country'",  
  7.                 headers: { "Accept""application/json; odata=verbose" },  
  8.                 contentType: "application/json; odata=verbose",  
  9.                 success: function (data) {  
  10.                     var results;  
  11.                    var divHTML = '';  
  12.                    var Picurl;  
  13.   
  14.                     if (data.d) {  
  15.                         if (data.d.query)  
  16.                             var users = new Array();                                     
  17.                         results = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;  
  18.                         for (i = 0; i < results.length; i++) {  
  19.                             var item = results[i];  
  20.                             var itemCell = item.Cells;  
  21.                             var itemResults = itemCell.results;  
  22.                             //Get Values for User  
  23.                             var userPic = getValueByKey("PictureURL", itemResults);  
  24.                             var fullname = getValueByKey("PreferredName", itemResults);  
  25.                             var CountryName= getValueByKey("Country", itemResults);                   
  26.       if(userPic!= null){                     
  27.           Picurl= userPic;  
  28.       }  
  29.       else  
  30.       {  
  31.           Picurl = '/Style Library/styles/images/tempPic.png';  
  32.       }                                            
  33.       // alert(PicId);   
  34.       divHTML += '<div class="item">'  
  35.               + '<div class="id">'  
  36.                               + '<div class="ms-tableCell ms-verticalAlignTop">'  
  37.                                   + '<div class="ms-peopleux-userImgDiv">'  
  38.                                       + '<div class="ms-imnSpan">'  
  39.                                            + '<div style="width: 36px; height: 30px;" id="ImgPicSpan1" class="ms-peopleux-userImgWrapper ms-subtleLink ms-peopleux-imgUserLink">'  
  40.                                               + '<img style="cliptop: 0px; clipright: 36px; clipbottom: 36px; clipleft: 0px; min-height: 30px; max-height:30px; min-width: 30px; max-width: 30px;" id="PictureDiv1" class="ms-peopleux-userImg" src="' + Picurl + '"/>'  
  41.                                           + '</div>'  
  42.                                       + '</div>'  
  43.                                   + '</div>'  
  44.                               + '</div>'  
  45.                               + '<div id="PersonNameDiv" class="ms-tableCell ms-verticalAlignTop" >'          + '<div> ' + fullname + ' </div>'  
  46.                                   + '<div class="place">' + CountryName + ' </div>'  
  47.                               + '</div>'  
  48.                           + '</div>'  
  49.                       + '</div>'  
  50.                   + '</div>'  
  51.               + '</div>'  
  52.           + '</div>'                     
  53.                         }  
  54. $("#Result").html(divHTML);                                        
  55.                     }  
  56.                     else if (data.d.postquery)  
  57.                         results = data.d.postquery.PrimaryQueryResult.RelevantResults.Table.Rows.results;  
  58.                     else  
  59.                         throw "Results not found";                      
  60.                 }  
  61.             });                                 
  62.   });                                                           
  63.     function getValueByKey(key, results) {  
  64.         var postItem = $.grep(results, function (e) {  
  65.             if (e.Key === key)  
  66.                 return e;  
  67.         })[0].Value;  
  68.         return postItem;  
  69.     }   
  70. });</script>  
  71.  <input type="text" id="search-input">  
  72. <input type="button" id="SearchQuery"  value="Search">  
  73. <div id="Result"></div> 

Final Output

 Output