Sometimes when I'm working on a new Feature I find it easier to take certain snippets that I need to test and pull them out into custom STSADM commands. This enables me to quickly and easily test the core code and without having to go through all the deployment steps. It was for this purpose that I originally created this new command, gl-setlistcontenttypes. I did find later that the command ended up being real useful in a scripted deployment that I'm working on so I managed to knock out a well tested new command and some reusable code for a Feature that I'm working on.
The code is really simple - I've got a primary method, SetContentTypes, which takes in 3 string arrays containing the content types to remove and add as well as another array for the new button order. There's also a couple of helper methods that are called. I then just loop through each array and call the necessary methods (Add or Delete) on the ContentTypes property which returns an SPContentTypeCollection object. The only tricky part is setting the order of the content types - this is done via RootFolder property. There's a UniqueContentTypeOrder property that must be set to an IList<SPContentType> object. Manipulating the IList object directly will not work as it does not set the dirty flag and therefore the Update() method will appear to do nothing so you must create a new object and reset the property:
The help for the command is shown below:
C:\>stsadm -help gl-setlistcontenttypes stsadm -o gl-setlistcontenttypes Adds or removes content typews associated with the given list. Parameters: -url <list view url> [-add <comma separated list of content type names to add to the list>] [-remove <comma separated list of content type names to remove from the list>] [-order <new button order, comma separated, first will be the default>]
The following table summarizes the command and its various parameters:
|Command Name||Availability||Build Date|
|gl-setlistcontenttypes||WSS v3, MOSS 2007||8/6/2008|
|Parameter Name||Short Form||Required||Description||Example Usage|
|url||Yes||The URL to the list of which the actions will be performed||-url http://portal/Lists/Events|
|add||a||No||A comma separated list of content type names to add to the list. It's recommended to wrap the value in quotes if more than one.||-add "Holiday,Vacation,Company Event"
-a "Holiday,Vacation,Company Event"
|remove||r||No||A comma separated list of content type names to remove from the list. It's recommended to wrap the value in quotes if more than one.||-remove Event
|order||No||A comma separated list of content type names. Sets the order of the items in the "New" button of the list. The first item will be the default content type.||-order "Vacation,Holiday,Company Event"|
The following is an example of how to set the content types for a list:
stsadm -o gl-setlistcontenttypes -url http://portal/Lists/Events -add "Holiday,Vacation,Company Events" -remove "Event" -order "Vacation,Holiday,Company Event"