2

I'm using SharePoint 2013 and I wrote an AppPart that needs items of a list other than it's host site collection. But it keeps popping up an error indicating that my request has been failed. Access denied!!

Here is my Code in App.js for query the list in another sitecollection:

context = new SP.ClientContext(appweburl);
factory = new SP.ProxyWebRequestExecutorFactory(appweburl);
context.set_webRequestExecutorFactory(factory);
appContextSite = new SP.AppContextSite(context, "http://pouyanserver/sites/Authoring");
this.web = appContextSite.get_web();
mylist = this.web.get_lists().getByTitle('Employees');
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><RowLimit>10</RowLimit></View>');
var collListItem = mylist.getItems(camlQuery);
context.load(mylist);
context.load(collListItem);
context.executeQueryAsync(
Function.createDelegate(this, successHandler),
Function.createDelegate(this, errorHandler)
);

I have tried these solutions but neither of these worked:

Solution 1:

In the AppManifest.xml , In permissions tab I've added SiteCollection and tenant as scopes who has full control.

Solution 2: In the AppManifest.xml code, I've added an appPrincipal as follows:

  <AppPrincipal>
     <Internal AllowedRemoteHostUrl="http://pouyanserver/sites/authoring"/>
  </AppPrincipal>

What do you think ?

1 Answer 1

10

You can find a great article that explains the cross-domain / cross-site collection issues and how to resolve them on the MSDN: Solving cross-domain problems in Apps

In the link above the following explanation can be found:

Cross-site collection calls

A different problem that is often lumped into the same cross-domain category is issuing calls to SharePoint across sites or across site collections. For example, an app installed at http://contoso.sharepoint.com/site1 wants to retrieve a list from http://contoso.sharepoint.com/site2.

On the surface this looks like a cross-domain problem, but it is not. It's actually a traversal problem inside SharePoint resources. What you really want is for your app to talk to your allowed endpoint (your app web) and from there you want to internally proxy the call to a different site collection.

So, does that mean that you are out of luck if you want to use JavaScript? Nope, we actually have a pretty handy object for you named AppContextSite. All you have to do is set AppContextSite to point to the target web you want to talk to. Please note that in order for cross-site collection calls to work your app must be deployed as a tenant scoped app by an administrator; this is currently a security restriction of the API. Here is the syntax:

JSOM Syntax:

var ctx = new SP.ClientContext(appWebUrl);
var appContextSite = new SP.AppContextSite(ctx, targetUrl);
ctx.Load(appContextSite.get_web());

Example:

var ctx = new SP.ClientContext("http://contoso-5334ef4b86c8ea.sharepointonline.com/app1"); 
//Get this from tokens instead of hardcoding it. 
var appContextSite = new SP.AppContextSite(ctx, http://contoso.sharepointonline.com/anothersite); 
//Get this from user input or context tokens. 
ctx.Load(appContextSite.get_web());
5
  • 2
    Can you please extend your answer with the good parts from your link, to make it more useful even if the link breaks? :) Nov 19, 2013 at 12:50
  • Ok I content duplicate the required part. Right ? Nov 19, 2013 at 13:05
  • Should be OK if you keep the link :) It look great now Nov 19, 2013 at 13:25
  • Can you do this in C# CSOM ? Jan 15, 2014 at 21:23
  • The original article and this post suggest that you can access data across SITE COLLECTIONS, but my tests prove otherwise as it only works cross domain within the equivalent Collection. Can anyone confirm cross SITE COLLECTION success? I get a 401 error regardless of the method attempted. Jan 20, 2016 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.