6

In simplified form, I have the following situation.

A Parent content type

<ContentType ID="0x0100db5b9dac9c5f4d098820f922eebf3b39"
             Name="Parent"
             Group="Example Content Types"
             Description="Parent Content Type"
             Inherits="TRUE"
             Version="0">
  <FieldRefs>
    <FieldRef ID="{3822302b-cff8-4f7c-bcc0-de1540cb7447}"
              Name="FirstParentField" />
    <FieldRef ID="{D1BBFFB1-A7DE-4DC8-B013-3D06E7D6FAAF}"
              Name="SecondParentField" />
  </FieldRefs>
</ContentType>

with the following fields

<Field ID="{3822302b-cff8-4f7c-bcc0-de1540cb7447}"
        Type="Text"
        Name="FirstParentField"
        DisplayName="First Parent Field"
        Group="Example Columns"
        Overwrite="TRUE" />
<Field ID="{D1BBFFB1-A7DE-4DC8-B013-3D06E7D6FAAF}"
        Type="Text"
        Name="SecondParentField"
        DisplayName="Second Parent Field"
        Group="Example Columns"
        Overwrite="TRUE" />

and a child content type defined as

<ContentType ID="0x0100db5b9dac9c5f4d098820f922eebf3b39005fc63c83e67045908707c64645d4db55"
             Name="Child"
             Group="Example Content Types"
             Description="Child Content Type"
             Inherits="TRUE"
             Version="0">
  <FieldRefs>
    <RemoveFieldRef ID="{D1BBFFB1-A7DE-4DC8-B013-3D06E7D6FAAF}"
                    Name="SecondParentField" />
    <FieldRef ID="{5C3ADD2C-2196-4C79-8117-EEBCD453DD5F}"
              Name="ChildField" />
  </FieldRefs>
</ContentType>

with the following field

<Field ID="{5C3ADD2C-2196-4C79-8117-EEBCD453DD5F}"
     Type="Text"
     Name="ChildField"
     DisplayName="Child Field"
     Group="Example Columns"
     Overwrite="TRUE" />

The idea is to hide the one of the parent's fields in the child content type while retaining the other.

As many of you will no doubt know, this does not work. The reason being XML declarations can only state how the object is. By trying to remove an inherited field reference I am stating the this is like the parent with a slight modification -- the modification not being allowed.

So, in an attempt to get around this limitation, I added the following

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        using (var web = ((SPSite)properties.Feature.Parent).RootWeb)
        {
            var child = web.AvailableContentTypes["Child"];
            child.Update();
        }
    }

thinking if I forced the update, the modification declared in the XML would be applied.

This throws an SPException is thrown stating 'The collection cannot be modified.'

Could you help me understand what collection is failing to be modified and why?

3 Answers 3

7

AvailableContentTypes is a read-only collection. Use SPWeb.ContentTypes instead.

1
  • That answered the question, but unfortunately, the child content type still inherits both of the parents fields. I'll continue experimenting. Thanks.
    – David Culp
    Commented Aug 20, 2010 at 2:40
2

If you are using a farm solution, setting Inherits="FALSE" or removing the Inherits attribute in the child content type should solve your problem. This will also eliminate the need for the Feature receiver.

See this blog post for more information:

http://msmvps.com/blogs/windsor/archive/2011/05/02/custom-content-types-and-inherits-in-sharepoint-2010.aspx

1

In the parent content type remove Inherits="TRUE"

1
  • And add the need fields again in the child's FieldRefs
    – Colin
    Commented Jun 8, 2011 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.