0

Goal

Access Sharepoint via JSOM from within an Office 365 Outlook Add-in.

Problem

I tried multiple things, most of them lead to a "403 - forbidden".

Code

 // 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());
}
Jordumus
  • 2,763
  • 2
  • 21
  • 38
  • Are you sure $.getScript() is working? If it does load the script in Office.Initialize(), then where in the scope of memory does it place it? Is that place still accessible in execOperation()? Maybe place it in a global variable so you know you still have it. – jmbmage Aug 07 '18 at 14:22
  • @jmb.mage The script enters the execOperation, so the `$.getScript()` should be working. `new SP.ClientContext(*url*)` works as well, but as soon as I do `ExecuteQueryAsync`, it gives an error. (403) – Jordumus Aug 07 '18 at 14:27
  • This sounds like a server permissions issue. Where is `ExecuteQueryAsync` attempting to execute the script and does the current User or acting User have permissions to execute the script there? – jmbmage Aug 07 '18 at 14:34
  • @jmb.mage the connected user in outlook is a full-admin user. (It's a Microsoft Demo environment, so it's "Megan B"). – Jordumus Aug 07 '18 at 14:37
  • Does this apply? [Security considerations of custom scripts](https://learn.microsoft.com/en-us/sharepoint/security-considerations-of-allowing-custom-script) – jmbmage Aug 07 '18 at 14:43

0 Answers0