How to impersonate Farm Administrator in server object model?

909 Views Asked by At

We are using SharePoint Foundation 2010. We are creating content database and site collection using server object model. We have more than 1000 users in SharePoint. As content database and site collection creation are administartive task, only farm administartor can do that. In our case any user should be able to create content database and site collection with farm administartor account. Can we use SPUser or Is there any other way to do this ? Can you please provide me any code or link through which I can resolve the above issue ?

1

There are 1 best solutions below

4
On

It sounds like you might need to use the SPSecurity.RunWithElevatedPrivleges. This will allow you to run the code as if you were the farm administrator the application pool identity account. Be careful because it gives the user running the code full higher access, but it sounds like that is what you want.

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite topSite = new SPSite("URL_OF_YOUR_SITE_COLLECTION"))
    {
       //Any code you run here would run with the permissions of the application pool identity
    }
});