Loading...

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
_layouts/15/EditResultSource.aspx?level=site&sourceid=48fec42e-4a92-48ce-8363-c2703a40e67d&view=1
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() {
    $("#toolbarDiv").show();
}

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 () {
            $.ajax(
                    {
                        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>";
            Results.element.html(html);
        },

        onError: function (err) {
            alert(JSON.stringify(err));
        }
    }

    Results.init($('#resultsDiv'));
    Results.load();

}


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=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" MasterPageFile="~masterurl/default.master" Language="C#" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, 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>
</asp:Content>

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

<%-- 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>
  <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>
    <div id="resultsDiv"></div>
 </div>

</asp:Content>

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: