Computed Field XML Definition

Today I wanted to create a XML based field definition for a computed field for a SharePoint 2010 solution. The requirement is to show the difference of two date/time fields in the column. The field should be used in an XML based content type that inherits from the document.

That should be easy to do. The field XML schema has all the necessary options. But as soon as I declared a field as type ‘Computed’ it was no longer deployed. Checking the site columns gallery via site settings on the site collections showed that the field was not available and therefore not part of the custom content type. Even the simplest field definition did not work:

<Field ID="{GUID}" Name="ComputedColumn" Type="Computed" />

As a next step I changed the type to calculated and that got the field deployed, appeared in the site columns gallery and was part of the content type as expected:

<Field ID="{GUID}" Name="CalculatedColumn" Type="Calculated">

  <Formula>=0</Formula>

</Field>

Then I added my custom content type with the CalculatedColumn to the Documents library of a collaboration site. The new site column appeared in the fields list on the document library settings page. But when uploading a document to that library I got the error message that the file I was uploading does not exist: <nativehr>0x81020030</nativehr><nativestack></nativestack>The URL ‘Documents/SomeDoc.docx’ is invalid. It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current web.

Removing the CalculatedColumn from the document library proved that it caused the error. So I added the calculated field via SharePoint UI instead and looked at the generated schema XML in SharePoint Manager. After a bit of trial and error I add a couple of attributes that made everything work:

<Field ID="{GUID}" Name="CalculatedColumn" Type="Calculated" ResultType="Text" ReadOnly="TRUE" Required="FALSE">

  <Formula>=INT((SomeDateColumn-Modified)*86400)&gt;=0</Formula>

  <FieldRefs>

    <FieldRef Name="SomeDateColumn" />

    <FieldRef Name="Modified" />

  </FieldRefs>

</Field>

What is the moral of the story? I do not know. Maybe you can get the Computed column to work properly with the right attributes.

2 thoughts on “Computed Field XML Definition

  1. Hi Brend,

    Many thanks for the article. It saved me a lot of time, as I was struggling to get my form solution working after i added a calculated field.

    Regards
    P

Leave a Reply

Your email address will not be published. Required fields are marked *