目标
在Office 365 Outlook加载项中通过JSOM访问Sharepoint。
问题
我尝试了多种方法,其中大多数导致“ 403-禁止使用”。
代码
// The Office initialize function must be run each time a new page is loaded.
Office.initialize = function (reason) {
$(document).ready(function () {
var element = document.querySelector('.ms-MessageBanner');
messageBanner = new fabric.MessageBanner(element);
messageBanner.hideBanner();
loadProps();
//app.initialize();
//Load everything required for sharepoint
// Get the URI decoded URLs.
var hostweburl = "https://-tenant here-.sharepoint.com";
// The js files are in a URL in the form:
// web_url/_layouts/15/resource_file
var scriptbase = hostweburl + "/_layouts/15/";
// Load the js files and continue to
// the execOperation function.
$.getScript(scriptbase + "SP.Runtime.js",
function () {
$.getScript(scriptbase + "SP.js", execOperation);
}
);
});
};
// Function to execute basic operations.
function execOperation() {
//Assure it is running - Doesn't work: SP.SOD does not exist.
//SP.SOD.executeFunc('sp.js', 'SP.ClientContext', Office.initialize);
var context = new SP.ClientContext("https://-that same tenant-.sharepoint.com"); // I tired: Get_current(), but I get an error that there is no "current".
var web = context.get_web();
var user = web.get_currentUser();
context.load(user);
context.executeQueryAsync(Function.createDelegate(this, this.onGetUserQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
debugger;
}
function onGetListQuerySucceeded() {
console.log("Hello" + user.get_title());
console.log(user);
debugger;
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}