I'm struggling to find a resolution for this and am hoping somebody can help please?
If I debug against my top-level site (ie - set my site url to 'http://mysharepointserver/") and use JSOM to bring back the entries in a specified list within that top level site using the RESTful endpoint, all is good :-
var hostweburl;
var appweburl;
// 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 () {
getUserName();
hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
var scriptbase = hostweburl + "/_layouts/15/";
$.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest);
});
function execCrossDomainRequest() {
var listname = "InvoiceRecharges";
var executor = new SP.RequestExecutor(appweburl);
var target = hostweburl;
//target = target + "/sites/AnotherSiteCollection";
$('#TopSite').text(target);
executor.executeAsync(
{
url: appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('InvoiceRecharges')/items?@target='" + target + "'",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: successHandler,
error:errorHandler
}
);
}
function successHandler(data) {
var jsonObject = JSON.parse(data.body);
var listHTML = "";
var results = jsonObject.d.results;
for (var i = 0; i < results.length; i++) {
listHTML = listHTML + "<p>" + results[i].Title + " - (" + results[i].ItemCount + ")</p>";
}
document.getElementById("output").innerHTML = listHTML;
}
function errorHandler(data,errorCode,errorMessage) {
$('#output').text('Oops - ' + data + " - " + errorCode + " - " + errorMessage);
}However, I have a requirement to call in to a different sitecollection in the farm and read / write entries from a different list - for example :
http://mysharepointserver/sites/anothersitecollection
If I make modifications to my 'execCrossDomainRequest' function and uncomment the suffix to target as follows :
var target = hostweburl; target = target + "/sites/anothersitecollection";
- I receive error 1002 / Unauthorized as the returned errorCode and errorMessage in the failure routine. My HOSTWEB in this instance is the top level site of "http://mysharepointserver" and I can suffix this "target" with any subsites
in this same site collection .... however it all falls down when referencing a different site collection.
I've tried setting Tenant / FullPermissions in the App Manifest to no avail ....
Any help would be much appreciated (assuming it's even possible from a SharePoint-hosted app using JSOM with REST??)
Thanks
Steve