ForEach element (View)

Applies to: SharePoint 2016 | SharePoint Foundation 2013 | SharePoint Online | SharePoint Server 2013

Provides a basic mechanism for enumerating a collection of views or fields defined in the current list.

<ForEach
  Select = "Text">
</ForEach>

Elements and attributes

The following sections describe attributes, child elements, and parent elements.

Attributes

Attribute Description
Select
Required Text. The namespace to enumerate.

Child elements

Numerous

Parent elements

Numerous

Occurrences

  • Minimum: 0
  • Maximum: Unbounded

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, etc.).

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>