ForEach Element

The ForEach element provides a basic mechanism for enumerating a collection of views or fields defined in the current list.

Syntax

<ForEach
  ExpandXML = "TRUE" | "FALSE"
  MatchVar = "Text"
  Select = "Text">
</ForEach>

Attributes

Name Description
ExpandXML Optional Boolean. TRUE to repass the rendered content through the Collaborative Application Markup Language (CAML) interpreter, which allows CAML to render CAML.
MatchVar Optional Text. Used to reference a variable.
Select Required Text. The namespace to enumerate.

Remarks

The value of the Select attribute varies in the different schema files:

  • CHOICES/CHOICE inside a Choice field. Note capitalization and the lack of a leading forward slash (/). In this case, the ForEach element must be nested inside another ForEach element that enumerates Fields/Field (in other words, inside a Fields element). The current point in the Fields enumeration must be a Choice field.
  • /FIELDS/Field for enumerating all the fields in the current list.
  • /FORMS/Form for enumerating all the forms for a particular list (very similar to views).
  • /LISTTEMPLATES/ListTemplate for enumerating all the server-supported list definitions specified in the ONET.XML file (for announcements, tasks, contacts, events, and so on).
  • /DOCUMENTTEMPLATES/DocumentTemplate for enumerating all the server-supported document library template files (Microsoft Excel worksheet, Microsoft Word document, and so on).

The body of the ForEach element is rendered once for each view or field in the collection. The Property element can be used to extract individual properties of the view or field.

The ForEach element can also be used to reference a single field or view. To do so, a variable must be set with the name of the desired field or view; the MatchVar attribute is used to reference that variable.

Example

The following example uses the ForEach element to construct a drop-down list box.

<ForEach Select="CHOICES/CHOICE">
   <HTML>fld.AddChoice(</HTML>
   <ScriptQuote>
      <Property Select="."/>
   </ScriptQuote>
   <HTML>, </HTML>
   <ScriptQuote>
      <Property Select="Value"/>
   </ScriptQuote>
   <HTML>);</HTML>
</ForEach>