Accessing a list from app web on host site generates access denied
-
10-12-2019 - |
Question
I am trying to get title of a list which is located on Host Site from an SharePoint hosted app by using csom (javascript). I have added permission on Manifest file and chose the list but still getting Access denied on executeQueryAsync. here is my code:
hostweburl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));
var hostcontext = new SP.ClientContext(hostweburl);
var oList = hostcontext.get_web().get_lists().getByTitle('SliderList');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml(' som query to get proper items')
this.collListItem = oList.getItems(camlQuery);
hostcontext.load(collListItem);
hostcontext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
it does not go to either onQuerySuccess or onQueryFailed, it says Access denied on SP_ClientRuntimeContext$executeQueryAsync, sp.runtime.debug.js, line 3103
Solution
You actually need to generate a SP.ClientContext for the app web and a SP.AppContextSite for the host web. To get a reference to a list in the host web, try the following:
// Get the ClientContext for the app web
clientContext = new SP.ClientContext.get_current();
// Use the host web URL to get a parent context - this allows us to get data from the parent
hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
parentCtx = new SP.AppContextSite(clientContext, hostweburl);
parentWeb = parentCtx.get_web();
parentList = parentWeb.get_lists().getByTitle("SliderList");
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange