Had a little time last night and I was curious so I thought I would throw
together a quick little webpart to do what you are trying. Create a web part
with the code below. Put it on any page, select a website and a feature to
activate and then click the button. It works on my site collection.
namespace Mindsharp.FeatureControl
{
public class ActivateFeatures:System.Web.UI.WebControls.WebParts.WebPart
{
DropDownList cbxFeatures;
DropDownList cbxWebs;
Button btnGo;
string name;
System.Globalization.CultureInfo culture = new
System.Globalization.CultureInfo(1033);
protected override void CreateChildControls()
{
base.CreateChildControls();
int temp = 0;
cbxWebs = new DropDownList ();
Controls.Add(cbxWebs);
cbxFeatures = new DropDownList();
SPFarm farm = SPFarm.Local;
SPFeatureDefinitionCollection featureDefinitions =
farm.FeatureDefinitions;
if (Page.IsPostBack)
{
temp = cbxFeatures.SelectedIndex ;
}
foreach (SPFeatureDefinition featureDef in featureDefinitions)
{
if ((featureDef.Scope == SPFeatureScope.Web)&&
(featureDef.Hidden==false ))
{
name = featureDef.GetTitle(culture);
cbxFeatures.Items.Add(new ListItem(name,
featureDef.Id.ToString()));
}
}
Controls.Add(cbxFeatures);
if (Page.IsPostBack)
{
cbxFeatures.SelectedIndex=temp;
}
btnGo = new Button();
btnGo.Text="Activate";
btnGo.Click += new EventHandler(btnGo_Click);
Controls.Add(btnGo);
}
//Populate treeview with hierarchy from current Site Collection
protected override void OnPreRender(EventArgs e)
{
SPSite siteCollection = SPContext.Current.Site;
SPWeb web = siteCollection.RootWeb;
//Add the TreeNode to the TreeView
cbxWebs.Items.Add(new ListItem(web.Title,web.Url));
//Add the subsites to the top-level site TreeNode
AddSubSites(web);
}
//Recursive function to add child nodes to the tr
private void AddSubSites(SPWeb parentWeb)
{
SPWebCollection webs = parentWeb.Webs;
foreach (SPWeb subWeb in webs)
{
//Add the TreeNode to the TreeView
cbxWebs.Items.Add(new ListItem(subWeb.Title, subWeb.Url));
//Add the subsites to the top-level site TreeNode
AddSubSites(subWeb);
//Check if this site has any subsites
if (subWeb.Webs.Count != 0)
{
//Add the subsites to this sites TreeNode
AddSubSites(subWeb);
}
}
}
void btnGo_Click(object sender, EventArgs e)
{
if (cbxFeatures.SelectedIndex > 0)
{
string featureGuid;
string weburl;
featureGuid =cbxFeatures.SelectedValue.ToString();
weburl = cbxWebs.SelectedValue;
activateFeature(weburl, featureGuid);
}
}
void activateFeature(string webName, string fGUID)
{
SPSite site = new SPSite(webName);
SPWeb web = site.OpenWeb();
SPFeatureCollection features = web.Features;
features.Add(new Guid(fGUID), true);
}
}
}