SetVar Element (View)

Last modified: July 15, 2011

Applies to: SharePoint Foundation 2013 | SharePoint Server 2013

In this article
Child Elements
Parent Elements

Allows variables to be set in the context of rendering the page—either locally to the current level of XML or globally to the page.

  ID = "Text"
  Name = "Text"
  Scope = "Request"
  Value = "Text">




Optional Text. Provides an ID for the variable.


Required Text. Specifies a name for the variable.


If set to Request, the variable is global.


Optional Text. Can be used to assign a value to the variable when this is an empty element.

Minimum: 0

Maximum: Unbounded

The SetVar element has both a spanning and a non-spanning form, so that <SetVar Name="MyVar">Announcements</SetVar> would be the same as <SetVar Name="MyVar" Value="Announcements/>.

If Scope="Request" is specified in the SetVar element, a variable assignment takes effect anywhere in the current page. Otherwise, the assignment affects only children of the SetVar element.

The SetVar element is often a child to a Method element used in batch processing of requests.

The SetVar element is frequently used to change the display mode by setting DisplayMode to one of the following values.




Used when rendering the ViewBody section of a view.


Used when rendering an edit item form.


Used when rendering the ViewHeader section of a view.


Used when rendering a new item form.


Used when editing a display form with Microsoft FrontPage.


Used when editing a new item form with FrontPage.


Used when editing an edit item form with FrontPage.

The following example illustrates using the SetVar element to set global scope for a variable. The second line returns the value set in the first line.

<SetVar Name="GlobalVar" Scope="Request">Value</SetVar>
<GetVar Name="GlobalVar">

In the following example, the first GetVar element returns "Value_2", and the second GetVar element returns "Value_1", because the SetVar element that contains "Value_2" applies only to children of the "Sample" element. "Value_2" goes out of scope after the closing "Sample" tag.

<SetVar Name="myVar">Value_1</SetVar>
      <SetVar Name="myVar">Value_2</SetVar>
      <GetVar Name="myVar"/>
   <GetVar Name="myVar"/>

The following example evaluates whether a field is required and, if it is required, creates the red asterisk (*) that is displayed beside required fields in New or Edit forms and sets the HasRequired variable to TRUE.

      <Property Select="Required"/>
   <Case Value="TRUE">
      <HTML><![CDATA[<font color=red> *</font>]]></HTML>
      <SetVar Scope="Request" Name="HasRequired">TRUE</SetVar>