Sunday, May 19, 2013

Developing Search App to Query by Result Sources using Search REST

I hope many people know about Result Source in SharePoint 2013 by now (if not Please  google out :))
Now Result sources can be queried using Search Rest Api
Following code snippet show how to do that
1)Along with query parameter you need to know guid for Result Source
To get this GUID , browse to this link _layouts/15/manageresultsources.aspx?level=site

2) Edit the required Result Source, and in browser  ,guid can be obtained from url similar to this url
3)Add New Napa Project and add  replace app.js with this code snippet

$(document).ready(function () {
    var e = ExecuteOrDelayUntilScriptLoaded(showToolbar, "sp.js");

function showToolbar() {

function executeQuery(queryTerms) {

    Results = {
        element: '',
        url: '',

        init: function (element) {
            Results.element = element;
            Results.url = _spPageContextInfo.webAbsoluteUrl + "/_api/search/query?querytext='" + queryTerms + "'&sourceid='48fec42e-4a92-48ce-8363-c2703a40e67d'";

        load: function () {
                        url: Results.url,
                        method: "GET",
                        headers: {
                            "accept": "application/json;odata=verbose",
                        success: Results.onSuccess,
                        error: Results.onError

        onSuccess: function (data) {
            var results = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;
            var html = "<table>";

            for (var i = 0; i < results.length; i++) {
                html += "<tr><td>";
                html += results[i].Cells.results[0].Value;
                html += "</td><td>"
                html += results[i].Cells.results[1].Value;
                html += "</td></tr>";
     html += "<tr><td>";
                html += results[i].Cells.results[2].Value;
                html += "</td><td>"
                html += results[i].Cells.results[3].Value;
                html += "</td></tr>";
     html += "<tr><td>";
                html += results[i].Cells.results[4].Value;
                html += "</td><td>"
                html += results[i].Cells.results[5].Value;
                html += "</td></tr>";
     html += "<tr><td>";
                html += results[i].Cells.results[6].Value;
                html += "</td><td>"
                html += results[i].Cells.results[7].Value;
                html += "</td></tr>";

            html += "</table>";

        onError: function (err) {



4) and Replace Default.aspx as follows

<%-- The following 4 lines are ASP.NET directives needed when using SharePoint components --%>
<%@ Page Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage, Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" MasterPageFile="~masterurl/default.master" Language="C#" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%-- The markup and script in the following Content element will be placed in the <head> of the page --%>
<asp:Content ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
 <script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js"></script>
 <script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
 <script type="text/javascript" src="/_layouts/15/sp.js"></script>

 <!-- Add your CSS styles to the following file -->
 <link rel="Stylesheet" type="text/css" href="../Content/App.css" />

 <!-- Add your JavaScript to the following file -->
 <script type="text/javascript" src="../Scripts/App.js"></script>

<%-- The markup in the following Content element will be placed in the TitleArea of the page --%>
<asp:Content ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server">
 Page Title

<%-- The markup and script in the following Content element will be placed in the <body> of the page --%>
<asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server">

  <div id="toolbarDiv" style="display: none">
        <input type="text" style="width: 200px" id="queryTerms" />
        <input type="button" value="Search" onclick="executeQuery($get('queryTerms').value);" />
    <div id="resultsDiv"></div>


5)And Apply Search Permissions as Read In Settings-->Permissions tab of Project 

To  Know more about search end point visit this post

No comments: