1

I have a feature that deploys a site in a folder in the root of the parent site. When we want to uninstall this feature, the only way to remove the files it deploys is by programmaticaly remove them using the SPFolder.Delete() method. I want to do this using an Event Receiver which I developed. The problem is that when I try to uninstall the feature, the event receiver doesn't catch the Event and nothing happens. This is the class and method where I do this:

public class Feature1EventReceiver : SPFeatureReceiver
{    
  public override void FeatureDeactivating(SPFeatureReceiverProperties properties){
    //delete folders
  }
}

I'm suspecting the reason the event receiver doesn't catch the event is because the event receiver is in a different project than the feature, so my question is which of these file structures should I use?

Solution
|
+   Webpart (project)
    |
    +   Features (folder)
        |
        +   Web part (feature)
        +   EventReceiver (feature)

OR

Solution
|
+   Webpart (project)
    |
    +   Features (folder)
        |
        +   Web part (feature)
+   EventReceiver (project)
    |
    +   Features (folder)
        |
        +   EventReceiver (feature)

Let me know if you think the reason to fail in catching the event migt be other.

EDIT I have previously tried @Evariste layout but I always get a "this sandboxed solution has exceeded its daily resource usage quota" error when I try to deploy although the Solution Gallery looks like this: enter image description here This is how my project structure looks when I get that error: enter image description here

2

IMO, the only correct handling of this problem would be to have only one VS project, building into one single WSP package. And also: one feature only. The feature that provisions files should be the one that deletes them at deactivation. It does not make sense to activate F01 and deactivate F02 to undo what F01 did! Furthermore, how F02 would be activated in the first place!?

You then should have:

Solution
|
+   Webpart (project)
    |
    +   Features (folder)
        |
        +   Web part (feature) with EventReceiver

That's all. From Visual Studio, once the feature is created simply right-click on it and choose to add a Feature Event Receiver. The added feature class has an Id attribute, that's automatically wired-up with the parent feature (see it in the feature's properties Window) when the WSP is built.

  • Hi Evariste. Sorry for taking longer to respond. I tried that in the past but it failed. I've updated my post with further information. – Pedro Gordo Feb 5 '16 at 10:54
  • 1
    The "resource usage quota" error cannot be because of this design. It may happen because of Something "bad" in the code. Do you have ER code at feature activation? Also, the proposed design is not only OK from a technical PoV, but also logical considering you need to do something when the first feature is deactivated, not when another random one is... – Evariste Feb 5 '16 at 10:59
  • I thought it could be the code, so I commented the whole class and still got the same error. I agree with you regarding the layout. Is what indeed makes more sense, so it must be somethig else failing. I'll mark your post as the answer because this other problem seems outside of the scope of this post and I created a new post here in case you want to contribute: sharepoint.stackexchange.com/questions/169257/… – Pedro Gordo Feb 5 '16 at 11:17

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.