Click here to Skip to main content
15,896,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I want to create content database programatically. I have tried using SPSecurity.RunWithElevatedPrivileges code but it gives Access Denied exception. The site is created using claim based authentication.

Below is my code:
C#
SPSite site=SPContext.Current.Site;
SPSecurity.RunWithElevatedPrivileges(delegate()
  {
      using (SPSite elevatedSite = new SPSite(site.ID))
        {
            SPWebApplication elevatedWebApp = elevatedSite.WebApplication;
            elevatedWebApp.ContentDatabases.Add(serverName, dbName, null, null, 0, 1, 1);
        }
  });

Thanks
Posted
Updated 22-May-12 22:11pm
v3

Generally, the code will not run within elevated privilege if the object accessed was not created within the SPSecurity.RunWithElevatedPrivileges block OR the implementation is not giving privileges to right user account handling the code execution.

Look at similar discussions here:
Access denied when code is executed remotely as a wcf service[^]
Access denied within SPSecurity.RunWithElevatedPrivileges[^]
SPSecurity.RunWithElevatedPrivileges - an important point while using it in web context[^]
 
Share this answer
 
Comments
gyana.dash123@gmail.com 23-May-12 5:24am    
Hi Sandeep,

Thanks for your reply.

I have changed my code still it is throughing access denied error. But when i run this code using central admin site its working fine. May be the problem is FBA authentication. If you have any solution please send it.

Below is my code:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite elevatedSite = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb web = elevatedSite.OpenWeb(SPContext.Current.Site.RootWeb.ID))
{
SPContentDatabase currentSiteDatabase = web.Site.ContentDatabase;

SPWebApplication elevatedWebApp = web.Site.WebApplication;
elevatedWebApp.ContentDatabases.Add(currentSiteDatabase.Server, dbName, null, null, 0, 1, 1);
}
}
});

Thanks
E.F. Nijboer 23-May-12 6:35am    
You need to authorize the site to run with elevated privileges. Did you also check out the links of Sandeep Mewara?
You SPSite variable is outside the elevated block. It also needs to be included, like this:
C#
SPSecurity.RunWithElevatedPrivileges(delegate() {
    using (SPSite site = new SPSite(SPContext.Current.Site.ID)) {
        using (SPWeb web = site.OpenWeb(SPContext.Current.Site.RootWeb.ID)) {
            // ...
        }
    }
});

Good luck!
 
Share this answer
 
Comments
gyana.dash123@gmail.com 23-May-12 5:08am    
Hi
I have changed my code still it is throughing access denied error. But when i run this code using central admin site its working fine.

Below is my code:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite elevatedSite = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb web = elevatedSite.OpenWeb(SPContext.Current.Site.RootWeb.ID))
{
SPContentDatabase currentSiteDatabase = web.Site.ContentDatabase;

SPWebApplication elevatedWebApp = web.Site.WebApplication;
elevatedWebApp.ContentDatabases.Add(currentSiteDatabase.Server, dbName, null, null, 0, 1, 1);
}
}
});

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900