SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In my code I'm running under elevated privileges, to access other Web applications. But I get a security error when the web application user can't access the database (when ever I try to access a SPWeb object).

Is there a way to make sure (in code) that a user has access to the Content database of a web application before trying to access sites in a site collection on that web application?

share|improve this question
up vote 2 down vote accepted

Even if you're using elevated privileges, that impersonates the account running the current web application only and wont elevate your privileges to other web applications (unless they share the same app-pool account.

To the best of my knowledge you want to find out whether the current app-pool account is the same as the one running the target SPWeb. You do this by checking the SPWebApplication.ApplicationPool of the current and target sites.

Alternatively, do what you're doing but wrap it with a try..catch block and if you fail to access the SPWeb then you know you don't have access.

Finally, you could try using web services and see if you get a status-code back (i.e. Access Denied), in which case you'd have your answer.

share|improve this answer
Thank you. I tried the try catch, but somehow that is to late (no way I can access that page if the user is wrong). I think the web service method is the way to go, as I have no control over the account users on the farm. – gaotter Mar 6 '11 at 17:29

Your Answer


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.