SharePoint 2010 - Custom calculated column

1.3k views Asked by At

In a document library I need a custom calculated column, because the default Excel formula don't provide the functionality I need.

I created a custom field inheriting from SPFieldText, that I then could customize at will. The question is: how is it possible, from my custom field, to access the content values of the other fields of the document library?

In other world, in the overriden GetValidatedString method, how can I return a value that is dependent upon values from other fields, for the same record? How to implement getFieldValue() , below:

public class MyCustomField : SPFieldText
{
    ....
    public override string GetValidatedString(object value)
    {
        string value1 = getFieldValue("Column-Name1");
        string value2 = getFieldValue("Column-Name2");
        return value1 + ", " + value2; // any arbitrary operation on field values
    }
}

Thanks!

1

There are 1 answers

1
Madhur Ahuja On

You should be able to grab other values from the form using the Item property of the FormComponent or the Item property of the ItemContext.

Either of these should work from the FieldControl class:

Code Snippet

if ((this.ControlMode == SPControlMode.New) || (this.ControlMode == SPControlMode.Edit))

{

   object obj = this.Item["Name"];

   if (obj != null)

      string name = obj.ToString();



   object obj2 = base.ItemContext.Item["Name"];

   if (obj2 != null)

string name2 = obj2.ToString();

}

where "Name" is the internal name of the field that you wish to retrieve.