0

I have a BCS connector.

In a constructor of its service class I want to access a SharePoint list.

using (var site = new SPSite("My site URL")
{
    using (var web = site.RootWeb)
    {
        var list = web.Lists.TryGetList("My List"); //Getting value error: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))!
        ...
    }
}

But when I try to access the list like in the code example above, Im getting the Getting value error: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))! error.

The account I'm using for crawling is the same account I'm using myself for SharePoint administration so I'm pretty sure it has access to the list since I'm the one who created it?

What is the cause of this access denied error and how can I resolve it?

EDIT:

I trid using

SPSecurity.RunWithElevatedPrivileges(()=>{/*Here goes the code from the example above*/});

but it didn't changed a thing.

I'm still getting the same error.

However, when I tried the exact same code as in the first example in a console application on the same server it worked as expected.

1 Answer 1

0

You can run the code in SPSecurity.RunWithElevatedPrivileges method.

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (var site = new SPSite("My site URL")
    {
        using (var web = site.OpenWeb())
        {
            var list = web.Lists.TryGetList("My List");
            //more code
        }
    }   
});
1
  • I tried this. It certainly changes the account in which context the code is running to NT AUTHORITY\NETWORK SERVICE but then I'm still getting the same error. I edited the question.
    – Szybki
    May 28, 2019 at 19:23

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.