SharePoint 2010 Conditional Formatting for Lists using Visual Studio

This post might be very interesting for business and also developers, who wants to use a cool feature in a SharePoint 2010 list. It is about conditional formatting of list elements. So if the status of an elemen has a special condition, the background-color of the row or of the column will be changed. Normally you would use SharePoint Designer (SPD) to format the rows dependent to a special condition.

An easy way which i would not refuse to use – BUT what if you are working for a customer and you have a solution in Visual Studio with different projects and you have to deploy a new version to different servers (Development, Staging and Production Server)?

In this situation you will have to customize one or more lists after deployment with SPD in order to restore the conditional formatting. That is not i want to do after each deployment and i guess the same is with you, right?

Well, let me explain how to get the conditional formatting into you Visual Studio project. I found a experimental how to at this post from Ingo Karstein, which i checked and which worked for me.

Step 1: Deploy your list schema

In your Visual Studio Project create a list schema and deploy your list schema in your site. After that create a list instance of the list schema. It is without conditional formatting now.

Step 2: Open the list view with SharePoint Designer 2010

Now you can select the row of the list and add conditional formatting in SPD, e.g. if the status column is “Abgeschlossen” or “Done” then the background color should be green.

Now open the code view of the list view and search for the <xsl> tag of the XsltListViewWebPart that renders the list data. Copy the content of the whole <xsl> tag in you clipboard.

Step 3: Open the list schema.xml of your Visual Studio Project

Open the schema.xml file of the list definition. There you find different views, normally you have a default view in the <views> tag. Look for the view you want to edit and add the following before the closing </view> tag:

<xsl>
<![CDATA[

]]
</xsl>

Replace the “…” with the content of the clipboard, which you can paste between the CDATA tag.

Step 4: Deploy your project

If you now deploy your project, the list displays the conditional formatting and it is working all the time after each deployment. That is pretty cool.

But one thing does not work, as the post i recommended before also mentioned this: After deployment the conditional formatting cannot be edited by SPD. This can be ignored cause your users will not work with SPD or you can choose another option in order to work with it.

Step 5: Make it more flexibel and manageable with SPD

Add a mapped folder to your Visual Studio Project to the XSL Folder of the 14 Hive (/Templates/Layouts/XSL) and add to this mapped folder a XSL File. Call it myFormat.xsl.

Add the content of the <xsl> tag into this file and save it.

Now open the schema.xml file of your list definition again. Delete the xsl which you copied in step 3 and look for the reference to the main.xsl. It looks like this:  <XslLink Default=”TRUE”>main.xsl</XslLink>

This reference goes to the xsl file folder of the 14 hive.Replace the reference to the xsl file myFormat.xsl which you created shortly before.

If you deploy your project now, you will see the same results as after step 4, but, if you open SPD this time, you will see that SPD recognizes the conditional formatting. That means: Now you can see the conditional formatting, your users can see it with SPD, but you cannot change the conditional formatting. The webpart will not work after changing with SPD. So if you have any idea how to achieve this, it would be great to get your idea!

The fine thing is: You can deploy the conditional formatting with your Visual Studio project on each server, each system and each SharePoint and do not have to use SPD each time to get the old results back.

..:: I LIKE SHAREPOINT ::..

The article or information provided here represents completely my own personal view & thought. It is recommended to test the content or scripts of the site in the lab, before making use in the production environment & use it completely at your own risk. The articles, scripts, suggestions or tricks published on the site are provided AS-IS with no warranties or guarantees and confers no rights.

Karsten Pohnke About Karsten Pohnke
He is Consultant for SharePoint Solutions for collaboration, communication and business processes. He provides his customers applications based on standard features as well as development or combining the power of several microsoft tools like Dynamics CRM. In his free time he tries to collect tipps and worthy experience in this blog.

1 comment on “SharePoint 2010 Conditional Formatting for Lists using Visual Studio

  1. Could please make a SAMPLE PROJECT available for download that includes the schema with the xsl.

    Thank you this is great post!

Submit comment

Allowed HTML tags: <a href="http://google.com">google</a> <strong>bold</strong> <em>emphasized</em> <code>code</code> <blockquote>
quote
</blockquote>

Please fill in the captcha: * Time limit is exhausted. Please reload CAPTCHA.