Activate-Deactivate Features Programmatically in SharePoint

At times, We may have to activate/deactivate features programmatically. Here is the code snippet:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace ActivateFeature
    class Program
        static void Main(string[] args)
            string SiteURL;

            if (args.Length == 0)
                Console.WriteLine("Enter the site collection URL:");
                SiteURL = Console.ReadLine();
                SiteURL = args[0];
            SPSite oSPSite = new SPSite(SiteURL);

            //Get the Feature to Activate
            SPFarm farm = oSPSite.WebApplication.Farm;
            SPFeatureDefinitionCollection features = farm.FeatureDefinitions;
            SPFeatureDefinition feature = features[new Guid("05BA4E54-C1E9-4244-8276-06451EBBB260")];

            if (feature != null)    //Make sure Feature is installed!
                // Activate the feature
                oSPSite.Features.Add(feature.Id, true);
                //call oSPSite.Features.Remove

PowerShell way to Activate-Deactivate Features:

To activate a feature in PowerShell:

Enable-SPFeature -identity <Feature-Folder-Name/GUID> -URL <URL-of-Web-app/Site coll/Site>

To deactivate a feature: 
Disable-SPFeature -identity <Feature-Folder-Name/GUID> -URL <URL-of-Web-app/Site coll/Site>

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. Passionate about sharing the deep technical knowledge and experience to help others, through the real-world articles!

Leave a Reply

Your email address will not be published. Required fields are marked *