Logo 
Search:

Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

Activate a Feature through Code

  Asked By: Brock    Date: Feb 06    Category: Sharepoint    Views: 6553

I have an web part that displays a list of features after a user
submits a URL. The features are displayed in a datagrid and has the
option for activating/deactivating. The featuredefinition ID is
passed in as a string in the activate method:

void activateFeature(string strFeatureID)
{

SPSite site = new SPSite(txtURL.Text);
SPWeb web = site.OpenWeb();

Guid guid= new Guid(strFeatureID);

web.Features.Add(guid, true);

}

When I navigate to the site's 'Site features' list under Site
Settings the feature is there...the feature is not activated. I
checked and the web.Features.Add statement is being executed.

Is anyone able to tell me how to activate the feature using the
object model?

Share: 

 

10 Answers Found

 
Answer #1    Answered By: Eashan Nadkarni     Answered On: Feb 06

Not sure if this is the problem or not, but the SharePoint object model  doesn't
normally like working with properties of properties of objects. Normally you
have to instantiate the object locally to modify its properties. Every example
I've ever seen uses code  similar to yours, except they always Instantiate the
Features Property as a SPFeatureCollection object and then call the add  method
directly on that object. Try replacing

web.Features.Add(guid, true);

with

SPFeatureCollection features  = web.Features;
features.Add(guid, true);

 
Answer #2    Answered By: William Odom     Answered On: Feb 06

Your explanation certainly made
sense why my code  doesn't work - but I still can't seem to get it
working.

At this point I am settling for having a regular 'ole windows
application that runs on the server, versus a nifty web  part. Kinda
stinks in the way we're deployed at my company, but I can't seem to
find a resolution.

If there are any other hints, please let me know :)

 
Answer #3    Answered By: Mia Scott     Answered On: Feb 06

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);
}
}
}

 
Answer #4    Answered By: Kristian Chaney     Answered On: Feb 06

It might be a permissions issue where the user running the web  part
does not have sufficient privileges to activate  the feature. You
might try running the code  under elevated permissions.

More info at: http://msdn.microsoft.com/en-
us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.a
spx

 
Answer #5    Answered By: Alicia Scott     Answered On: Feb 06

If permissions is in fact the issue, I would recommend impersonating the system
account rather than elevating privileges.

My blog has links to various sites  that show how. A whitepaper is forthcoming.

 
Answer #6    Answered By: Mike Lamb     Answered On: Feb 06

I would assume if permissions were the issue she would get an error rather than
just not activating the feature.

 
Answer #7    Answered By: Jose Scott     Answered On: Feb 06

you are correct - it wasn't a permission issue. Granted, I
don't know what I was doing wrong, but your example ROCKED!!!

 
Answer #8    Answered By: Taylor Clark     Answered On: Feb 06

Does the user of the page have permission to activate  features?

Is the feature  installed (through STSADM or via code)?

Did you double check that the GUID being passed in matches the id in
feature.xml?

Did you check the ULS logs (in the LOGS folder of the 12 hive)? (You may need to
adjust the log settings to write more details.)

 
Answer #9    Answered By: Anthony Rutledge     Answered On: Feb 06

Thanks so much for the quick response. I the changed you
suggested, as well as a few other modifications - alas, no workie.

At this point I'm going to just settle for a little custom app to run
on the server versus having it in a web  part.

 
Answer #10    Answered By: Heena Nagori     Answered On: Feb 06

Add the line
web.Update();
after
web.Features.Add(guid, true);
and it should works

 
Didn't find what you were looking for? Find more on Activate a Feature through Code Or get search suggestion and latest updates.




Tagged: