Tuesday, November 24, 2009

Modified date stopped working...

So here is the scenario - I had a piece of code in a feature that changed the title of the built-in site column "Date Modified" to...well..."Date Modified". As you can see, the code looks a bit useless, but someone thought it would be useful, so there it was. Next thing I know, the date modified field stopped working in out of the box document libraries. You would edit a document or its properties and the modification date would be the same as the creation date. This would even show in the version history...

Solution - I had to write a small piece of code that would set the SchemaXml property of the site column to the default. It turns out that if you change the field using code (like SPField.Title="Date Modified"; SPField.Update()) then sharepoint removes some very important tags from the schemaxml property of the field - which causes it to break. To fix I just copied over the SchemaXml property from a site column in a brand new site that was not damaged.

Code sample:



using (SPSite site = new SPSite(txtSite.Text))


using (SPWeb web = site.OpenWeb())


SPField field = web.Fields.GetField(txtField.Text);

if (field != null)


field.SchemaXml = txtSchema.Text;


MessageBox.Show("Operation completed successfuly");




MessageBox.Show("Field '" + txtField.Text + "' could not be found in the site columns in" + Environment.NewLine+ "'" + txtSite.Text + "'");



} } catch (Exception ex)

{MessageBox.Show("An error has occured : " + Environment.NewLine + ex.Message);}

Sample schema: <Field ID="{28cf69c5-fa48-462a-b5cd-27b6f9d2bd5f}" ColName="tp_Modified" RowOrdinal="0" ReadOnly="TRUE" Type="DateTime" Name="Modified" DisplayName="Date Modified" StorageTZ="TRUE" SourceID="http://schemas.microsoft.com/sharepoint/v3" StaticName="Modified" FromBaseType="TRUE"/>

No comments: