One thing that’s been hanging over my head for a while is what to do about the Site Directory. The first problem was to get the business users to decide on where it should live (the master site directory that is) and what columns (or meta data) should be part of the directory (either new columns or changes to existing columns). Once I finally got that information I had to solve my second problem which was to find a way to script all the changes. I’ve got commands for moving lists and list items but I had no ability to add new fields or update existing fields.
After looking at the SPField object some I discovered that the SchemaXml property has a getter and a setter and that I could add new fields via the SPFieldCollection.AddFieldAsXml() method which takes in the same XML that you can get via the SchemaXml property. With this information I concluded that I could very easily create commands to export, import, and update a list field.
The commands I created are: gl-exportlistfield, gl-importlistfield, and gl-updatelistfield. Now I could set up a field in my test environment, export the field out to an XML file and then import or update that field during the upgrade (depending on whether it’s a new field or changes to an existing field). Fortunately the code to do all of this was extremely simple and it only took me a few minutes to create and test each one. The import and export are the simplest and least likely to throw errors – the update could cause you issues if you are attempting to make invalid changes (like changing the type from a Text type to a Choice type – I don’t do any validation of your XML and rely completely on Microsoft’s internal validation). The commands I created are detailed below.
Because I already had helper methods to get a field the code for this became basically just one line of text: File.WriteAllText(Params["outputfile"].Value, field.SchemaXml); The rest of the code is in a utility method which I’ve previously shown in other posts so won’t show again here. The syntax of the command can be seen below:
C:\>stsadm -help gl-exportlistfield stsadm -o gl-exportlistfield Exports a list field (column) to a file. Parameters: -url <list view URL> -fielddisplayname <field display name> / -fieldinternalname <field internal name> -outputfile <file to output field schema to>
Here’s an example of how export the DivisionMulti field to a file:
stsadm –o gl-exportlistfield -url "http://intranet/SiteDirectory/SitesList/AllItems.aspx" -fieldinternalname DivisionMulti -outputfile "c:\divisionmulti.xml"
Running the above command will produce results similar to the following :
Once we have our list field exported we can then import the field into another list either as is or with whatever manual modifications you may have made (just be careful that you know what you are doing – it’s always better to make the modifications using the browser and then export those changes than it is to try and hack the XML directly).
By default when you run the import command the code will attempt to locate the "ID" attribute and replace it with a new GUID value – otherwise you may get errors stating that the field already exists (even if you’ve changed the "Name" attribute). If you don’t want the code to do this then you can pass in the "-retainobjectidentity" parameter. Note that I’m not doing anything with the "SourceID" attribute as I couldn’t detect any issues with keeping the value unchanged.
Like the export command there’s really not much to the code – I get the SPList object, load up the XML, replace the ID attribute if needed, and then call AddFieldAsXml() and then call the ReorderField method (detailed at the end of this post in the 11/9/2007 update):
The syntax of the command can be seen below:
stsadm -o gl-importlistfield Imports a field (column) into a list. Parameters: -url <list view URL> -inputfile <input file containing field schema information> [-addfieldoptions <default | addtodefaultcontenttype | addtonocontenttype | addtoallcontenttypes | addfieldinternalnamehint | addfieldtodefaultview | addfieldcheckdisplayname> [-addtodefaultview] [-retainobjectidentity] [-sortindex <field order index>]
Here’s an example of how to import the field exported above into a new list (note that it assumes that the DivisionMulti field does not exist in the target list and that there’s no field with a display name of "Division")and setting the sort order index to zero thus making it the first item in the list:
stsadm –o gl-importlistfield -url "http://teamsites/sitedirectory/siteslist/allitems.aspx" -inputfile "c:\divisionmulti.xml" -addfieldoptions addfieldcheckdisplayname -addtodefaultview -sortindex 0
When I first set out to create this command I originally thought it was going to be a real pain in the @$$ but then I discovered that the SchemaXml property had a setter and life got a whole lot easier. What I thought would end up being hundreds of lines of code to deal with all the possible changes ended up being one core line plus a few others just to get the data. I had to a bit of additional complexity though to deal with the ability to determine the field to edit via the name parameters and then to deal with the fact that I wanted to be able to edit the sort order without having to change anything else (thus making the inputfile optional):
The syntax of the command can be seen below:
C:\>stsadm -help gl-updatelistfield stsadm -o gl-updatelistfield Updates a field (column) using the provided input XML. Use exportlistfield to get the existing schema and then modify the results (note that the 'Name' attribute of the Field node must not change unless the fieldinternalname or fielddisplayname is passed in as this attribute is what is used to determine which field to update). Parameters: -url <list view URL> [-inputfile <input file containing the field schema information>] [-fielddisplayname <field display name> / -fieldinternalname <field internal name>] [-sortindex <field order index>]
Here’s an example of how to update the field exported above with changes made to the resultant XML file (such as adding new choice elements) and setting the sort order index to zero thus making it the first item in the list:
stsadm –o gl-updatelistfield -url "http://intranet/sitedirectory/siteslist/allitems.aspx" -inputfile "c:\divisionmulti.xml" -sortindex 0
I thought about using the "ID" attribute within the XML to locate the field to update but in the end I decided that someone may want to use this to change the ID for whatever reason and they’re less likely to want to change the internal name.
Update 11/9/2007: I’ve modified the gl-updatelistfield and gl-importlistfield commands so that they now support the passing in of a sortindex parameter which effectively does what it says – it changes the field order. I’ve updated the content above to reflect the changes. I wish that this were one of those easy things to implement but it turns out that it was a real pain – fortunately I found a post by Michael Ekegren which discusses how to do this using the ProcessBatchData method of the SPWeb object. I’ve included the code that makes this work below: