Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to impersonate Farm Administrator in server object model?

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 ?

like image 370
Shailesh Jaiswal Avatar asked Nov 03 '22 23:11

Shailesh Jaiswal


1 Answers

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
    }
});
like image 101
Peter Jacoby Avatar answered Nov 09 '22 11:11

Peter Jacoby