2

I am creating some SharePoint hosted apps, and have decided to use Napa Office 365 Developer Tools for ease of setup, and then export the projects to Visual Studio at a later date if required.

I have a problem calling cross domain to retrieve information from a list on the SPHostWeb.

My code is from the following example project

(function () {

    'use strict';

    var user = context.get_web().get_currentUser();

    // This code runs when the DOM is ready and creates a context object which is 
    // needed to use the SharePoint object model
    $(document).ready(function () {
        getHostWebTitle();
    });

    function getHostUrl () {
        var args = window.location.search.substring(1).split("&");
        var spHostUrl = "";
        for (var i = 0; i < args.length; i++) {
            var n = args[i].split("=");
            if (n[0] == "SPHostUrl")
                spHostUrl = decodeURIComponent(n[1]);
        }
        return spHostUrl;               
    }

    function getAppWebUrl () {
        var appWebUrl = "";
        var args = window.location.search.substring(1).split("&");
        for (var i = 0; i < args.length; i++) {
            var n = args[i].split("=");
            if (n[0] == "SPAppWebUrl")
                appWebUrl = decodeURIComponent(n[1]);
        }
        return appWebUrl;
    }

    // Get Host web title
    function getHostWebTitle() {
        var spHostUrl = getHostUrl();
        var appWebUrl = getAppWebUrl();

        //Load Libraries
        var scriptbase = spHostUrl + "/_layouts/15/";
        var list = "Links";     

        jQuery.getScript(scriptbase + "MicrosoftAjax.js").then(function (data) {
            return jQuery.getScript(scriptbase + "SP.Runtime.js");
        }).then(function (data) {
            return jQuery.getScript(scriptbase + "SP.js");
        }).then(function (data) {
            return jQuery.getScript(scriptbase + "SP.RequestExecutor.js");
        }).then(function (data) {

            var executor = new SP.RequestExecutor(appWebUrl);
            executor.executeAsync(
                    {
                        url:
                            appWebUrl +
                            "/_api/SP.AppContextSite(@target)/web/lists/getByTitle('" + list + "')/items?$select=Title&@target='" +
                            spHostUrl + "'",
                        method: "GET",
                        headers: { "Accept": "application/json; odata=verbose" },
                        success: function (data) {
                            console.log('success');
                        },
                        error: function (data) {
                            console.log('fail');
                        }
                    }
                );

        });
    }   

}());

My code follows all the way through to the executor.executeAsync request, however comes back with the following error: value:

Access denied. You do not have permission to perform this action or access this resource.

1 Answer 1

3

I think you need to provide read access to your app on your list. Due to this it is throwing access denied error.

See the MSDN link for detail information on App permission http://msdn.microsoft.com/en-us/library/office/fp142383(v=office.15).aspx

4
  • 1
    Your spot on in regards to permission (clearly) - but it appears that he is probably missing the "site collection" permission. Without that you can't access the host web. In the NAPA tools - open up properties (small wrench icon) and you will see the permission tab where you can set App permission requirements. Commented Jan 12, 2015 at 5:02
  • 1
    in the App Manifest file, have you set the permission of the app, if you are retrieving the List data you need to mention permissions as Read, Then while installing the app it will ask you to choose the List. Select the there from which you need to get the data. Commented Jan 12, 2015 at 5:38
  • thanks for your feedback, this seems to be the likely issue, will attempt this tomorrow and let you know how it goes.
    – svnm
    Commented Jan 12, 2015 at 10:53
  • Worked perfectly once I set the List permission to Write, Thanks
    – svnm
    Commented Jan 21, 2015 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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