Cross Domain request SharePoint hosted app Office 365 Developer Tools
Question
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.
Solution
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