I have a SharePoint 2013 SharePoint Hosted app, and I've got some code that works swimmingly well when it's pointed to the host site collection. Part of the requirement is to have it point to a user specified site collection. When I specify another site collection in my farm, it errors:
Access denied. You do not have permission to perform this action or access this resource
Is this possible? I'm guessing this is a security thing in the app model.
Update Adding some code, since it sounds like this should work. Also my app has Tenant Write permissions.
var reqExecutor = new SP.RequestExecutor(appWebUrl);
var url = appWebUrl + "/_api/SP.AppContextSite(@TargetSite)/web/lists/getByTitle(@TargetLibrary)/RootFolder/folders(@TargetFolderName)/files/" +
"add(url=@TargetFileName,overwrite='true')?" +
"@TargetSite='" + targetSiteUrl + "'&" +
"@TargetLibrary='" + targetLibrary + "'&" +
"@TargetFileName='" + fileName + "'&" +
"@TargetFolderName='" + folderName + "'";
console.log('uploadDocToLibrary ' + url);
reqExecutor.executeAsync({
url: url,
method: "POST",
headers: {
"Accept": "application/json; odata=verbose"
},
contentType: "application/json;odata=verbose",
binaryStringRequestBody: true,
body: fileData,
success: function (x, y, z) {
console.log("Successful upload, updating metadata");
getDocumentListItem(libraryName, fileName);
},
error: function (x, y, z) {
updateDivStatus('Error: ' + x.statusText);
console.log(x.body + " " + z);
}
});
Another Update I switched from REST to CSOM, just to see, and I'm still getting access denied.
function getTargetSiteName() {
var context = new SP.ClientContext(appWebUrl);
var hostContext = new SP.AppContextSite(context, targetSiteUrl);
var web = hostContext.get_web();
context.load(web);
context.executeQueryAsync(
function () {
alert(web.get_title());
},
function (sender, args) {
alert(args.get_message());
});
}
Update Appears to be a bug or a limitation with SharePoint Hosted apps... testing out a provider hosted instead.