Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm using the following code to let users without full access rights on my site deleting it through a custom layouts page (delete site button):

  SPSecurity.RunWithElevatedPrivileges(delegate() {
         using (SPSite Site = SPContext.Current.Site) {
                 Site.AllowUnsafeUpdates = true;
                 Site.Delete();
                 Site.AllowUnsafeUpdates = false;
         }
        });

I get an access denied for the non site collection user trying to do this:

Deleting the site at http:// and not deleting AD accounts.
Unknown SPRequest error occurred. More information: 0x80070005
An SPRequest object was not disposed before the end of this thread.

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

You are passing a reference of the existing SPSite which also passes the current security context. Instead try:

  SPSecurity.RunWithElevatedPrivileges(delegate() {
         using (SPSite Site = new SPSite(SPContext.Current.Site.ID)) {
                 Site.AllowUnsafeUpdates = true;
                 Site.Delete();
                 Site.AllowUnsafeUpdates = false;
         }
        });
share|improve this answer
    
Thanks a lot Sig, this solved the issue! –  Wim Hill Nov 22 '11 at 17:53
add comment

Your Answer

 
discard

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.