Activate a feature using code (C# programming)

Jul 30, 2009 at 9:09 AM

Hi ,

    I have created a eventhandler, and i need to deploy it a feature. I have written the code to deploy the solution and even bind the eventhandler to any particular list through  C# code.

Now i need to activate the feature through C#.

I checked the link posted by nseven
 ,  the code is pretty straight forward

SPWeb web = new SPSite("").OpenWeb();

SPFeatureCollection featureCollect = web.Features;

featureCollect.Add(new Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"), true); //True to force activation

But i couldn't makeout where to place the code? and how to use it?

As I feel that this code needs to be running as soon as the solution is deployed on the site (ie., before binding the feature ) 

But i couldn't makeout where to place the code? and how to use it? Could anyone please guide me with this?

Aug 18, 2009 at 6:54 AM

There are some really nice tutorials out there, describing how to do what you trying to.

There are several ways for activating features, like the ONET.xml or - the other way around - activate them by code interaction or another feature or whatever.

So, there must be a reason to active your feature by code.
The most common way to do this might be the feature receiver code of the parent feature, if there is one.

But it occurs to me that the WSPBuilder discussion isn't the right place to discuss this.