Say one of your users sends you a screenshot proving that they do not have access to a web site to which, however, you’ve specifically granted them access. What can the reason be? How can a site collection administrator not be able to access a web site?
One quick piece of advice is this: check your web parts. Do they fetch data from some other web site, site collection, web service, something?
1. A thing to remember is that web parts run with the current user’s credentials, so, if they’re expected to fetch data from another location, if the current user does not have access to that location, the site will display this error message.
So, what do you do when you absolutely can’t grant the user access to the site where the data is being fetched from? You can either impersonate or use a method known as RunWithElevatedPrivileges (stick everything inside an SPSecurity.RunWithElevatedPrivileges(delegate(){}) structure – notice that the brackets includes the entire code that needs to be run with elevated privileges). Like so:
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(web.Site.ID))
{
// implementation details omitted
}
}); //this is the method to use when you don’t have a named method that needs to be run. If you only need to execute some portion of your code with elevated privileges, this is the way to go.
Another way to utilise RunWithElevatedPrivileges would be to first create an instance of the SPSecurity.CodeToRunElevated and then pass that in the RunWithElevatedPrivileges method. Like so:
SPSecurity.CodeToRunElevated elevateCode = new SPSecurity.CodeToRunElevated(NameOfMethodToRun);
SPSecurity.RunWithElevatedPrivileges(elevateCode);
2. A fellow developer came across this error and the actual reason was completely different. We need to keep in mind the framework architecture we’re targeting in our builds. For example, console SharePoint applications default to x86 architecture. Unfortunately, the error message is not very helpful, but you always need to target the 64-bit architecture when working with SharePoint.