I'm following the sample code located at https://nickvandenheuvel.eu/tag/adal-js/ but my code fails with the SharePoint connection with a ADAL Error AADSTS500011.
The error message states "The resource principal named https://my.sharepoint.com/sites/mysite was not found in the tenant named mytenant.onmicrosoft.com. This can happen if the application has not been installed by the administrator of the tenant or consented to by any user in the tenant. You might have sent your authentication request to the wrong tenant."
My code is exactly the same as the article. My thought is maybe something needs to be done with my Azure registration for my SharePoint site, or, the code is outdated with how Office 365 and SharePoint now works.
// Assign variables
var variables = {
// Domain of Azure AD tenant
azureAD: "tenantname.onmicrosoft.com",
// ClientId of Azure AD application principal
clientId: "11111111-1111-1111-1111-111111111111",
// GUID of SharePoint list
listId: "22222222-2222-2222-2222-222222222222",
// Name of SharePoint tenant
sharePointTenant: "tenantname"
}
// Create config and get AuthenticationContext
window.config = {
tenant: variables.azureAD,
clientId: variables.clientId,
postLogoutRedirectUri: window.location.origin,
endpoints: {
graphApiUri: "https://graph.microsoft.com",
sharePointUri: "https://" + variables.sharePointTenant + ".sharepoint.com",
},
cacheLocation: "localStorage"
};
var authContext = new AuthenticationContext(config);
var user = authContext.getCachedUser();
if (!user) {
authContext.login();
}
// Get OneDrive documents of current user with AuthenticationContext of Graph API
authContext.acquireToken(config.endpoints.graphApiUri, function (error, token) {
if (error || !token) {
console.log("ADAL error occurred: " + error);
return;
}
else {
var filesUri = config.endpoints.graphApiUri + "/v1.0/me/drive/root/children";
$.ajax({
type: "GET",
url: filesUri,
headers: {
"Authorization": "Bearer " + token
}
}).done(function (response) {
console.log("Successfully fetched files from OneDrive.");
var items = response.value;
for (var i = 0; i < items.length; i++){
console.log(items[i].name);
$("#OneDrive").append("<li>" + items[i].name + "</li>");
}
}).fail(function () {
console.log("Fetching files from OneDrive failed.");
});
}
});
// Get SharePoint documents of list with AuthenticationContext of SharePoint
authContext.acquireToken(config.endpoints.sharePointUri, function (error, token) {
if (error || !token) {
console.log("ADAL error occurred: " + error);
return;
}
else {
var listUri = config.endpoints.sharePointUri + "/_api/web/lists('" + variables.listId + "')/items?$select=Title";
$.ajax({
type: "GET",
url: listUri,
headers: {
"Authorization": "Bearer " + token,
"accept": "application/json;odata=verbose"
}
}).done(function (response) {
console.log("Successfully fetched list from SharePoint.");
var items = response.d.results;
for (var i = 0; i < items.length; i++){
console.log(items[i].Title);
$("#SharePoint").append("<li>" + items[i].Title + "</li>");
}
}).fail(function () {
console.log("Fetching list from SharePoint failed.");
});
}
});
I can say that the code does work for the rest API and OneDrive. It also works for getting the user information from Azure AD. The error message only appears when I try to interact with the SharePoint site. I'm not sure if the code isn't up to date with how ADAL interacts with SharePoint, or if there is something in Azure AD that needs to be configured. Since the interaction with OneDrive seems to work I assume Azure AD isn't the issue (since I can't recall having to set anything in Azure AD for OneDrive).