How to activate feature using C# code programmatic ally for multiple sub-sites . my feature is deployed "site" level .

Is there anyway we can automatically activate the feature to all the sub-sites?

  • You can only activate Site level features to site collection only. You can't activate it to sub site. – users1100 May 11 '16 at 11:07

SharePoint features can exist at several different scopes, including Web, Site, and Web Application. Each of these can be activated and deactivated programmatically, but they have to be handled slightly differently.

For Site:


{SPSite}.Features.Remove(FeatureGuid, true);

For Web :


{SPWeb}.Features.Remove(FeatureGuid, true);

Example :

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

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

SPWebApplication webApplication= web.Site.WebApplication;

Loop through all sub sites and activate feature

 SPSite osite = properties.Feature.Parent as SPSite;
    foreach(SPWeb oWeb in site.AllWebs) 
        if (oWeb .Features[featureGuid] == null) 
            oWeb .Features.Add(featureGuid);
  • HI can send me the full code . i am getting erros – 811041 May 11 '16 at 10:57
  • What error did you got? Please share it – Mohamed Arif May 11 '16 at 12:03
  • SPSite osite = properties.Feature.Parent as SPSite; SPWeb web = osite.OpenWeb(); foreach (SPWeb webApplication in osite.AllWebs) { SPSite site = webApplication.Site; if (site.Features[featureGuid] == null) { site.Features.Add(featureGuid); } } – 811041 May 13 '16 at 14:11
  • For each loop i am getting the error ones you check and tell me the correct code i am deploy the "site" scoped feature. – 811041 May 13 '16 at 14:13
  • If you are using site scoped feature.. The below code is enough SPSite osite = properties.Feature.Parent as SPSite; if (osite.Features[featureGuid] == null) { osite.Features.Add(featureGuid); } } – Mohamed Arif May 14 '16 at 4:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.