locked
Programmatically Activating and Deactivating a feature RRS feed

Answers

  • You can activate features through code by adding the GUID of the feature to the SPWeb, SPSite, or SPWebApplication object.

    Guid featureGuid = new Guid(”[Feature-GUID-Here]″);
    if (web.Features[featureGuid] == null)
    {
             web.Features.Add(featureGuid);
    }

    To activate features on the Farm level , it's a little bit tricky :

    SPWebApplication webApplication= web.Site.WebApplication;
    if(webApplication.WebService.Features[featureGuid]==null)
    {
           webApplication.WebService.Features.Add(featureGuid);
    }


    Ayman M. El-Hattab, Microsoft Certified SharePoint Specialist, http://ayman-elhattab.blogspot.com .. Follow me at twitter @ http://twitter.com/aymanelhattab
    • Marked as answer by Roth Sharp Wednesday, May 13, 2009 7:28 AM
    Wednesday, May 13, 2009 7:24 AM

All replies

  • You can activate features through code by adding the GUID of the feature to the SPWeb, SPSite, or SPWebApplication object.

    Guid featureGuid = new Guid(”[Feature-GUID-Here]″);
    if (web.Features[featureGuid] == null)
    {
             web.Features.Add(featureGuid);
    }

    To activate features on the Farm level , it's a little bit tricky :

    SPWebApplication webApplication= web.Site.WebApplication;
    if(webApplication.WebService.Features[featureGuid]==null)
    {
           webApplication.WebService.Features.Add(featureGuid);
    }


    Ayman M. El-Hattab, Microsoft Certified SharePoint Specialist, http://ayman-elhattab.blogspot.com .. Follow me at twitter @ http://twitter.com/aymanelhattab
    • Marked as answer by Roth Sharp Wednesday, May 13, 2009 7:28 AM
    Wednesday, May 13, 2009 7:24 AM
  • Unfortunately the SPFeatureCollection.Add method seems a bit buggy -- it will not throw an exception even if the caller is not a Site Collection Admin. It will add the feature to the collection locally, but it won't actually be activated on the site/site collection/web application. Even creating a new SPSite and checking its feature collection doesn't seem to work to distinguish failure from success.
    Wednesday, March 10, 2010 12:49 AM
  • Thanks Ayman..

    Useful Info. It works for me.

    Thanks again.


    -Kaustubh
    Wednesday, March 10, 2010 10:26 AM
  • SPWebApplication webApplication= web.Site.WebApplication;

    I don't quite understand what web I should substitute here? Central Admin?

    Thursday, September 26, 2013 10:33 AM