Friday, July 6, 2012

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 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>

You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.

Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...