Click to Rate and Give Feedback
Community Content
In this section
Statistics Annotations (0)
Collapse All/Expand All Collapse All
This page is specific to
The 2007 product release

Other versions are also available for the following:
SPFieldCalculated Class (Microsoft.SharePoint)
Represents a calculated field in a list.

Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
Visual Basic (Declaration)
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel:=True)> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel:=True)> _
Public Class SPFieldCalculated
    Inherits SPField
Visual Basic (Usage)
Dim instance As SPFieldCalculated
C#
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel=true)] 
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel=true)] 
public class SPFieldCalculated : SPField

If you set the properties of the SPFieldCalculated class, you must call the Update method for changes to take effect in the database.

Use the Formula property to implement a formula in a calculated field. For information about the different kinds of formulas that you can use, see Calculated Field Formulas.

NoteNote:

Windows SharePoint Services 3.0 does not support inheriting from this class.

The following example creates a calculated field in a list and adds it to a view. The calculated field displays "Yes" if the value of MyColumn2 is greater than MyColumn1.

Visual Basic
Dim webSite As SPWeb = SPContext.Current.Site.AllWebs("MyWebSite")
Try
    Dim list As SPList = webSite.Lists("MyList")
    Dim fields As SPFieldCollection = list.Fields

    Dim newFieldName As String = fields.Add("MyNewColumn", SPFieldType.Calculated, False)
    Dim newField As SPFieldCalculated = CType(fields(newFieldName), SPFieldCalculated)
    newField.Formula = "=[MyColumn1]<[MyColumn2]"
    newField.Update()

    Dim view As SPView = list.Views("MyView")
    Dim viewFields As SPViewFieldCollection = view.ViewFields
    viewFields.Add(newFieldName)
    view.Update()
Finally
    webSite.Dispose()
End Try
C#
using (SPWeb oWebsite = SPContext.Current.Site.AllWebs["Website_Name"])
{
    SPList oList = oWebsite.Lists["MyList"];
    SPFieldCollection collFields = oList.Fields;

    string strNewFieldName = collFields.Add("MyNewColumn", 
        SPFieldType.Calculated, false);
    SPFieldCalculated strNewField =       
        (SPFieldCalculated)collFields[strNewFieldName];
    strNewField.Formula = "=[Column1]<[Column2]";
    strNewField.Update();

    SPView oView = oList.Views["MyView"];
    SPViewFieldCollection collViewFields = oView.ViewFields;
    collViewFields.Add(strNewFieldName);
    oView.Update();
}   
NoteNote:

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Best Practices: Using Disposable Windows SharePoint Services Objects.

System.Object
   Microsoft.SharePoint.SPField
    Microsoft.SharePoint.SPFieldCalculated
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker