Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Sharepoint
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:
 
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 22-May-12 21:47pm
Edited 22-May-12 22:11pm
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  
Comments
gyana.dash123@gmail.com at 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 at 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?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You SPSite variable is outside the elevated block. It also needs to be included, like this:
SPSecurity.RunWithElevatedPrivileges(delegate() {
    using (SPSite site = new SPSite(SPContext.Current.Site.ID)) {
        using (SPWeb web = site.OpenWeb(SPContext.Current.Site.RootWeb.ID)) {
            // ...
        }
    }
});
Good luck!
  Permalink  
Comments
gyana.dash123@gmail.com at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 288
1 Maciej Los 260
2 _Amy 230
3 Manfred R. Bihy 200
4 CHill60 180
0 OriginalGriff 7,445
1 Sergey Alexandrovich Kryukov 6,177
2 Maciej Los 3,774
3 Peter Leow 3,478
4 CHill60 2,702


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 23 May 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100