02 Feb 2011

Some follow up on my previous post

I had to work with this a bit more today, so I wrapped it up a bit to make it more convenient to work with:

public static class SPFieldExtension
{
    static IDictionary<Guid, bool> PublishingBuildInFields { get; set; }

    static SPFieldExtension()
    {
        Type type = typeof(FieldId);

        var propInfoArray = type.GetProperties(BindingFlags.Static | BindingFlags.Public);

        SPFieldExtension.PublishingBuildInFields = propInfoArray
            .Select(prop => (Guid) prop.GetValue(null, null))
            .ToDictionary(g => g, g => true);
    }

    /// <summary>
    /// Check if the given field is a build in field of SharePoint (WSS and MOSS)
    /// </summary>
    /// <param name="guid"></param>
    public static bool IsSPBuiltInField(Guid guid)
    {
        return SPBuiltInFieldId.Contains(guid)
            || SPFieldExtension.PublishingBuildInFields.ContainsKey(guid);
    }

    /// <summary>
    /// Check if the given field is a build in field of SharePoint (WSS and MOSS)
    /// </summary>
    /// <param name="field"></param>
    public static bool IsSPBuiltInField(this SPField field)
    {
        return SPFieldExtension.IsSPBuiltInField(field.Id);
    }
}

So, I created a static class to hold my two methods. One is the same a the one in the previous post, taking a Guid and returning a Boolean. The second one is an extension method for SPField that simply call the first method. It’s more continent to use it this way.



blog comments powered by Disqus