Second Life of a Hungarian SharePoint Geek

August 30, 2011

Creating totals programmatically

Filed under: SP 2010, Totals, Views — Tags: , , — Peter Holpar @ 23:50

Displaying totals is a useful feature of views in SharePoint 2010 that enables users to create aggregations for specific type of fields (like text, date and numeric values) in the view. The types of aggregations available for a field depend on the field type. General discussion of totals are out of scope of this post, however, if you would like to learn more about totals, you can find information a bit more info about the feature here.

For the sake of this post, I’ve created a custom list and added two numerical and two date fields to it.

You could customize the view using the web UI to compute the maximum for the date and sum of the numeric values as illustrated below:


Sometimes we need to achieve the same result programmatically. To understand how the schema of the view is modified when we make the changes manually, I made the modifications through the UI, then checked the SchemaXml property of my view. The following nodes were added by the change:

  1. <Aggregations Value="On">
  2.   <FieldRef Name="PlannedNumber" Type="SUM"/>
  3.   <FieldRef Name="DueDate" Type="MAX"/>
  4.   <FieldRef Name="FinishedDate" Type="MAX"/>
  5.   <FieldRef Name="ActualNumber" Type="SUM"/>
  6. </Aggregations>

You can see that the type of the aggregation is set by the Type attribute of the FieldRef node, while the Name attribute refers to the field name in the view.

The SPView class contains two properties related to totals, the Aggregations and AggregationsStatus.

To set the totals we should run the following code:

  1. // view is a reference to the SPView instance
  2. // you would like to alter
  3. view.Aggregations = "<FieldRef Name='PlannedNumber' Type='SUM'/><FieldRef Name='DueDate' Type='MAX'/><FieldRef Name='FinishedDate' Type='MAX'/><FieldRef Name='ActualNumber' Type='SUM'/>";
  4. view.AggregationsStatus = "On";
  5. view.Update();

It is important to set the properties in the order illustrated above, since setting the Aggregations property creates the Aggregations node in the schema XML. If that node does not exist, setting the AggregationsStatus property throws an ArgumentException.

Note: There is a method called GetAggregationString that expects a single string parameter, the name of the field in the aggregations, and returns the type of the aggregation. In our case:


returns MAX.


Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blog at

%d bloggers like this: