I try to get items from a list with JSOM. But I get a System.UnauthorizedAccessException with error code 2147024891. If I navigate and authenticate to the site with a browser I am able to access the list without problems. So I think I have the correct permissions.
Here is my code:
function getClientContextForSiteURL() {
var siteUrl = "/sites/mysite";
return new SP.ClientContext(siteUrl);
}
function getList(context) {
return context.get_web().get_lists().getByTitle('Listname');
}
var clientContext = getClientContextForSiteURL();
var oList = getList(clientContext);
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml("<View><Query><Where><Geq><FieldRef Name=\'ID\'/>'" +
"<Value Type=\'Number\'>1</Value></Geq></Where></Query><RowLimit>10</RowLimit></View>");
sharePointCollectionListItems = oList.getItems(camlQuery);
clientContext.load(sharePointCollectionListItems);
clientContext.executeQueryAsync(
Function.createDelegate(this, onQuerySucceeded),
Function.createDelegate(this, onQueryFailed));