Sharepoint Insight by Namwar Rizvi

Sharepoint Tips, Tricks and inside knowledge from real world experience by Namwar Rizvi

Posts Tagged ‘CAML’

How to use Case statement in RenderPattern of a Custom Field Type

Posted by namwar on October 14, 2009


While designing a custom field type, you may sometime need to render different HTML based on the value of field. For example, if you want to display Red,Amber, Green traffic light images to highlight High,Medium, Low value then you need to dynamically switch image url based  on the value of the field.

In CAML, you have Case structure to implement this switching logic. It works very much like the C# switch-case statement. Following is an example of RenderPattern of a custom field which display different images based on the value of the field.

<RenderPattern Name="DisplayPattern">
      <Switch>
        <Expr>
          <Column />
        </Expr>
        <Case Value="1" >
          <HTML><![CDATA[<img alt="Red" src="/_layouts/IMAGES/ewr210m.gif" />]]></HTML>
         </Case>
        <Case Value="2" >
          <HTML><![CDATA[<img alt="Amber" src="/_layouts/IMAGES/ewr211m.gif"]]></HTML>
        </Case>
        <Case Value="3" >
          <HTML><![CDATA[<img alt="Green" src="/_layouts/IMAGES/ewr209m.gif"]]></HTML>
        </Case>
        <Default>
            <HTML><![CDATA[<span>No Value</span>]]></HTML>
        </Default>
      </Switch>
    </RenderPattern>

Advertisements

Posted in Tips | Tagged: , , , | Leave a Comment »