Recently, I got into issues with using feature upgrade mechanism for adding a new field to a content type. We have been using AddContentTypeField Element inside UpgradeActions Element successfully for some releases and it was adding new content type fields to existing sites properly. However, so far, we did not get into scenario where the same feature was also required to be activated on new sites. When we got into the scenario, we realized this approach was not working at all. The approach we have been using was as per SharePoint Application Lifecycle Management (ALM) guidelines that feature upgrade should take care of both the scenarios:
The above approach is in line with the approach suggested in the MSDN article: Application Lifecycle Management in SharePoint 2010. Under the section “Upgrading SharePoint 2010 Features: A High-Level Walkthrough”, the article gives an example of adding a new field to existing content type and mentions the following in step 2:
“Update the existing definition of the content type in the existing feature element file. This update will apply to all sites where the feature is newly deployed and activated.”
After a lot of troubleshooting and trying various options, we finally could find an approach that works.
Whether we use code (custom upgrade action) OR declarative (using <AddContentTypeField>) – it’ll never work (sometime throws error, or sometime doesn’t update content types without any error) if we change the existing content type definition xml file.
Later we also realized that Official documentation also states the same thing, however, it was never documented in connection with feature upgrade. Hence, there is confusion across community on this area. Here are the references that state that we should never update content type definition file:
Do not, under any circumstances, update the content type definition file for a content type after you install and activate that content type. SharePoint Foundation does not track all the changes that are made to the content type definition file. Therefore, you have no reliable method for pushing down all the changes made to site content types to the child content types.
You cannot add columns to an existing site content type declaratively, in other words, by updating the Feature XML files
The similar issues are also documented by Charles Chen in his blog SharePoint Content Type Lifecycle Management, however, the approach suggested there is to update the original content type definition manifest file, and add a CustomUpgradeAction as well in addition to the AddContentTypeField. I’m not sure what’s official recommendation here, however, personally I am not in favor of this approach for two reasons: first, it updates original content type definition, which is not recommended in official documentation. Second, it is adding two instructions for upgrade – which can cause issue in some later updates. At the same time, the blog has been written well and there’s a lot of sample code in that which applies to the approach suggested here also.
Update (25-Mar-2012)
Sample Code
As there are plenty other resources available for the samples, I am not providing sample code for this particular approach. Vesa has provided a detailed post on this. Check out section "Task 2 – Updating features and content types". He has also provided zip file containing the complete source code here