Search People as a Search Scope or Content Source Using SharePoint 2013 REST API

InfoPath alternatives for form designing SharePoint

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 SharePoint 2013, 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 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”. Read about script editor web part in SharePoint.

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;
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;
}
else
{
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>’
}
$(“#Result”).html(divHTML);
}
else if (data.d.postquery)
results = data.d.postquery.PrimaryQueryResult.RelevantResults.Table.Rows.results;
else
throw “Results not found”;
}
});
});

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

})[0].Value;
return postItem;
}
});</script>

<input type=”text” id=”search-input”>

<input type=”button” id=”SearchQuery” value=”Search”>

<div id=”Result”></div>

Search People as a Search Scope or Content Source Using SharePoint 2013 REST API
Search People as a Search Scope or Content Source Using SharePoint 2013 REST API

Hope this will be helpful.

Similar SharePoint 2013 Tutorials


About Sagar Pardeshi

I am Developer working on Microsoft Technologies for the past 6+years. I am very much passionate about programming and my core skills are SharePoint, ASP.NET & C#,Jquery,Javascript,REST. I am running this blog to share my experience & learning with the community I am an MCP, MCTS .NET & Sharepoint 2010, MCPD Sharepoint 2010, and MCSD HTML 5,Sharepoint 2013 Core Solutions. I am currently working on Sharepoint 2010, MOSS 2007, Sharepoint 2013,Sharepoint 2013 App Dev, C#, ASP.NET, and SQL Server 2008.

View all posts by Sagar Pardeshi →

Leave a Reply