Skip to content

Instantly share code, notes, and snippets.

@nakkeerann
Created November 4, 2017 19:14
Gist to get the My Microsoft Teams
<script type="text/javascript" src="/SiteAssets/jquery-1.9.1.js"></script>
<script type="text/javascript" src="/SiteAssets/mymsteams.js"></script>
<div id="mymsteams"></div>
$(document).ready(function () {
//var token = getParameterByName['access_token'];
var token = getParameterByName('access_token');
if (token == null || token == undefined) {
// On Page load or if token not available on the URL.
requestToken();
}
else {
// Token available, extract the data
$.ajax({
url: "https://graph.microsoft.com/beta/me/joinedTeams",
type: "GET",
headers: { "Authorization": "Bearer " + token },
success: function (data) {
// Extract the data
console.log(data);
var myTeams = "";
for (var i = 0; i < data.value.length; i++) {
if (data.value[i].displayName != null) {
myTeams += "<p>" + data.value[i].displayName + "</p>";
}
}
$("#mymsteams").append(myTeams);
},
error: function (sender, args) {
console.log("error");
}
});
}
});
// Extract the access token from the URL
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&#]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
// Get the token using the redirect URL
function requestToken() {
var clientId = '9921bb32-b288-465b-9f74-11222f28534a';
var replyUrl = 'https://nakkeerann.sharepoint.com/Pages/trainingpage.aspx';
var resource = "https://graph.microsoft.com";
var authServer = 'https://login.microsoftonline.com/nakkeerann.onmicrosoft.com/oauth2/authorize?';
var responseType = 'token';
var url = authServer +
"response_type=" + encodeURI(responseType) + "&" +
"client_id=" + encodeURI(clientId) + "&" +
"resource=" + encodeURI(resource) + "&" +
"redirect_uri=" + encodeURI(replyUrl);
// Redirect to the URL, which will have access token on successful authentication
window.location = url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment