Share via


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 repeat passage of the rendered content through the CAML interpreter, which allows CAML to render CAML.
MatchVar Optional Text. Used to reference a variable.
Select Required Text. The namespace to enumerate.
Parent Elements Child Elements
Case, ForEach, RenderPattern, Then, XML ForEach, HTML, IfEqual, Property, ScriptQuote, Switch

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.
  • /LISTS/List for enumerating all the lists in the current team Web site.
  • /FORMS/Form for enumerating all the forms for a particular list (very similar to views).
  • /LISTTEMPLATES/ListTemplate for enumerating all the server-supported list templates as defined in ONET.XML (for announcements, tasks, contacts, events, and so forth).
  • /DOCUMENTTEMPLATES/DocumentTemplate for enumerating all the server-supported document library template files (Microsoft Excel spreadsheet, Microsoft Word document, and so forth).

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 and the MatchVar attribute is used to reference that variable.

Example

The following example enumerates through all the fields in a Fields collection and sets the variable vDisplayName equal to the DisplayName or the Title field.

<ows:XML>
  <SetVar Name="Field">Title</SetVar>
  <ForEach Select="/FIELDS/Field" MatchVar="Field">
    <SetVar Name="vDisplayName" Scope=" Request"><Property Select="DisplayName"/><SetVar>
  </ForEach>
  </SetVar>
</ows:XML>

The following example prints out the display names of all the fields in a list and, if any of them are choice fields, it also prints out all of the choices.

<ows:XML>
  <SetList>Tasks</SetList>
  <ForEach Select="/FIELDS/Field">
    <Property Select="DisplayName" HTMLEncode="TRUE"/>
    <Switch>
      <Expr><Property Select="Type"/></Expr>
      <Case Value="Choice">
        <HTML><![CDATA[: ]]></HTML>
        <ForEach Select="CHOICES/CHOICE">
          <Property Select="." HTMLEncode="TRUE"/>
          <HTML><![CDATA[ ]]></HTML>
        </ForEach>
      </Case>
    </Switch>
    <HTML><![CDATA[<br>]]></HTML>
  </ForEach>
</ows:XML>

See Also

Universal Attributes for Page Rendering Elements