<div id="content-header">
<strong><span class='app-user navbar-text'></span></strong>
<div class="search-area">
<input id="tb-search-input" type="text" value="search query" />
<input id="btn-search" type="button" value="Search" /><input id="btn-onedrive" type="button" value="OneDrive" />
<div id="search-results">
</div>
</div>
</div>
<script type="text/javascript">
(function (window, $) {
// Azure AD App Manifest - Set 'oauth2AllowImplicitFlow' property to 'true' ("oauth2AllowImplicitFlow": true)
//
https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-application-manifes... var sharePointTenantName = '<<yourO365tenant>>';
var config = window.config = {
tenant: sharePointTenantName + '.onmicrosoft.com',
clientId: 'feea2058-6ff6-4847-9dfe-854400becd24',
postLogoutRedirectUri: window.location.origin,
endpoints: {
graphApiUrl: '
https://graph.microsoft.com',
sharePointUrl: 'https://' + sharePointTenantName + '.sharepoint.com'
},
cacheLocation: 'localStorage'
};
var authContext = new AuthenticationContext(config);
var $userDisplay = $(".app-user");
var $searchInput = $('#tb-search-input');
var $searchButton = $('#btn-search');
var $searchResultsDiv = $('#search-results');
var $onedriveButton = $('#btn-onedrive');
var isCallback = authContext.isCallback(window.location.hash);
authContext.handleWindowCallback();
// Check Login Status, Update UI
var user = authContext.getCachedUser();
if (user) {
$userDisplay.html(user.userName);
$userDisplay.show();
$searchButton.click(function () {
var searchText = $searchInput.val();
$searchResultsDiv.empty();
if (searchText.length > 0) {
search(searchText);
}
});
$onedriveButton.click(function () {
onedrive();
});
}
else {
authContext.login();
$userDisplay.empty();
$userDisplay.hide();
$searchInput.hide();
$searchButton.hide();
}
function search(searchText) {
var searchEndpoint = 'https://' + sharePointTenantName + '.sharepoint.com/_api/search/query?querytext=\'' + searchText + '\'';
authContext.acquireToken(config.endpoints.sharePointUrl, function (error, token) {
if (error || !token) {
console.log(error);
}
else {
$.ajax({
beforeSend: function (request) {
request.setRequestHeader("Accept", "application/json");
},
type: "GET",
url: searchEndpoint,
dataType: "json",
headers: {
'Authorization': 'Bearer ' + token,
}
}).done(function (response) {
$searchResultsDiv.html(JSON.stringify(response));
}).fail(function (response) {
console.log(response.responseText);
});
}
});
}
function onedrive() {
var onedriveEndpoint = "
https://graph.microsoft.com/v1.0/me";
authContext.acquireToken(config.endpoints.graphApiUrl, function (error, token) {
if (error || !token) {
console.log(error);
}
else {
$.ajax({
beforeSend: function (request) {
request.setRequestHeader("Accept", "application/json");
},
type: "GET",
url: onedriveEndpoint,
dataType: "json",
headers: {
'Authorization': 'Bearer ' + token,
}
}).done(function (response) {
$searchResultsDiv.html(JSON.stringify(response));
}).fail(function (response) {
console.log(response.responseText);
});
}
});
}
})(window, window.jQuery);
</script>