In this post, let me explain you about search in App Web using REST API in SharePoint.
- You can use Search REST service : <Site URL>/_api/search/query?querytext=”<SearchTerm>”.
- To Process the returned JSON result.
- Add required Search Service Permission to App.Manifest. Add ‘Search’ in Scope and set permission to ‘QueryAsUserIgnoreAppPrincipal’.
Now, write the following code on Default.aspx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
<%-- 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="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" %> <%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" 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="../Scripts/jquery-1.9.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> <meta name="WebPartPageExpansion" content="full" /> <!-- 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> <p id="message"> <!-- The following content will be replaced with the user name when you run the app - see App.js --> initializing... </p> </div> <!-- App Web Search--> <div id="searchDiv"> <b>Search For</b> <input type="text" id="txtSearchTerm" /> <input type="button" id="btnSearch" value="Search" onclick="searchAppWeb()" /> </div> <div id="SearchResultsDiv"></div> <!--Search Ends--> </asp:Content> |
App.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
'use strict'; var context = SP.ClientContext.get_current(); var user = context.get_web().get_currentUser(); // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model $(document).ready(function() { getUserName(); }); // This function prepares, loads, and then executes a SharePoint query to get the current users information function getUserName() { context.load(user); context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail); } // This function is executed if the above call is successful // It replaces the contents of the 'message' element with the user name function onGetUserNameSuccess() { $('#message').text('Hello ' + user.get_title()); } // This function is executed if the above call fails function onGetUserNameFail(sender, args) { alert('Failed to get user name. Error:' + args.get_message()); } //Search Implemenatation in App Web using Search REST API //Do not forget to provide Search Permission in AppManifest file var html; function searchAppWeb() { //Get the Search Term from textbox var searchTerm = $("#txtSearchTerm").val(); //REST API query URL var queryUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/search/query?querytext='" + searchTerm + "'";; //Empty the string html = ""; //Make the ajax call $.ajax({ url: queryUrl, method: "GET", headers: { "Accept": "application/json; odata=verbose" }, success: onSearchSuccess, error: onSearchError }); } function onSearchSuccess(data) { // JSON object contains two elements which have search results //1. PrimaryQueryResult //2. SecondaryQueryResults (When documents are grouped on Host Web) //Get PrimaryQueryResult and Render it in HTML Table format html = "<table>"; var primaryQueryResult = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results; if (primaryQueryResult != null && primaryQueryResult != undefined) { for (var iPrimaryResultCounter = 0; iPrimaryResultCounter < primaryQueryResult.length; iPrimaryResultCounter++) { html += "<tr><td>"; html += primaryQueryResult[iPrimaryResultCounter].Cells.results[3].Value; html += "</td><td><a href=\"" html += primaryQueryResult[iPrimaryResultCounter].Cells.results[6].Value; html += "\">" + primaryQueryResult[iPrimaryResultCounter].Cells.results[6].Value + "</a></td><tr>"; } } //Get SecondaryQueryResults and continue rendering it in HTML Table format var secondaryResult = data.d.query.SecondaryQueryResults; if (data.d.query.SecondaryQueryResults != null && data.d.query.SecondaryQueryResults != undefined) { for (var iSecondaryResultCounter = 0; iSecondaryResultCounter < data.d.query.SecondaryQueryResults.results.length; iSecondaryResultCounter++) { var resultBlock = data.d.query.SecondaryQueryResults.results[iSecondaryResultCounter].RelevantResults.Table.Rows.results; for (var iResults = 0; iResults < resultBlock.length; iResults++) { html += "<tr><td>"; html += resultBlock[iResults].Cells.results[3].Value; html += "</td><td><a href=\"" html += resultBlock[iResults].Cells.results[6].Value; html += "\">" + resultBlock[iResults].Cells.results[6].Value + "</a></td><tr>"; } } } html += "</table>"; $("#SearchResultsDiv").append(html); } function onSearchError(err) { alert(JSON.stringify(err)); } |
Best SharePoint 2013 Hosting Recommendation
HostForLIFE.eu
HostForLIFE.eu offer the Best, Cheap and Recommended SharePoint 2013 hosting start from €9.99/month. They can load your site against anticipated traffic and recommend the best option for you. HostForLIFE.eu is Microsoft No #1 Recommended Windows and ASP.NET Hosting in European Continent. Their service is ranked the highest top #1 spot in several European countries, such as: Germany, Italy, Netherlands, France, Belgium, United Kingdom, Sweden, Finland, Switzerland and many top European countries.
ASPHostPortal.com
ASPHostPortal.com’s Best ASP.NET hosting platform on Windows 2012 and Windows 2008 is compatible with SharePoint 2013 hosting. Needless to say, you’ll be able to really feel very comfy with their hosting service. Their best and inexpensive SharePoint 2013 hosting package is starting from $9.99/mo only. And with their promo code, you will get free domain as well. Not just that, in addition they supply 30 days cash back guarantee. Their servers are 99.99% uptime, it’s important believe which you require for your SharePoint 2013 site, so your website is by no means down.
DiscountService.com.au
Get high functionality, best uptime along with the most dependable SharePoint 2013 Hosting site with them. SharePoint 2013 hosting from DiscountService.com.au provides a protected, trustworthy and performance-driven foundation for your website and apps. If you’re searching for the correct Windows ASP.NET hosting that support SharePoint 2013 hosting provider, they are the correct selection for you. They have proactive monitoring down to seconds with reactive options in spot to ensure the stability in the services they offer. All hosting servers are monitored 24/7/365. They use enterprise software to monitor their whole network infrastructure. Their very best and inexpensive SharePoint 2013 hosting price begins from $10.00 monthly.