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

Posted: July 20, 2014 in SharePoint2013

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.                     if (data.d) {
  14.                         if (data.d.query)
  15.                             var users = new Array();
  16.                         results = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;
  17.                         for (i = 0; i < results.length; i++) {
  18.                             var item = results[i];
  19.                             var itemCell = item.Cells;
  20.                             var itemResults = itemCell.results;
  21.                             //Get Values for User
  22.                             var userPic = getValueByKey(“PictureURL”, itemResults);
  23.                             var fullname = getValueByKey(“PreferredName”, itemResults);
  24.                             var CountryName= getValueByKey(“Country”, itemResults);
  25.       if(userPic!= null){
  26.           Picurl= userPic;
  27.       }
  28.       else
  29.       {
  30.           Picurl = ‘/Style Library/styles/images/tempPic.png’;
  31.       }
  32.       // alert(PicId);
  33.       divHTML += ‘<div class=”item”>’
  34.               + ‘<div class=”id”>’
  35.                               + ‘<div class=”ms-tableCell ms-verticalAlignTop”>’
  36.                                   + ‘<div class=”ms-peopleux-userImgDiv”>’
  37.                                       + ‘<div class=”ms-imnSpan”>’
  38.                                            + ‘<div style=”width: 36px; height: 30px;” id=”ImgPicSpan1″ class=”ms-peopleux-userImgWrapper ms-subtleLink ms-peopleux-imgUserLink”>’
  39.                                               + ‘<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 + ‘”/>’
  40.                                           + ‘</div>’
  41.                                       + ‘</div>’
  42.                                   + ‘</div>’
  43.                               + ‘</div>’
  44.                               + ‘<div id=”PersonNameDiv” class=”ms-tableCell ms-verticalAlignTop” >’          + ‘<div> ‘ + fullname + ‘ </div>’
  45.                                   + ‘<div class=”place”>’ + CountryName + ‘ </div>’
  46.                               + ‘</div>’
  47.                           + ‘</div>’
  48.                       + ‘</div>’
  49.                   + ‘</div>’
  50.               + ‘</div>’
  51.           + ‘</div>’
  52.                         }
  53. $(“#Result”).html(divHTML);
  54.                     }
  55.                     else if (data.d.postquery)
  56.                         results = data.d.postquery.PrimaryQueryResult.RelevantResults.Table.Rows.results;
  57.                     else
  58.                         throw “Results not found”;
  59.                 }
  60.             });
  61.   });
  62.     function getValueByKey(key, results) {
  63.         var postItem = $.grep(results, function (e) {
  64.             if (e.Key === key)
  65.                 return e;
  66.         })[0].Value;
  67.         return postItem;
  68.     }
  69. });</script>
  70.  <input type=“text” id=“search-input”>
  71. <input type=“button” id=“SearchQuery”  value=“Search”>
  72. <div id=“Result”></div>

Final Output

 Output
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s