Second Life of a Hungarian SharePoint Geek

September 20, 2011

Applying conditional formatting based on a field that is not displayed in the view

Filed under: Conditional formatting, SP 2010 — Tags: , — Peter Holpar @ 22:42

The other day I had to apply conditional formatting for a row in a view based on the status of the item. I had to mark each status with a different background color.

The status was stored as a text field called RequestState. Since the color of the row already would have signed the status of the item, I have not included the status field itself into the view to avoid the redundant information.

I created the rules and styles for conditional formatting, and it worked great in the preview in SharePoint Designer. Although when checked with the browser, no formatting was applied. It was not hard to realize that the source of the problem is that the RequestState field is not included in the view, especially when the formatting started to work after I’ve added the field of the view.

You can add the field to the view using the standard Edit View page on the web UI, or by adding a FieldRef to your field in the view definition, for example editing the .aspx page of the view using SPD.

<ViewFields>
…. your other field refs are here …
    <FieldRef Name="RequestState"/>
</ViewFields>

Note: I should say, it was not the first time the preview in SharePoint Designer produced different rendering than the real view in SharePoint.

However, adding the field to the view has the by-design side effect that the value of the field is displayed on the web UI. That is usually your intent when adding a field to a view, but as I’ve mentioned, displaying the status was undesired as the users were able to identify different states based on the colors and displaying three-letter technical status codes intended to use only internally was not much help for them.

Fortunately, it is quite easy to remove the field from the rendered view, you simply have to select the column using SPD and delete it:

image

Or even easier, you should apply these two templates (one for the header, other one for the content in the body) in the XSL of the view:

<xsl:template name="FieldRef_header.RequestState" ddwrt:dvt_mode="header" match="FieldRef[@Name=’RequestState’]" mode="header" ddwrt:ghost="" xmlns:ddwrt2="urn:frontpage:internal" />   

<xsl:template name="FieldRef_printTableCell_EcbAllowed.RequestState" match="FieldRef[@Name=’RequestState’]" mode="printTableCellEcbAllowed" ddwrt:dvt_mode="body" ddwrt:ghost="" xmlns:ddwrt2="urn:frontpage:internal" />

After saving the modified page in SPD, the field was removed from the (rendered) view, however, the conditional formatting still worked as planned.

Note: If you would like to apply these templates, do not forget to replace RequestState to your actual field name!

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: