Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var authorizeSharepoint = function(){
var resource = 'https://abc.sharepoint.com';
var endpoint = 'https://abc.sharepoint.com/_api/web';
var authContext = new AuthenticationContext({
instance: 'https://login.microsoftonline.com/',
tenant: 'common',
clientId: 'a5f6eaff-7a30-4c08-9553-xxx',
postLogoutRedirectUri: 'http://localhost:8080/com.abc.services',
cacheLocation: 'localStorage'
});
jQuery("#sp-signInLink").click(function() {
authContext.login();
});
jQuery("#sp-signOutLink").click(function() {
authContext.logOut();
});
authContext.handleWindowCallback();
var user = authContext.getCachedUser();
if (user) {
jQuery("#sp-loginMessage").text("Welcome, " + user.userName);
jQuery("#sp-signInLink").hide();
jQuery("#sp-signOutLink").show();
authContext.acquireToken(resource, function(error, token) {
if (error || !token) {
jQuery("#sp-loginMessage").text('ADAL Error Occurred: ' + error);
return;
}
$.ajax({
type: 'GET',
url: endpoint,
headers: {
'Accept': 'application/json',
'Authorization': 'Bearer ' + token,
},
}).done(function(data) {
jQuery("#sp-loginMessage").text('The name of the SharePoint site is: ' + data.Title);
}).fail(function(err) {
jQuery("#sp-loginMessage").text('Error calling REST endpoint: ' + err.statusText);
}).always(function() {});
$.ajax({
type:"GET",
url:"https://abc.sharepoint.com/_api/web/GetFolderByServerRelativeUrl('/Shared Documents')/Files",
headers: {
'Accept' : "application/json;odata=verbose",
'Authorization': 'Bearer ' + token
},
success:function(data){
console.log('Here - ',data)
},
error: function(jqXHR, textStatus, errorThrown) {
//Error
}
});
});
} else if (authContext.getLoginError()) {
jQuery("#sp-signInLink").show();
jQuery("#sp-signOutLink").hide();
jQuery("#sp-loginMessage").text(authContext.getLoginError());
} else {
jQuery("#sp-signInLink").show();
jQuery("#sp-signOutLink").hide();
jQuery("#sp-loginMessage").text("You are not logged in.");
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.