SharePoint content types inheritance fixed

I came across a great post from Martin Hatch today, which provides a solution for problems with content types and list definitions in features.

I tested it and it seems to work great, although i did make a little change, since the handler throws an error when using a RemoveFieldRef node in the CT definition.

So, i changed this: 

foreach (XmlNode fieldRef in node.ChildNodes)

{

    #region Loop through FieldRefs

    //ignore XML comment tags

    if (fieldRef.Name == "#comment")

    {

       continue;

    }

    string fieldID = fieldRef.Attributes["ID"].Value;

    SPField field = cType.Fields[new Guid(fieldID)];

    cType.FieldLinks.Delete(new Guid(fieldID));

    cType.Update(true);

    cType.FieldLinks.Add(new SPFieldLink(field));

    cType.Update(true);

    #endregion

}

to this:

foreach (XmlNode fieldRef in node.ChildNodes)

{

    #region Loop through FieldRefs

    //ignore everything but FieldRefs

    if (fieldRef.Name == "FieldRefs")

    {

        string fieldID = fieldRef.Attributes["ID"].Value;

        SPField field = cType.Fields[new Guid(fieldID)];

        cType.FieldLinks.Delete(new Guid(fieldID));

        cType.Update(true);

        cType.FieldLinks.Add(new SPFieldLink(field));

        cType.Update(true);

    }

    #endregion

}

Seems to work to me, but if anyone has another suggestion :) please do let me know ;)

Leave a Reply