Wednesday, April 6, 2011

URL fields in SharePoint Content Query Web Part

When a content query web part is used to query and display the items in the Links list, i got the output in a very undesirable format.

As you can see in the image above, it is showing the URL of the link followed by a comma and then the link text. Also when you click on the link in will take you to the display form for that item in the links list instead of taking us directly to the URL of the link.
So i started to modify the XSL to get the desired bahaviour which is
  1. It should show only the link text without the URL
  2. The hyperlink should point to the URL rather that the display form for that item.
For this go to your site's Style Lirrary->XSL Style Sheets
Download a copy of the ItemStyle.xsl
I named the copy as CategoryItemStyle.xsl. Modify the content of it to this

Upload the CategoryItemStyle.xsl to the Style library-> XSL Style Sheets.

Then export the default the Content Query Web Part and open in it in notepad or Visual Studio
Change the ItemXslLink property to the newly created style sheet

Save it and upload the web part to the web part gallery of your site collection.
Add this web part to a page and view the output now

As you can see, it no longer shows the URL for the hyperlink. Also the link points to for MSN, for Google, and so on as entered in the Links list, instead of the displayform for the corresponding item.