'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));
}