tags:

views:

766

answers:

2

I'm doing some work on an existing SharePoint site and part of what I need to do is create a new field on an existing content type.

I'm going to be deploying changes via a feature so I'm debating whether I use CAML to generate the field and then programmatically add it to the existing content type, or whether I can just do it all via the feature receiver.

What would be the best/ cleanest (and most reusable) solution for doing this?

A: 

A simple way is to do it in the feature receiver, get a reference to the SPContentType, add the field as XML (SPContentType.Fields.AddFieldAsXml) and then update pushing the changes to child content types

It is really tricky to modify content types on a site that already exists. My preferred method involves having a solution for all the content types and re-deploying that solution when changes are made.

Nat
I'm doing what the linked article stated but I keep getting "Collection cannot be modified" when doing the contentType.Update(true). Any ideas?
Slace
Not sure, could be one of the fields that are being modified is a publishing field or otherwise kicks up a fuss when it is altered.Try making the changes one at a time and see which one dies.
Nat
A: 

I am going to go on the assumption that by field you mean a new Site Column. In this case I would do it all in the code behind. The amount of code that you need to write to create a site column is rather minimal. You can also attempt to remove the Site Column from any Content Types that are using it if you wish, but this is a task that is quite more involved.

From a reuse perspective if you set-up properties in the feature.xml file you can use those to configure things such as the column name, column type, target content type, etc. and then have a class that can be re-used in the future.

Richard C. McGuire