Example #1
0
        /// <summary>
        /// Get a feature definition based on an activated feature, even from a faulty one
        /// </summary>
        /// <param name="feature">an activated Feature</param>
        /// <returns>A FeatureDefinition with current feature already added</returns>
        /// <remarks>
        /// ActivatedFeature is better than SPFeature, because it already handled the faulty feature issues
        /// </remarks>
        public static FeatureDefinition GetFeatureDefinition(ActivatedFeature feature)
        {
            if (feature == null)
            {
                throw new ArgumentNullException("Parameter 'feature'");
            }

            var fd = new FeatureDefinition()
            {
                Id         = feature.Id,
                Name       = feature.Name,
                Scope      = feature.Scope,
                Faulty     = feature.Faulty,
                Definition = feature.Definition
            };

            fd.ActivatedFeatures.Add(feature);

            return(fd);
        }
Example #2
0
        /// <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);
        }