예제 #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);
        }
        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);
        }
예제 #3
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();
                            }
                        }
                    }
                }
            });
        }