0

I have a function that runs in a provider-hosted application ( written in C# and using CSOM ) and accesses a list on my SharePointOnline tenant.

The function works exactly as intended as long as I access the provider-hosted application from the Site Collection in which I created the list.

public bool CanUserReadList(ClientContext context, string listId)
    {
        try
        {                
            if (string.IsNullOrWhiteSpace(listId)) return false;

            List list = context.Web.Lists.GetById(new Guid(listId));                                                             
            context.Load(list, a => a.EffectiveBasePermissions,a=> a.DefaultViewUrl);
            context.ExecuteQuery();
            return list.EffectiveBasePermissions.Has(PermissionKind.OpenItems);
        }
        catch (Exception e)
        {
            //Error thrown if accessing from different site collection to 
        }
    }

However if I access the provider-hosted application from a different Site Collection I cannot access the list ( I get a File Not Found ) exception.

How do I access a list contained in one site collection from another using the CSOM Microsoft.SharePoint.Client library?

How do I do a "Cross Site-Collection" read of a list in a CSOM/Provider-Hosted App?

"On-Prem" I would open the SPSite using the URL of the list ... but everything I have tried so far in CSOM/SharePointOnline has failed.

user3658298
  • 341
  • 1
  • 5
  • 15
  • I am not sure about Online environment. Have you tried creating context for site-collection in which list exists ? ClientContext context = new ClientContext("http://SiteUrl"); – Vaibhav Feb 14 '17 at 06:30
  • Yes I tried that - but that fails with an exception. Problem is that the user enters the provider-hosted App with a context created for the other Site Collection. I need to be able to create him a Context for the SiteCollection that contains the list OR somehow access the list using the current Context – user3658298 Feb 14 '17 at 22:02

1 Answers1

0

Try creating the context of the site-collection you are trying access.

ClientContext context = new ClientContext(“[Site URL]”);
Web web = context.Web;
context.Load(web);
context.ExecuteQuery();