.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Articles >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Articles

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

Posted By:Sagar Pardeshi       Posted Date: June 23, 2014    Points: 200    Category: SharePoint    URL: http://www.dotnetspark.com  

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



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



Specifies the unique identifier of the Result Source to use for executing the search query.





The following steps shows:

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() {
                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;
                   var divHTML = '';
                   var Picurl;

                    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];
                            var itemCell = item.Cells;
                            var itemResults = itemCell.results;
                            //Get Values for User
                            var userPic = getValueByKey("PictureURL", itemResults);
                            var fullname = getValueByKey("PreferredName", itemResults);
                            var CountryName= getValueByKey("Country", itemResults);

      if(userPic!= null){
          Picurl= userPic;
          Picurl = '/Style Library/styles/images/tempPic.png';
      // alert(PicId);

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


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


    function getValueByKey(key, results) {
        var postItem = $.grep(results, function (e) {
            if (e.Key === key)
                return e;
        return postItem;


Final Output:

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend