Wednesday, November 21, 2007

Set custom properties of a custom field in List Definition

Problem:

I made a custom field, named "CrossSiteLookupFilterFieldControl". This field has some properties:

...

<PropertySchema>

<Fields>

<Field Name="SelectedSite" DisplayName="Site" Type="Text" Hidden="TRUE" />
<Field Name="SelectedList" DisplayName="List" Type="Text" Hidden="TRUE" />
<Field Name="SelectedColumn" DisplayName="Column" Type="Text" Hidden="TRUE" />
<Field Name="FilterValue" DisplayName="Filter value" Type="Text" Hidden="TRUE" />
<Field Name="FilterQuery" DisplayName="Filter query" Type="Text" Hidden="TRUE" />

</Fields>

</PropertySchema>
...

I want to use this field in a Custom List, so I add the following to schema.xml of my List:

<Field ID="{222EF3E9-3470-4619-89EC-7FFCDA18A3FC}" Name="Segment"  SourceID="{F9C3BB43-1EAA-4ac1-8878-EDBD7AFF3B4C}" StaticName="Segment"  Type="CrossSiteLookupFilterFieldControl" DisplayName="Segment" />

This works. But if I want to add a value for the FilterQuery property of the field, something like this doesn't work automatically:

<Field ID="{222EF3E9-3470-4619-89EC-7FFCDA18A3FC}" FilterQuery="Test" Name="Segment"  SourceID="{F9C3BB43-1EAA-4ac1-8878-EDBD7AFF3B4C}" StaticName="Segment"  Type="CrossSiteLookupFilterFieldControl" DisplayName="Segment" />

Solution

In the constructor of your custom field, you can read out the SchemaXml, and find the desired attributes. This way, you can set the values of your custom field with the values in the attributes:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(this.SchemaXml);
XmlAttributeCollection fieldAttributes = xmlDoc.FirstChild.Attributes;

if (fieldAttributes["FilterQuery"] != null)
                this.FilterQuery = fieldAttributes["FilterQuery"].Value;

 

PS: More about custom field definitions later..

1 comment:

public key infrastructure said...

Hi there! glad to drop by your page and found these very interesting and informative stuff. Thanks for sharing, keep it up!