Today I came across a requirement to get outlook events to SharePoint custom webpart. Thanks to Graph API to ease the work.
Here are the steps that we need to follow in order to get events.
1. Register a new app in Azure Active Directory. Here is the blog that i referred.
2. Now its time to get access token using generated client id and secret in previous step.
Execute below code to get access token.
Here are the steps that we need to follow in order to get events.
1. Register a new app in Azure Active Directory. Here is the blog that i referred.
2. Now its time to get access token using generated client id and secret in previous step.
Execute below code to get access token.
$.ajax({
type: "POST",
crossDomain: true,
url:
"https://cors-anywhere.herokuapp.com/https://login.microsoftonline.com/<Tenant
ID>/oauth2/v2.0/token",
headers: {
"content-type":
"application/x-www-form-urlencoded"
},
data: {
grant_type:
"client_credentials",
client_id: "",
//ClientId@TenantId
client_secret:
"",//Client Secret
scope:
"https://graph.microsoft.com/.default"
},
success: function(data) {
console.log(data.access_token);
getOutlookCalendarEvents(data.access_token);
},
error: function(data,
errorThrown, status) {}
});
|
You can get code access token from console of the browser.
3. Once you have access token, lets get outlook events. Below is the code to get events from outlook using access token generated earlier.
function getOutlookCalendarEvents(token){
var today = new
Date().toLocaleDateString();
var loggedInUser
= _spPageContextInfo.userEmail;
var
OutlookCalendarURL =
"https://graph.microsoft.com/v1.0/users/"+loggedInUser+"/events?&$orderby=start/DateTime&$filter=start/DateTime
ge '"+today+"'";
$.ajax({
type: "GET",
crossDomain: true,
url:OutlookCalendarURL,
headers: {
"authorization": "Bearer
" + token
},
success: function(data) {
console.log(data);
var
OutlookCalendarResults = data.value;
},
error: function(data, errorThrown,
status) {}
});
}
|
Above code gets events that are greater than today.
Here is the list of other API end points that could help in further options.
Cheers,
Keyur
Comments
Post a Comment