Custom Related Fields
Last modified: March 03, 2011
Applies to: SharePoint Foundation 2010
This topic explains how to use the RelatedField property to establish a relationship between two custom fields and to use it in rendering one of the fields on a list view.
This topic assumes that you are familiar with the topic How to: Customize the Rendering of a Field on a List View.
There may be scenarios when you want to specify a relationship between two custom fields. For example, you might have a Note field that renders its text on some list views, but on certain list views should render as one or the other of two small GIF images, depending on whether the note has any content. One way to accomplish this would be to have a second field based on the Hyperlink or Picture type. The second column is defined as hidden, but on specific views, the image renders in the Note field column instead of the Note contents.
The rendering logic is part of the XSLT style sheet that you create for your custom fields. The example in the topic How to: Customize the Rendering of a Field on a List View is a case in which the rendering of one field is governed partly by the value in another field. In that example, the relationship between the two fields was implicit in the fact that the two fields have the same name except that one adds a period (".") character to the end of the name. This makes it possible to turn a reference to the current field into a reference to the other field with the following XPath.
However, in some situations, there is no built-in relationship of which you can take advantage. In those situations, you can use the SPField.RelatedField property of the main field to designate the internal name of the related field. You can then use the following XPath to refer to the related field.
The variable $fieldDefinition would need to be defined as follows.
<xsl:variable name="fieldDefinition" select="$XmlDefinition/ViewFields/FieldRef[@Name=current()/@Name]"/>
The variable $XmlDefinition is a built-in variable that is defined as the current node of the source node tree, which in this context would be the View element.
With the relationship between a main field and a related field established, you can use your XSLT markup to specify how the value of the related field will affect the rendering of the main field. You can also have the value in the related field appear in addition to, or even in place of, the main field.