Example #1
0
        public static FeatureParent GetFeatureParent(SPWebService farmWebService)
        {
            FeatureParent p = null;

            try
            {
                if (farmWebService == null)
                {
                    return(GetFeatureParentUndefined());
                }
                p = new FeatureParent()
                {
                    DisplayName = "Farm",
                    Url         = "Farm",
                    Id          = farmWebService.Id,
                    Scope       = SPFeatureScope.Farm
                };
            }
            catch (Exception ex)
            {
                Log.Error("Error when trying to get Farm object.", ex);
                return(GetFeatureParentUndefined(ex.Message));
            }

            try
            {
                var farmFeatures = farmWebService.Features;
                p.ActivatedFeatures = ActivatedFeature.MapSpFeatureToActivatedFeature(farmFeatures, p);
            }
            catch (Exception ex)
            {
                Log.Error("Error when trying to load Farm features.", ex);
            }
            return(p);
        }
Example #2
0
        public static FeatureParent GetFeatureParentUndefined(string displayName = "")
        {
            var p = new FeatureParent()
            {
                DisplayName       = string.IsNullOrEmpty(displayName) ? "undefined" : displayName,
                Url               = "undefined",
                Id                = Guid.Empty,
                Scope             = SPFeatureScope.ScopeInvalid,
                ActivatedFeatures = new List <ActivatedFeature>()
            };

            return(p);
        }
Example #3
0
        public static FeatureParent GetFeatureParent(SPWebApplication webApp, string name = "")
        {
            FeatureParent p           = null;
            string        locationUrl = string.Empty;

            try
            {
                if (webApp == null)
                {
                    return(GetFeatureParentUndefined());
                }

                locationUrl = webApp.GetResponseUri(SPUrlZone.Default).ToString();

                p = new FeatureParent()
                {
                    DisplayName = string.IsNullOrEmpty(name) ? webApp.Name : name,  // + " (" + web.Name + ")",
                    Url         = locationUrl,
                    Id          = webApp.Id,
                    Scope       = SPFeatureScope.WebApplication
                };
            }
            catch (Exception ex)
            {
                Log.Error(
                    string.Format(
                        "Error when trying to get web app {0}.",
                        locationUrl
                        ),
                    ex);
                return(GetFeatureParentUndefined(ex.Message));
            }

            try
            {
                var features = webApp.Features;
                p.ActivatedFeatures = ActivatedFeature.MapSpFeatureToActivatedFeature(features, p);
            }
            catch (Exception ex)
            {
                Log.Error(
                    string.Format(
                        "Error when trying to load features from web app {0}.",
                        locationUrl
                        ),
                    ex);
            }
            return(p);
        }
Example #4
0
        public static FeatureParent GetFeatureParent(SPWeb web)
        {
            FeatureParent p           = null;
            string        locationUrl = string.Empty;

            try
            {
                if (web == null)
                {
                    return(GetFeatureParentUndefined());
                }

                locationUrl = web.Url;

                p = new FeatureParent()
                {
                    DisplayName = web.Title, // + " (" + web.Name + ")",
                    Url         = locationUrl,
                    Id          = web.ID,
                    Scope       = SPFeatureScope.Web
                };
            }
            catch (Exception ex)
            {
                Log.Error(
                    string.Format(
                        "Error when trying to get web {0}.",
                        locationUrl
                        ),
                    ex);
                return(GetFeatureParentUndefined(ex.Message));
            }

            try
            {
                var features = web.Features;
                p.ActivatedFeatures = ActivatedFeature.MapSpFeatureToActivatedFeature(features, p);
            }
            catch (Exception ex)
            {
                Log.Error(
                    string.Format(
                        "Error when trying to load features from web {0}.",
                        locationUrl
                        ),
                    ex);
            }
            return(p);
        }
Example #5
0
        public static FeatureParent GetFeatureParent(SPSite siCo)
        {
            FeatureParent p           = null;
            string        locationUrl = string.Empty;

            try
            {
                if (siCo == null)
                {
                    return(GetFeatureParentUndefined());
                }

                locationUrl = siCo.Url;

                p = new FeatureParent()
                {
                    DisplayName = siCo.RootWeb.Title,
                    Url         = locationUrl,
                    Id          = siCo.ID,
                    Scope       = SPFeatureScope.Site
                };
            }
            catch (Exception ex)
            {
                Log.Error(
                    string.Format(
                        "Error when trying to get SiteCollection {0}.",
                        locationUrl
                        ),
                    ex);
                return(GetFeatureParentUndefined(ex.Message));
            }

            try
            {
                var features = siCo.Features;
                p.ActivatedFeatures = ActivatedFeature.MapSpFeatureToActivatedFeature(features, p);
            }
            catch (Exception ex)
            {
                Log.Error(
                    string.Format(
                        "Error when trying to load features from {0}.",
                        locationUrl
                        ),
                    ex);
            }
            return(p);
        }
        private static ActivatedFeature MapSpFeatureToActivatedFeature(SPFeature feature, FeatureParent parent)
        {
            var af = GetActivatedFeature(feature, parent);

            return(af);
        }
        /// <summary>
        /// Factory to map SPFeatureCollections to ActivatedFeatures
        /// </summary>
        /// <param name="featureCollection"></param>
        /// <param name="parent"></param>
        /// <returns></returns>
        public static List <ActivatedFeature> MapSpFeatureToActivatedFeature(SPFeatureCollection featureCollection, FeatureParent parent)
        {
            List <ActivatedFeature> activatedFeatures = new List <ActivatedFeature>();

            if (featureCollection != null)
            {
                foreach (SPFeature f in featureCollection)
                {
                    var af = MapSpFeatureToActivatedFeature(f, parent);
                    activatedFeatures.Add(af);
                }
            }

            return(activatedFeatures);
        }
        /// <summary>
        /// Generate ActivatedFeature
        /// </summary>
        /// <param name="spFeature"></param>
        /// <param name="parent"></param>
        /// <returns></returns>
        public static ActivatedFeature GetActivatedFeature(SPFeature spFeature, FeatureParent parent = null)
        {
            if (spFeature == null)
            {
                return(null);
            }

            // create preliminary feature with default values ...
            ActivatedFeature af = new ActivatedFeature()
            {
                Id                = Guid.Empty,
                Version           = new Version("0.0.0.0"),
                Parent            = parent,
                SharePointFeature = spFeature,
                Name              = "undefined",
                Faulty            = false
            };

            // in case of faulty features, many of these settings can not be set
            // therefore, every property is set in try / catch statement

            // ID
            try
            {
                af.Id = spFeature.DefinitionId;
            }
            catch (Exception ex)
            {
                if (af.Name.Equals("undefined"))
                {
                    af.Name = ex.Message;
                }
                af.Faulty = true;
            }

            // Version
            try
            {
                af.TimeActivated = spFeature.TimeActivated;
            }
            catch (Exception ex)
            {
                if (af.Name.Equals("undefined"))
                {
                    af.Name = ex.Message;
                }
                af.Faulty = true;
            }

            // Version
            try
            {
                af.Version = spFeature.Version;
            }
            catch (Exception ex)
            {
                if (af.Name.Equals("undefined"))
                {
                    af.Name = ex.Message;
                }
                af.Faulty = true;
            }

            // Parent
            try
            {
                af.Parent = parent == null?FeatureParent.GetFeatureParent(spFeature) : parent;
            }
            catch (Exception ex)
            {
                if (af.Name.Equals("undefined"))
                {
                    af.Name = ex.Message;
                }
                af.Faulty = true;
            }

            // SharePointFeature is already set on initialization of af
            // Name
            try
            {
                var def = spFeature.Definition;

                if (def != null)
                {
                    af.Name = def.DisplayName;
                }
                else
                {
                    af.Name   = string.Format(Common.Constants.Text.UndefinedActivatedFeature, af.Id);
                    af.Faulty = true;
                }
            }
            catch (Exception ex)
            {
                if (af.Name.Equals("undefined"))
                {
                    af.Name = ex.Message;
                }
                af.Faulty = true;
            }

            return(af);
        }