Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am trying to access a list via LINQ to SharePoint that is part of a different site collection. I am able to create the DataContext object for the other site but when I try to access the DataContext.ListName property I get:

    System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
 at Microsoft.SharePoint.SPGlobal.HandleUnauthorizedAccessException(UnauthorizedAccessException ex)
 at Microsoft.SharePoint.Library.SPRequest.GetListsWithCallback(String bstrUrl, Guid foreignWebId, String bstrListInternalName, Int32 dwBaseType, Int32 dwBaseTypeAlt, Int32 dwServerTemplate, UInt32 dwGetListFlags, UInt32 dwListFilterFlags, Boolean bPrefetchMetaData, Boolean bSecurityTrimmed, Boolean bGetSecurityData, Boolean bPrefetchRelatedFields, ISP2DSafeArrayWriter p2DWriter, Int32& plRecycleBinCount)
 at Microsoft.SharePoint.SPListCollection.EnsureListsData(Guid webId, String strListName)
 at Microsoft.SharePoint.SPListCollection.GetListByName(String strListName, Boolean bThrowException)
 at Microsoft.SharePoint.Linq.Provider.SPServerDataConnection.GetSPList(String url, String listName)
 at Microsoft.SharePoint.Linq.Provider.SPServerDataConnection.GetList(String url, String listName)

Do you know what permissions are needed to grant access to the list object? Does it need to be granted to IUSR?

I am able to get this to work with RunWithElevatedPrividges but I would rather not use that everywhere I need to access a list on a different site collection.

share|improve this question

3 Answers 3

As an alternative to Ryans answer (which works great for SharePoint 2010).

As of SharePoint 2013 there's an constructor overload for DataContext with the parameter crossSiteCollection. So instead of temporarily clearing the HttpContext, add a partial class with the constructor for your DataContext (or modify your SPMetal-generated classes directly) and use that one when querying another site collection.

public partial class {YourList}DataContext : DataContext
{
    public {YourList}DataContext(string requestUrl, bool crossSiteCollection) :
        base(requestUrl, crossSiteCollection)
    {
        this.OnCreated();
    }
}

The reason this works is because SharePoint always creates a new SPSite and SPWeb object if this parameter crossSiteCollection is true instead of reusing SPContext.Current.Site (from HttpContext.Current, which solved it for 2010).

share|improve this answer
up vote 0 down vote accepted

I was unable to find a workaround for this. As using LINQ to SharePoint across site collections is "not supported". I am then required to run the get_list function with elevated privileges when opening lists from other site collections.

Here is an example of the function I used:

    public void GetCompaniesList(CompaniesDataContext cdc, PipelineDataContext dc)
    {
        HttpContext current = HttpContext.Current;
        HttpContext.Current = null;
        try
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                companies = cdc.GetList<companies.CompaniesItem>("Companies");
                HttpContext.Current = current;
                DoQuery(cdc,dc);
            });
        }
        finally
        {
            HttpContext.Current = current;
        }

    }
share|improve this answer

Unless the current user under who this call will be made has at least read access on the target site collection, you'll always run into troubles.

If you're target site collection is running a on separate web application which has its own application pool, you'll also need to ensure that they share the same "application pool" account identity as running with elevated privileges will simply execute the current operation under the application pool identity.

For trusty cross site collection data mining, you should rely on the search service application (with a crafted search scope) or impersonate an account that has access on all target site collections.

Hope it helps.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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