This post will show you how to work with features through code. Using the API we can install, uninstall, activate and deactivate features.
Install a Feature.
To install a feature we first need to get a reference to our local farm.
SPFarm oFarm = SPFarm.Local;
Then all we have to do is add the feature to the FeatureDefinitions collection on the local farm. The first parameter if a relative path to the feature.xml file (relative to the Features directory. ex: MyFeature\feature.xml). The second parameter is the guid of the feature itself. The guid can be parsed from the feature.xml file. Or this can be the solution guid that the feature belongs to. There are 2 overloads to the Add method explained below.
oFarm.FeatureDefinitions.Add(<<feature definition object>>); (Add the feature based on a SPFeatureDefinition object to add).
oFarm.FeatureDefinitions.Add(<<feature.xml.>>, <<feature guid>>); (Adds a feature based on the feature.xml file and a solution guid).
oFarm.FeatureDefinitions.Add(<<feature.xml.>>, <<feature guid>>); (Adds a feature based on the feature.xml file and a solution guid and true to force the reinstall of a feature definition).
Uninstall a Feature.
To uninstall a feature we first need to get a reference to our local farm.
SPFarm oFarm = SPFarm.Local;
Then all we need to do is remove the feature from the FeatureDefinitions collection on the local farm. There are 4 overloads to the Remove method explained below.
oFarm.FeatureDefinitions.Remove(<<feature guid>>); (Removes the feature based on a specific feature guid).
oFarm.FeatureDefinitions.Remove(<<feature.xml>>); (Removes the feature based on a a relative feature.xml file).
oFarm.FeatureDefinitions.Remove(<<feature guid>>, <<true, false>>); (Removes the feature based on a specific feature guid, true to force the removal of the feature definition).
oFarm.FeatureDefinitions.Remove(<<feature.xml>>); (Removes the feature based on a a relative feature.xml file, true to force the removal of the feature definition).
NOTE: The above method will install the feature to the local farm. If the feature is scoped at the farm level it will also activate it at the farm level as well.
Activate a Feature on a Specific Site
To activate a feature on a specific site, you need to add it to that site. The first thing we need to do is get a SPSite object and open a SPWeb object
SPSite oSite = new SPSite("http://mysiteurl");
SPWeb oWeb = oSite.OpenWeb();
Now all we need to do is add the feature to the Features collection of that specific site. There are 2 overloads to the Add method explained below.
oWeb.Features.Add(<<guid>>); (Add the feature with the specific guid.).
oWeb.Features.Add(<<guid>>, <<true, false>>); (Add the feature with the specific guid, true to overwrite an existing feature with the same guid).
Deactivate a Feature on a Specific Site
To deactivate a feature on a specific site, you need to remove it from that specific site. The first thing we need to do is get a SPSite object and open a SPWeb object
SPSite oSite = new SPSite("http://mysiteurl");
SPWeb oWeb = oSite.OpenWeb();
Now all we need to do is remove the feature from the Features collection for that specific site. There are 2 overloads to the Remove method explained below.
oWeb.Features.Remove(<<guid>>); (Removes the feature with the specific guid.).
oWeb.Features.Remove(<<guid>>, <<true, false>>); (Removes the feature with the specific guid, true to force the operation event if there are errors).
NOTE: To activate are deactivate a feature that is scoped at a web application or site collection level, you work with the Features collection on a SPWebApplication object for a web application or a SPSite object for a site collection.
Continue reading if you want to see how to accomplish the same tasks in SWAT (SharePoint Work Acceleration Toolkit). You can find SWAT here http://www.idevfactory.com
Install a Feature
In Features window in SWAT, right click on the feature that you want to install and click Feature Manager.

On the Feature Manager window, click Install Feature. This will install the feature into your farm.

Uninstall a Feature.
In Features window in SWAT, right click on the feature that you want to install and click Feature Manager.

On the Feature Manager window, click UnInstall Feature. This will uninstall the feature into your farm.

Activate a Feature on a Specific Site
In the Farm Tree window, rick click on the site that you want to activate the feature on and click Show Site Details. This will load all the details for that site.

In the Site Details are, select the Site Features tab. This will show you all the features for that site that are activated and deactivated.

In the Site Features section, right click on the feature that you want to activate and click Activate Feature. This will activate that feature on that specific site.

Deactivate a Feature on a Specific Site
In the Farm Tree window, rick click on the site that you want to activate the feature on and click Show Site Details. This will load all the details for that site.

In the Site Details are, select the Site Features tab. This will show you all the features for that site that are activated and deactivated.

In the Site Features section, right click on the feature that you want to deactivate and click Deactivate Feature. This will deactivate that feature on that specific site.

Activate a Web Scoped Feature on many Sites at once.
SWAT has a lot of wizards built into it to accomplish many routine tasks. On the Wizards menu select Activate Features. This will launch the Activate Features wizard which will guide you through the steps.

On Step 1, select the sites that you want to activate the features on and click next.

On Step 2, select the features that you wish to activate.

On Step 3, click finish. This will activate all the features on the sites that you selected.

Enjoy!!!