Reading a SharePoint List in SharePoint Online Cross-Site Collection

648 views Asked by At

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.

1

There are 1 answers

0
karan khetan On

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();