SharePoint 2010: How to Customize List Content Display using Content Query Web Part
This post is going to describe how we can use Content Query Web Part to Style SharePoint List. Assume we have a SharePoint list called Projects with the following fields:
http://lh6.ggpht.com/-yhRVy3WMEf8/TvBavazwTSI/AAAAAAAAAs0/gvVaF1lqHv0/image_thumb1.png?imgmax=800
As you already know, the default look and feel would be like this:
http://lh5.ggpht.com/-h2KRa8JKMxA/TvBax3NpyXI/AAAAAAAAAtE/UhdkLOyNOWM/image_thumb7.png?imgmax=800
Let's render this list in a format something like this:
http://lh6.ggpht.com/-vrYdnidMZ4o/TvBa0aMs08I/AAAAAAAAAtU/mTA6D791nfc/image_thumb8.png?imgmax=800
Step One
First you need to define some names for these place holders (This may not make any sense to you but don’t worry, just do the step). Let's define these columns as follows:
- Title –> MyTitle
- Logo –> MyLogo
- Project Summary –> MySummary
- Project Description –> MyDesc
Step Two
Now you want to define an XSL Style Sheet using these names. Let's use <table> to layout the list item as below:
<xsl:template name="MyStyle" match="Row[@Style='MyStyle']" mode="itemstyle">
<table border='1'>
<tr>
<td colspan="2"><h3><xsl:value-of select="@MyTitle" /></h3></td>
</tr>
<tr>
<td>
<img>
<xsl:attribute name="src">
<xsl:value-of select="@MyLogo" />
</xsl:attribute>
</img>
</td>
<td>
<xsl:value-of disable-output-escaping="yes" select="@MySummary" />
</td>
</tr>
<tr>
<td colspan="2">
<xsl:value-of disable-output-escaping="yes" select="@MyDesc" />
</td>
</tr>
</table>
</xsl:template>
XSLT is used to position the content inside HTML tags. There are a few attributes which are mandatory in this section.
- Name and Match should have equal names.
- name =’X’ match=Row[@style=’X’]
- mode = ‘itemstyle’
Now you have created an XSLT which can be given as a list style in SharePoint.
Step Three
Here we are going to put XSLT into the SharePoint.
First you need to open the SharePoint designer with the particular site.Then navigate to All Files and Style Library.
http://lh6.ggpht.com/-DN5Sb6ifT6s/TvBa2bF8qCI/AAAAAAAAAtk/1NLzKrGGfuw/image_thumb1%25255B1%25255D.png?imgmax=800
Here you can see many Folders inside. Now you have to go to the XSL Style Sheet Folder.
http://lh3.ggpht.com/-gN60ixoORF8/TvBa5_ZfdmI/AAAAAAAAAt0/9acUUd2pk30/image_thumb2.png?imgmax=800
Now you can see XSL files which are used by SharePoint to render its content. Here we are using ItemStyle.xsl to enable custom rendering.
http://lh4.ggpht.com/-g5aJc960nj0/TvBa7_2k2SI/AAAAAAAAAuE/6H2khAzLZGA/image_thumb3.png?imgmax=800
Open this XSL file and put your XSL (the one created in Step Two) in the bottom of the XSL as follows:
http://lh5.ggpht.com/-NWPAVHgO0rg/TvBa9529BWI/AAAAAAAAAuU/SmxTvwMO1r8/image_thumb5.png?imgmax=800
Now the difficult things are over.
Step Four
- Add a Content Query Web Part
http://lh5.ggpht.com/-2r-Rb0y79CI/TvBbB9NFDKI/AAAAAAAAAuk/ew91elOUYFQ/image_thumb7%25255B1%25255D.png?imgmax=800
- Go to edit web part and and browse your list (here the list is Our Projects).
http://lh5.ggpht.com/-VJVjzsQe090/TvBbEElEZjI/AAAAAAAAAu0/iCqmt0TLzHs/image_thumb9.png?imgmax=800
- Go to presentation section and go down a little bit. You can see Styles drop down.
- Now you can see your style.
http://lh6.ggpht.com/-xhsGU4vDOm0/TvBbGv53SII/AAAAAAAAAvE/ttmLeA6s5Mk/image_thumb13.png?imgmax=800
- Select your custom XSL (here MySample).
- Once selected it looks like below.
http://lh3.ggpht.com/-jJBhzucfyLU/TvBbJKaCQ-I/AAAAAAAAAvU/tUDigmXGZhs/image_thumb15.png?imgmax=800
- Now you need to put the actual list column names into these text boxes as follows (Title column should be used with the scope).
http://lh4.ggpht.com/-sOPB1Lbfbgo/TvBbNiwJbuI/AAAAAAAAAvk/89ZcOLrnseI/image_thumb17.png?imgmax=800
- Click OK to Save. You can see your new list rendering now.
http://lh3.ggpht.com/-ctwP9GSgyAI/TvBbQuqUi0I/AAAAAAAAAv0/lYmjqkZxgaw/image_thumb19.png?imgmax=800