1

I have a SPUser which is site collection admin and included in the Owners group, and i am getting access denied error when to get the Documents library, knowing that library takes its permissions by inheritance from the site itself. The most annoying part is when i log to the site with this user i can open the library and download its files.

The main issue occurred in a custom Web Api that uses the same AppPool as the site. I also tried to test the case in a console app and i faced the same result.Noting the i am logging to the server with the same user of the site AppPool so the console app is runs with System Account which means the runwithelevated works, anyways the error occurred on GetList as mentioned in the commented code below

Here is the console app code sample:

var ListName = "Documents";
SPUserToken userToken = null;

//Getting the userToken to impersonate it
SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite(SiteURL))
    {
        using (SPWeb web = site.OpenWeb())
        {
            web.AllowUnsafeUpdates = true;
            site.CatchAccessDeniedException = false;
            userToken = web.GetUserToken("mydomain\\myusername");
            site.CatchAccessDeniedException = true;
            web.AllowUnsafeUpdates = false;

        }
    }
});

//Accessing the site with this user so he just see what he should see
using (SPSite site = new SPSite(SiteURL, userToken))
{

    using (SPWeb web = site.OpenWeb())
    {
        web.AllowUnsafeUpdates = true;

        SPList list = web.GetList(ListName);//Here is the error

        var items = list.GetItems();

        web.AllowUnsafeUpdates = false;

    }

}

The error is:

An unhandled exception of type 'System.UnauthorizedAccessException' occurred in Microsoft.SharePoint.dll

Additional information: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

Stack trace

at Microsoft.SharePoint.SPGlobal.HandleUnauthorizedAccessException(UnauthorizedAccessException ex)
at Microsoft.SharePoint.Library.SPRequest.GetMetadataForUrl(String bstrUrl, Int32 METADATAFLAGS, Guid& pgListId, Int32& plItemId, Int32& plType, Object& pvarFileOrFolder)
at Microsoft.SharePoint.SPWeb.GetList(String strUrl)
at SPTest.TestCls.GetUserContent(String SiteURL) in c:\Projects\SPTest\SPTest\SPTest\TestCls.cs:line 1155
0

The error could be misleading, because it requires the "The server-relative URL to the root folder of a list, such as /sites/sitecollection/subsite/Lists/Announcements." . Try the following:

  1. SPList spList = spWeb.Lists.TryGetList("Site Assets"); // if you only have the display name of the list; OR
  2. SPList wfList = spWeb.GetList((spWeb.ServerRelativeUrl + "/Lists/WorkflowTasks/").Replace("//", "/")); // If you have the RootFolder path

good luck

  • thank you for the hint. But it's not a Url issue because when using another user it works successfully. – amenways May 26 '16 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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