I’ve been a little quiet recently, mainly because I’ve had my head down with a large SharePoint and K2 blackpearl project in Manchester, in which I’ve got lots to blog about but just haven’t had the time. However, I think this is pretty important - today I solved a little problem that was bugging me.
My scenario is this;
We have SharePoint sites, created from STP files as part of our line of business application (I know, I know, the STP bit is a bit stupid and it’s hard to update, but we are where we are), and the document libraries within these created sites use content types deployed in the root site which are defined and deployed with a feature.
I needed to add a new field to one of the content types and remove a field from another one. Sounds easy enough – I updated the XML for the definition, redeployed and the definition in the site content type library updated as expected. What I didn’t expect though was all the sites already provisioned didn’t get those changes applied, and even worse, when I provisioned a new site (from the STP) the document library in that also didn’t have the changes. It was almost like the site document libraries had their own copy of the content types.
Of course that’s EXACTLY what the problem is. Solution 1 is not to deploy changes through the features. Using the UI to add or remove the columns as necessary and select the option to also update anything that uses that content type. For me this is a non starter – I’d have to re-export all my STPs so that future provisioned sites get the changes as well as make the manual changes to the content type and remember to propagate down – something that doesn’t sit well with my build and deploy strategy.
So, I found another solution. By attaching a feature receiver I can programmatically change content types and have those changes propagated down through everything that uses it. It’s still not ideal, but it works well. I keep my XML definition static and apply additive changes through the feature receiver in code, which looks like this;
The AddFieldToContentType and RemoveFieldFromContentType methods are extension methods as follows;
Finally, you still have to define the site column before you can attach it.