Deploy new and existing Content Types with SAF (using standard CAML)
If you have ever deployed a SharePoint Content Type via a Feature (using standard CAML) you will more than likely run into one or more of the various “quirks”, namely :
- The Content Type deploys correctly if its not already in the Farm, but if your Content Type is in use, or customised via the GUI then updating Feature has no effect. In this situation you are forced to create your Feature with CAML, but upgrade and maintain it with Feature Receiver Code.
- If you want to “push” your changes down to any lists, this has to be done in code.
I have detailed most of the issues in “Upgrading Content Types”. Becky Bertram also wrote an awesome post on the issues in “Updating Content Types and Site Columns That Were Deployed as a Feature”.
How “EnsureContentType” (in SAF) solves these problems
The reason for this post is to tell you about a SAF Action that removes some of these barriers and offers the following benefits :
- The Action uses a standard SharePoint CAML file containing one or many <ContentType> elements (as defined in the ”http://schemas.microsoft.com/sharepoint/” schema).
- Content Types that don’t already exist can be deployed.
- Content Types that do already exist in the target farm can easily be updated (regardless of whether they have been changed in the GUI or not).
- The Action gives the ability to “Undo” the latest changes, by reverting to a previous version.
- You can optionally push changes down to any dependent lists.
STEP 1. Install SAF.
To start using the “EnsureContentType” action, you need to install SAF following these instructions – “How to install SAF”. “SAF.WSP” (which deploys the necessary binaries into the GAC), needs to be installed and deployed on all machines that will need to run a Saf Macro. Please test that the Action meets your requirements first, before running straight into Live, it’s still beta! 🙂
STEP 2. Create your Content Type in CAML.
The file illustrated below is simply a standard CAML File that is used to deploy Content Types. For information on how to create Content Types using CAML please refer to the “ContentType Element (ContentType)” document on MSDN.
The example file below creates a new Content Type called “PersonContentType” and adds 3 (already installed) site columns to it. One point to note, is that the Display Name for “AssistantNumber” has been changed to “Secretary Mobile” and the “Title” has been changed to “Customer Title”. These changes will take effect if the Content Type is new, or has gone through several iterations in the GUI.
Filename : “Person_Ctype.xml”
STEP 3. Create a SAF Macro.
The next step in the process is to define a SAF Macro that will process the Content Type (defined above). A SAF Macro is really just a Spring IOC Container and hence, must conform to the Spring.Net Schema. The example below illustrates how to call the “Action.EnsureContentType” action and pass an Entity called “Entity.ElementsInfo” into it. The Entity.ElementsInfo element is used to supply parameters about where the CAML file is and how to process it. The most important properties in the Entity are :
- FilePath : Specifies where to load the CAML file from.
- PushChangesToList : a boolean value, specifying whether to push the changes to any lists that use the content type.
- RemoveInChild : If the Content Type you are changing removes existing columns, then this flag specifies whether to remove in child lists.
Note. It is possible to supply many changes to the Action by simple supplying more than one Entity.
Filename : “Macro.xml”
STEP 4. Run your Macro.
SAF allows you to run your Macro in a variety of ways, (such as from a SharePoint Feature, STSADM extension, MSBuild task (beta) or remotely via a WCF Service (beta)). For this example, we will run the Macro from a standard SharePoint Feature, using a Feature.xml file.
This is a standard feature with the main differences being the “ReceiverAssembly”, “ReceiverClass” and “ActionsFile”, which are hopefully self explanatory.
To run the feature below, please use either the SharePoint GUI, or “STSAdm –o ActivateFeature”. If you then deactivate the feature then the Macro will Undo the changes you made.
Filename : “Feature.xml”
Hopefully, from this article you will see how we can now use SAF to control the deployment of Content Types. Using SAF to manage your Content Types means you gain the following advantages.
- Your Content Types can be mastered once in source control, meaning that you can have one version of the truth.
- Content Types can be managed regardless of whether they are new or existing (with several updates).
This Action is new, so if you see any ways to improve it, or find any defects with it, please let me know via email : Mark.Jones@collaboris.co.uk, or by posting an issue onto the CodePlex site, at http://saf.codeplex.com/WorkItem/List.aspx. We are currently, doing regular releases so it wont be long before we can incorporate the fix, or change.
Happy Content Typing!