Question

I'm familiar with SharePoint Designer and have just moved to using Visual Studio. I am building a SharePoint Add-in here. I can't figure out how to add a calculated column into lists, however. The option simply isn't there. Advice, please?

Was it helpful?

Solution

You can create your own calculated column field definition, and then include that field in the list definition/schema (along with the other fields used in the calculation, of course).

The CAML for a calculated column looks roughly like this:

<Field ID="{900A6481-2EA0-4F7A-922D-F9596E0BF05F}"
       Name="MyCalculatedField"
       DisplayName="My Calculated Field"
       Type="Calculated"
       ResultType="Number"
       Group="My Columns">
  <Formula>=SUM([OtherColumn], [YetAnotherColumn])</Formula>
  <FieldRefs>
    <FieldRef Name="OtherColumn" />
    <FieldRef Name="YetAnotherColumn" />
  </FieldRefs>
</Field>

The key things to see here: the Type is Calculated, you will need to specify the ResultType, you can include the formula using the Formula child element, and you need to include FieldRefs for every other field you are using in the calculation.

Here is the MSDN documentation on the Field element schema.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top