2

I have a FeatureReceiver class for a feature that deploy a couple of list instances. In the FeatureDeactivating event I want to remove those instances as well as other artifacts which get deployed during feature activation (web parts, page layouts and so on). I have the following piece of code

public override void FeatureDeactivating( SPFeatureReceiverProperties properties ) {
    SPSecurity.RunWithElevatedPrivileges( delegate() {
        SPWeb web = (SPWeb)properties.Feature.Parent;
        bool allowUnsafeUpdates = web.AllowUnsafeUpdates;
        web.AllowUnsafeUpdates = true;

        web.Lists["_MYLIST__"].Delete();

        web.Update();
        web.AllowUnsafeUpdates = allowUnsafeUpdates;
    } );
}

This code works very good when I am debugging it and the list get removed as expected. When this code run upon a stsadm command the list does not get removed.

I had a suspect about the SPSecurity.RunWithElevatedPrivileges block, which get executed, I think, in another thread, and so I've added a simple Thread.Sleep(5000) after that code block.

Now the feature is always removed :O

Because of the fact that the Sleep is not properly elegant, can somebody advise a better way to resolve this problem?

Thanks a lot for helping

  • What scope does the feature have? If you are able to deactivate the feature than you are already site owner and you don't need to use SPSecurity.RunWithElevatedPrivileges – Stefan Bauer Feb 10 '14 at 19:53
  • The feature has scope Web. I need to use that code block because the deploy on the production servers is not made by me. So I need to get sure that the code will run under the AppPool identity. – Lorenzo Feb 10 '14 at 19:55
  • 1
    It should run always under the user context. The admin / end user should be able to activate / deactivate the feature. With this you just bypass the built in security system which is not good I think but I might be wrong. – Stefan Bauer Feb 10 '14 at 19:59
  • You don't need to use RunWithElevatedPrivileges to do this - instantiate a new SPSite using the SystemAccount user token instead. Never use RunWithElevatedPrivileges if all you work with is SharePoint object model code in the delegate. – James Love Feb 11 '14 at 10:36
2

You should use Delete() method of SPWeb.Lists

    SPWeb webSite = properties.Feature.Parent as SPWeb;
    SPList myCustomList = mySite.Lists["MyCustomList"];
    mySite.Lists.Delete(myCustomList.ID);
    mySite.Update();

You should also create a new SPSite object while running a code as elevated privileges:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite(web.Site.ID))
    {
    // deletion code goes here...
    }
});
  • I think you have misunderstood the question. I do not have problems with the delete operation. – Lorenzo Feb 11 '14 at 11:04
  • You said you were having problem deleting the list using stsadm? – Falak Mahmood Feb 14 '14 at 8:22
  • Oh no. I wrote that the code is working great if I add the sleep. The problem I have encountered is related to the fact that SPSecurity.RunWithElevatedProvileges runs in another thread which finish after the FeatureDeactivating method. – Lorenzo Feb 14 '14 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.