Pages

Saturday, October 23, 2010

SharePoint: Active/Deactivate Feature programmatically

Sometimes you may need to activate/deactivate features programmatically. In my case, I needed to do some synchronization from a Synchronization feature. As part of the synchronization, I needed to move data from one list to another. However the destination list had event receiver attached. The event receiver was attached using another feature (say ListItemEventReceiverAttacher). So I needed to deactivate the feature (i.e., ListItemEventReceiverAttacher) to make sure that event receiver associated to the list is not active. So let’s come to the point rather than beating around the bush.

First of all, your feature can have scope of web, site or webapplication or Farm. so you need to get the feature first. You can do so by accessing Features properties of Site or web as shown below:

1. Get the activated Features available in the WebApplication/Site/Web level

var webApplicationFeatures = webapplication.Features
var webFeatures = web.Features;
var siteFeatures = site.Features;

2. once you have got the features you can find your feature by feature id or name, if the feature is activated, as shown below:

var feature = site.Features.SingleOrDefault(sf => sf.DefinitionId == myFeatureDefinitionId);

var feature = site.Features.SingleOrDefault(sf => sf.Definition.DisplayName.Equals("Feature Title"));

If the feature is null then the feature is not activated. Only activated features will be available through site.Features/web.Features property.

3. If feature is not activated, you can activate the feature as shown below:

if(feature==null)//if feature is not activated
{
site.Features.Add(featureId);//activate feature
}

4. If the feature is activated, you can deactivate the feature as shown below:

if(feature!=null)//if feature is activated
{
site.Features.Remove(feature.DefinitionId);//deactivate feature
}

Important: One important thing to notice that once you activate/deactivate a feature against an object (SPWebApplication/SPSite/SPWeb), you need to open a new SPWebApplication/SPSite/SPWeb object to get the changes you made. The scenario shown below:

using (SPSite site=new SPSite("http://myserver"))
{
    //Activate or deactivate feature
    //the changes will not be available in this scope.
}
using (SPSite site=new SPSite("http://myserver"))
{
    //Changes made to the feature in upper using block will be effeftive here
}

6 comments:

  1. Thanks Sohel for the articles.

    I found a problem with activating the web features using your code. It turns out that web.Features returns only *activated* features. It means that the line of code:

    var feature = site.Features.SingleOrDefault(sf => sf.Definition.DisplayName.Equals("Feature Title"));

    always returns null unless the feature is already activated. Could you comment on that?

    Thanks,
    Leszek

    ReplyDelete
  2. You can use SPFarm.Local.FeatureDefinitions[featureName]; for Web features

    // Look for feature definition using feature name
    SPFeatureDefinition featureDefinition = SPFarm.Local.FeatureDefinitions[featureName];

    // Look for activated feature
    SPFeature feature = web.Features.Cast().FirstOrDefault(f => f.DefinitionId == featureDefinition.Id);

    // Deactivate feature if activated
    if (feature != null)
    {
    try
    {
    web.Features.Remove(feature.DefinitionId);
    }
    catch (Exception exc)
    { }
    }

    // Activate feature
    try
    {
    web.Features.Add(featureDefinition.Id);
    }
    catch (Exception exc)
    { }

    ReplyDelete
  3. Thanks a lot David for pointing out the mistakes. I've updated the post.

    ReplyDelete
  4. Hi,
    I want to activate my navigation feature when user select different language from welcome menu
    our top bar menu titles were in the resource but
    to create them automatically I moved the into sql
    but sharepoint does not refres feature when language changes.
    Could you please help me on this?
    Regards
    Reside

    ReplyDelete
  5. The Sunny Days Sunscreen rubs in easily and does not leave a greasy after effect that is so often left behind with sunscreens.The smell is a light citrus scent,which I personally love.



    SEOServices

    ReplyDelete
  6. why is this not working i am not able to deactivate the feature

    public class Deactivate_Features : SPWebEventReceiver
    {
    ///
    /// A site was provisioned.
    ///
    public override void WebProvisioned(SPWebEventProperties properties)
    {
    SPWeb web = properties.Web;
    SPFeatureDefinition listtemplate = SPFarm.Local.FeatureDefinitions["ReportListTemplate"];
    SPFeatureDefinition holidaylist = SPFarm.Local.FeatureDefinitions["HolidaysList"];
    web.Features.Remove(listtemplate.Id, true);
    web.Features.Remove(holidaylist.Id, true);


    web.Update();
    }


    }

    ReplyDelete

Note: Only a member of this blog may post a comment.