0

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!

Quiche31
  • 119
  • 10

1 Answers1

0

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.

Madhur Ahuja
  • 22,211
  • 14
  • 71
  • 124
  • Thanks for your answer. You seem to imply that a controller (where a code similar to the one above, would run) is needed, in order to interact with other columns. Does it mean that there is no way, from a given field alone, to access the values of other fields, other than by using a controller? – Quiche31 Mar 02 '12 at 12:45