0

I've created a very simple SharePoint Hosted Add-in, its primary purpose is to create a list on the HostWeb, using the JSOM.

Code:

ExecuteOrDelayUntilScriptLoaded(function () {
var scriptbase = _spPageContextInfo.siteAbsoluteUrl + "/_layouts/15/";

$.getScript(scriptbase + "SP.RequestExecutor.js", function () {

    $("#CreateStuff").click(function () {
        var site = "https://myHostWeb.sharepoint.com/sites/dev";
        var context = new SP.ClientContext(site);
        var factory = new SP.ProxyWebRequestExecutorFactory(site);
        context.set_webRequestExecutorFactory(factory);

        var web = context.get_web();
        var listcreation = new SP.ListCreationInformation();
        listcreation.set_title("MyList1");
        listcreation.set_templateType(SP.ListTemplateType.genericList)
        var list = web.get_lists().add(listcreation);
        context.load(list);           
        context.executeQueryAsync(onQuerySucceeded, onQueryFailed);

        function onQuerySucceeded() {
            alert("Created");
        }

        function onQueryFailed(sender, args) {
            alert('Request failed. ' + args.get_message() +
                '\n' + args.get_stackTrace());
        }
    });
});
}, "sp.js");

I've tried many different ways, and I'm always getting a vague error when it hits OnQueryFailed.

Request Failed, and a correlation ID....

I've hardcoded my hostweb, just to make sure the URL was right... and I've tried using the SP.RequestExecutor, and the normal way of getting the client context.

The user I'm logged in with, is a site collection administrator on the hostweb.

Any ideas? Feel like I'm missing something obvious.

** Updated Code:

var context = SP.ClientContext.get_current();
        var appContextSite = new SP.AppContextSite(context, site);

        var oWebsite = appContextSite.get_web();
        var oList = oWebsite.get_lists();

        var listcreation = new SP.ListCreationInformation();
        listcreation.set_title("MyList2");
        listcreation.set_templateType(SP.ListTemplateType.genericList)
        var list = oList.add(listcreation);
        context.load(list);           
        context.executeQueryAsync(onQuerySucceeded, onQueryFailed);

With the above modified code, I'm now getting an access denied error. Too confirm, the logged in account is a site collection admin on the host web. Any Ideas?

1

From Work with host web data from JavaScript in the add-in web

Use var hostWebContext = new SP.AppContextSite(clientContext, hostWebURL); to get ClientContext for working with HostWeb

  • Thanks, changing the way the context is created is giving me a proper error message - access denied. My account has full permissions (Site Collection Admin). Any ideas why I'm getting this error? Thanks – user1106196 May 30 '18 at 10:55
  • What permission have you given the add-in? – Per Jakobsen May 30 '18 at 11:17
  • The App is granted (Site Collection: Full Control). The code works fine if I create the list on the App Web, but as soon as I switch back to trying to create on the HostWeb, I run into this permissions blocker. – user1106196 May 30 '18 at 11:52
  • Your updated code work for me. Make sure that App has been given permissions, by going to "Apps in Testing" | ... (next to app)| Manage Permissions | Trust it – Per Jakobsen May 30 '18 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.