TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Search in App web using REST API in SharePoint 2013
Anit Kumar
Sep 22
2015
Code
1.6
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
SearchAppWeb.zip
Implementation:
Make use of Search REST service :
<Site URL>/_api/search/query?querytext=”<SearchTerm>”
.
Process the returned JSON result.
Add required Search Service Permission to App.Manifest. Add ‘Search’ in Scope and set permission to ‘
QueryAsUserIgnoreAppPrincipal
’.
Default.aspx
<%-- 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
'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));
}
SharePoint 2013
Search
App Web
Add In
REST API
JavaScript