Activate-Deactivate Features Programmatically in SharePoint
At times, We may have to activate/deactivate features programmatically. Here is the code snippet:
PowerShell way:
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>
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(); } else { 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 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>
No comments:
Please Login and comment to get your questions answered!