Showing posts with label Programmatically. Show all posts
Showing posts with label Programmatically. Show all posts

Wednesday, June 1, 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;
             }
         }

Programmatically check a WSP is deployed on a Web Application or not

Sometimes we have a scenario like we have to check that whether a particular wsp (Solution Package) is deployed on a Web application or not.
Suppose we have a feature and on the feature activation we need to add a webpart on some page of a site but that webpart is not packaged in the Feature Deployment Solution Package rather it’s a part of some other wsp package.
So during the activation process we have to know whether the webpart which we are going to add through feature is deployed on the webapplication or not.
Below is the code snippet with the help of which we can check that whether a particular wsp package is deployed on a webapplication or not.
private static bool CheckWSPDeployedStatus(SPSite objSite)
        {
            System.Collections.ObjectModel.Collection<SPWebApplication> objWebApplications = objSite.WebApplication.Farm.Solutions["Sample.wsp"].DeployedWebApplications;
            if (objWebApplications != null && objWebApplications.Count > 0)
            {
                if (objWebApplications.Contains(objSite.WebApplication))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }