Saturday 28 June 2014

How search for People as a Search Scope / Content Source Using SharePoint 2013 REST API



Introduction

SharePoint 2013 Search API via 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. External Applications 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

sourceid
Specifies the unique identifier of the Result Source to use for executing the search query.
String
Empty
http://host/site/_api/search/query?querytext='term'&sourceid='b09a7990-05ea-4af9-81ef-edfab16c4e31'

The following stepsshows:
Step 1: Navigate to your SharePoint 2013 site.

Steps 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. You can insert the HTML and/or JavaScript:


<script src="/Scripts/jquery-1.10.1.min.js" type="text/javascript"></script>

<script type="text/javascript">
$(document).ready(function () {   
    $("#SearchQuery").click(function() {


            $.ajax({
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'",
headers: { "Accept": "application/json; odata=verbose" },
contentType: "application/json; odata=verbose",
success: function (data) {
var results;
vardivHTML = '';
varPicurl;

if (data.d) {
if (data.d.query)
var users = new Array();
                                                      

results = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;
for (i = 0; i <results.length; i++) {
var item = results[i];
varitemCell = item.Cells;
varitemResults = itemCell.results;
//Get Values for User
varuserPic = getValueByKey("PictureURL", itemResults);
varfullname = getValueByKey("PreferredName", itemResults);
varCountryName= getValueByKey("Country", itemResults);



if(userPic!= null){

Picurl=userPic;
      }
else
      {
Picurl = '/Style Library/styles/images/tempPic.png';
      }


// alert(PicId);

divHTML += '<div class="item">'
              + '<div class="id">'
                              + '<div class="ms-tableCellms-verticalAlignTop">'
                                  + '<div class="ms-peopleux-userImgDiv">'
                                      + '<div class="ms-imnSpan">'
                                           + '<div style="width: 36px; height: 30px;" id="ImgPicSpan1" class="ms-peopleux-userImgWrapperms-subtleLinkms-peopleux-imgUserLink">'
                                              + '<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 + '"/>'
                                          + '</div>'
                                      + '</div>'
                                  + '</div>'
                              + '</div>'
                              + '<div id="PersonNameDiv" class="ms-tableCellms-verticalAlignTop" >'          + '<div>' + fullname + '</div>'
                                  + '<div class="place">' + CountryName + ' </div>'
                              + '</div>'
                          + '</div>'
                      + '</div>'
                  + '</div>'
              + '</div>'
          + '</div>'

                          
                        }
$("#Result").html(divHTML);

                          
                    }
elseif (data.d.postquery)
results = data.d.postquery.PrimaryQueryResult.RelevantResults.Table.Rows.results;
else
throw"Results not found";


                }
            });





  });      


functiongetValueByKey(key, results) {
varpostItem = $.grep(results, function (e) {
if (e.Key === key)
return e;
})[0].Value;
returnpostItem;
    }

});</script>
<input type="text" id="search-input">
<input type="button" id="SearchQuery"  value="Search">
<div id="Result"></div>

Final Output:

 
c         

No comments:

Post a Comment

SharePoint 2013 - Uploading Multiple Attachments To The New Item On List Using JSOM And REST API

  Introduction In this article, we will explore how we can attach multiple attachments to the new item on list using JSOM and REST API. Ther...