1

I have this SharePoint hosted app on SharePoint Online. In this app, I am able to read the data from any SharePoint List/Library using the REST calls. However, when I try to fetch the items from a external list, I keep getting the Access denied error.

This external list is pulling the data from the Azure SQL Server using the BCS model. I can view all the data when I click on the List in the browser, but unable to fetch the same using the REST APIs.

Here's what I have tried so far:

  • On the Azure side, I have added an ip range of, 0.0.0.0 to 255.255.255.255 to the sql server's firewall. (Again, it's a test envr so I just gave everything.) :)
  • Have also configured the, 'Manage connections to online services' for BCS on the -admin page.
  • For the app, have given it the read permission for BCS.

  • On the 'Do you trust this app' page, I am selecting the given BCS connection also. But when the rest call is being executed it's throwing the access denied exception!

Here's my code snippet:

var headers = {
    "Accept": "application/json;odata=verbose",
    "X-RequestDigest": $("#__REQUESTDIGEST").val(),
    "content-type": "application/json; odata=verbose"
};

//First tried GETITEMS
url = appWebUrl + "/_api/Sp.AppContextSite(@target)/Web/Lists(guid'" + listID + "')/getitems?$select=" + fields + "&@target='" + hostWebUrl + "'" ;

//Also tried ITEMS
url = appWebUrl + "/_api/Sp.AppContextSite(@target)/Web/Lists(guid'" + listID + "')/items?$select=" + fields + "&@target='" + hostWebUrl + "'" ;

var payload = {
    'query': {
        '__metadata': { 'type': 'SP.CamlQuery' },
        'ViewXml': '<View><Query>' + query.ViewQuery + '</Query></View>'
    }

var ajaxOptions =
{
    url: url,
    method: 'GET',
    headers: headers,
    success: function (data) {
        if (data.body) {
            var jsonObject = JSON.parse(data.body);
            dfd.resolve(jsonObject);
        }
        else
            dfd.resolve();
    },
    error: function (sender) {
        dfd.reject(sender);
    }
};

ajaxOptions.body = JSON.stringify(payload);

var executor = new SP.RequestExecutor(appWebUrl);
executor.executeAsync(ajaxOptions);

Following is the permission set given to the app:

<AppPermissionRequests>
    <AppPermissionRequest Scope="http://sharepoint/content/sitecollection/web" Right="Write"/>
    <AppPermissionRequest Scope="http://sharepoint/bcs/connection" Right="Read" />
</AppPermissionRequests>

My question here is, is it possible to pull the BCS data, using REST APIs in a SharePoint hosted app? Plz advice.

Your Answer

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

Browse other questions tagged or ask your own question.