Techbubbles

Activating and Deactivating Feature programmatically in SharePoint

Sometimes, You may need to activate/deactivate Feature programmatically in SharePoint. Feature should have the scope of the web, site or web application or Farm.You can get the Feature by accessing Feature properties of Site or web. Write the following code to retrieve the activated features available in Web application/Site/Web level.

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

Once you have the reference to the list of features then you can find the feature by feature id or feature name if the feature is activated, using the following code

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

only activated features are available through site.Features/web.features property. if the variable feature is null then it means the feature is not activated. You can activate the feature using the following code

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

if the feature is activated the you can write the following  code to deactivate it

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

You need to open a new SPWebApplication/SPSite/SPWeb object to get the changes abruptly made as shown below

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

Suppose if you want to apply a custom master page to your site after you activate the feature then you can write the code in feature event receiver activated event

private const string defaultMasterFileName = "v4.master";
private const string customMasterFilename= "custom.master";
public override void FeatureActivated(SPFeatureReceiverProperties properties) {
    using (SPWeb web = (properties.Feature.Parent as SPSite).AllWebs[0])
         {
                if (web != null)
                {
                    UpdateMasterPage(web, customMasterFilename);
                }
          }
      }
private void UpdateMasterPage(SPWeb web, String newMasterFileName) {
            try {                
                bool webAllowUnsafeUpdates = web.AllowUnsafeUpdates;

                web.AllowUnsafeUpdates = true;

                web.CustomMasterUrl = web.CustomMasterUrl.Replace(Path.GetFileName(web.CustomMasterUrl), newMasterFileName); // Site Masterpage
                web.MasterUrl = web.MasterUrl.Replace(Path.GetFileName(web.MasterUrl), newMasterFileName); // System Masterpage
                web.Update();

                web.AllowUnsafeUpdates = webAllowUnsafeUpdates;               
            }
            catch (Exception e) {
                
            }
        }

Related Posts:

%d bloggers like this: