示例#1
0
        public static SPWebApplication GetWebAppByUrl(string webAppUrl)
        {
            if (string.IsNullOrEmpty(webAppUrl))
            {
                return(null);
            }

            SPWebApplication webApp = null;

            // first try content web apps:
            var webApps = FarmRead.GetWebApplicationsContent();

            if (webApps != null && webApps.Count > 0)
            {
                webApp = webApps.FirstOrDefault(wa => wa.GetResponseUri(SPUrlZone.Default).ToString().Equals(webAppUrl, StringComparison.InvariantCultureIgnoreCase));
            }

            // else try admin web apps:
            if (webApp == null)
            {
                webApps = FarmRead.GetWebApplicationsAdmin();

                if (webApps != null && webApps.Count > 0)
                {
                    webApp = webApps.FirstOrDefault(wa => wa.GetResponseUri(SPUrlZone.Default).ToString().Equals(webAppUrl, StringComparison.InvariantCultureIgnoreCase));
                }
            }
            return(webApp);
        }
        /// <summary>forcefully removes a feature definition from the farm feature definition collection</summary>
        /// <param name="id">Feature Definition ID</param>
        public static int Uninstall(Guid id, int compatibilityLevel, bool force, out Exception exception)
        {
            var processingCounter = 0;

            exception = null;

            try
            {
                var featureDefinitions       = FarmRead.GetFeatureDefinitionCollection();
                var featureDefinitionsBefore = featureDefinitions.Count;
#if (SP2013)
                {
                    featureDefinitions.Remove(id, compatibilityLevel, force);
                }
#else
                {
                    featureDefinitions.Remove(id, force);
                }
#endif
                processingCounter = featureDefinitionsBefore - featureDefinitions.Count;

                if (processingCounter > 0)
                {
                    SingletonDb.Singleton.InMemoryDb.RemoveUninstalledFeatureDefinition(id);
                }
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            return(processingCounter);
        }
        public static int ProcessAllFeaturesInWebApp(string webAppUrl, IEnumerable <Guid> featureIds, SPFeatureScope scope, bool activate, bool force, out Exception exception)
        {
            var processingCounter = 0;

            exception = null;

            if (string.IsNullOrEmpty(webAppUrl))
            {
                exception = new ArgumentNullException("webAppUrl is null");
                return(processingCounter);
            }
            try
            {
                var webApp = FarmRead.GetWebAppByUrl(webAppUrl);
                if (webApp == null)
                {
                    exception = new ArgumentNullException("webApp not found by Url " + webAppUrl);
                    return(processingCounter);
                }

                processingCounter += ProcessAllFeaturesInWebApp(webApp, featureIds, scope, activate, force, out exception);
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            return(processingCounter);
        }
        private static int ProcessAllFeaturesInFarm(SPWebService farm, IEnumerable <Guid> featureIds, SPFeatureScope scope, bool activate, bool force, out Exception exception)
        {
            var processingCounter = 0;

            exception = null;

            if (farm == null)
            {
                exception = new ArgumentNullException("farm is null");
                return(processingCounter);
            }
            try
            {
                if (scope == SPFeatureScope.Farm || scope == SPFeatureScope.ScopeInvalid)
                {
                    processingCounter += ProcessAllFarmFeaturesInFarm(farm, featureIds, activate, force, out exception);
                }

                if (scope != SPFeatureScope.Farm)
                {
                    SPWebApplicationCollection webApps = FarmRead.GetWebApplicationsContent();

                    if (webApps != null && webApps.Count > 0)
                    {
                        foreach (SPWebApplication wa in webApps)
                        {
                            processingCounter += ProcessAllFeaturesInWebApp(wa, featureIds, scope, activate, force, out exception);
                        }
                    }

                    webApps = FarmRead.GetWebApplicationsAdmin();
                    if (webApps != null && webApps.Count > 0)
                    {
                        foreach (SPWebApplication wa in webApps)
                        {
                            processingCounter += ProcessAllFeaturesInWebApp(wa, featureIds, scope, activate, force, out exception);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            return(processingCounter);
        }
        private static int ProcessAllFeaturesInFarm(IEnumerable <Guid> featureIds, SPFeatureScope scope, bool activate, bool force, out Exception exception)
        {
            var processingCounter = 0;

            exception = null;


            try
            {
                var farm = FarmRead.GetFarm();
                if (farm == null)
                {
                    exception = new ArgumentNullException("farm is null !? not enough rights?");
                    return(processingCounter);
                }

                processingCounter += ProcessAllFeaturesInFarm(farm, featureIds, scope, activate, force, out exception);
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            return(processingCounter);
        }
示例#6
0
        private List <FeatureDefinition> LoadAllFeatureDefinitions()
        {
            var spDefs = FarmRead.GetFeatureDefinitionCollection();

            return(FeatureDefinition.GetFeatureDefinition(spDefs));
        }
示例#7
0
        /// <summary>
        /// Get activated features from farm and build up SharePointParentHierarchy
        /// </summary>
        private void LoadAllActivatedFeaturesAndHierarchy()
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                var farm = FarmRead.GetFarm();
                // Get Farm features
                var farmFeatures = farm.Features;

                var parent = FeatureParent.GetFeatureParent(farm);

                FarmId = parent.Id;

                SharePointParentHierarchy.Add(FarmId, new List <FeatureParent>());
                Parents.Add(parent);

                if (farmFeatures != null)
                {
                    var activatedFarmFeatures = ActivatedFeature.MapSpFeatureToActivatedFeature(farmFeatures, parent);

                    ActivatedFeatures.AddRange(activatedFarmFeatures);
                }

                // Get Web App features

                var adminWebApps = FarmRead.GetWebApplicationsAdmin();

                // Central Admin
                var caIndex = 1;
                foreach (SPWebApplication adminApp in adminWebApps)
                {
                    if (adminApp != null)
                    {
                        var index    = (adminApp.Features.Count == 1 && caIndex == 1) ? string.Empty : " " + caIndex.ToString();
                        var caParent = FeatureParent.GetFeatureParent(adminApp, "Central Admin" + index);

                        AddParentToHierarchyAndParentsList(FarmId, caParent, true);

                        var adminFeatures = adminApp.Features;

                        if (adminFeatures != null && adminFeatures.Count > 0)
                        {
                            var activatedCaWebAppFeatures = ActivatedFeature.MapSpFeatureToActivatedFeature(adminFeatures, caParent);
                            ActivatedFeatures.AddRange(activatedCaWebAppFeatures);
                        }

                        var sites = adminApp.Sites;

                        if (sites != null && sites.Count > 0)
                        {
                            foreach (SPSite s in sites)
                            {
                                GetSiteFeatuesAndBelow(s, caParent.Id);
                                s.Dispose();
                            }
                        }
                    }
                }

                // Content Web Apps
                var contentWebApps = FarmRead.GetWebApplicationsContent();

                foreach (SPWebApplication webApp in contentWebApps)
                {
                    if (webApp != null)
                    {
                        var waAsParent = FeatureParent.GetFeatureParent(webApp);

                        AddParentToHierarchyAndParentsList(FarmId, waAsParent, true);

                        var waFeatures = webApp.Features;

                        if (waFeatures != null && waFeatures.Count > 0)
                        {
                            var activatedWebAppFeatures = ActivatedFeature.MapSpFeatureToActivatedFeature(waFeatures, waAsParent);
                            ActivatedFeatures.AddRange(activatedWebAppFeatures);
                        }

                        var sites = webApp.Sites;

                        if (sites != null && sites.Count > 0)
                        {
                            foreach (SPSite s in sites)
                            {
                                GetSiteFeatuesAndBelow(s, waAsParent.Id);
                                s.Dispose();
                            }
                        }
                    }
                }
            });
        }