Calculated field

If you are defining a SharePoint list using declarative definitions in Visual Studio, you won’t find calculated field in the field types. I am using two examples, the first one is with date and the other one is with integer.

Date data type:

The solution will be create your field with any data type and go to the schema file and manually do the following changes:

  <Field Type="Calculated" DisplayName="ValidationDays" EnforceUniqueValues="FALSE" Indexed="FALSE" 
             Format="DateOnly" LCID="1033" ResultType="Text" ReadOnly="TRUE"
             ID="{776ce876-209f-4063-adb8-0829572049a2}" 
             StaticName="ValidationDays" Name="ValidationDays">
        <Formula>=StartDate-StopDate</Formula>
        <FormulaDisplayNames>=StartDate-StopDate</FormulaDisplayNames>
        <FieldRefs>
          <FieldRef Name="StopDate" />
          <FieldRef Name="StartDate" />
        </FieldRefs>
      </Field>
  • Type –> Calculated
  • ReadOnly –> True
  • Add the formula <Formula>=StartDate-StopDate</Formula>
  • Add the field ref

<FieldRefs>

<FieldRef Name=”StopDate” />

<FieldRef Name=”StartDate” />

</FieldRefs>

Deploy the feature to the SharePoint server and activate the feature.

Test the list.

Integer data type:

Repeat the same steps and the field definition in the schema field will be:

<Field Name="FreeTickets" ID="{90dfb1f0-257e-4530-b885-84842594323e}" Type="Calculated" >
				<Formula>= Tickets - SoldTickets</Formula>
<FieldRefs>
          <FieldRef Name="Tickets" />
          <FieldRef Name="SoldTickets" />
        </FieldRefs>
			</Field>

Deploy the feature to the SharePoint server and activate the feature.

Test the list.