Search This Blog

Monday, April 11, 2011

Programmatically check a Feature is activated on a SiteCollection or Not

Sometime we have a requirement like while deactivating a feature we need to check programmatically weather that Particular feature is deployed to other site collections of the same Web Application or not.
 Below is the code snippet with the help of which we can check it:
    public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
         {
             base.FeatureDeactivating(properties);            
             String URL;
             URL = ((SPSite)properties.Feature.Parent).Url;
             bool isFeatureActiveInOtherSiteCollection = false;
             
             try
             {
                 SPSecurity.RunWithElevatedPrivileges(delegate()
                         {
                             using (SPSite objSite = new SPSite(URL))
                             {
                                 using (SPWeb objWeb = objSite.OpenWeb())
                                 {
                                     SPWebApplication objSPWebApp = objSite.WebApplication;

                                     foreach (SPSite objOtherSite in objSPWebApp.Sites)
                                     {
                                         if (!objOtherSite.Url.Equals(objSite.Url) && objOtherSite.Features[properties.Feature.DefinitionId] != null)
                                         {
                                             isFeatureActiveInOtherSiteCollection = true;
                                             break;
                                         }
                                     }
                                 }
                             }
                         });
             }                    
             catch (Exception ex)
             {
                 throw ex;
             }
         }

No comments:

Post a Comment