Add the icon (DocIcon) of the file-type to a custom xsl-template when using the ContentByQueryWebPart (CQWP)
In one of my current projects it was required to show the recent documents inside the site collection along with its associated icon. Of course our friend the
ContentByQueryWebPart (CQWP) comes in.
But how do I get the icon of the document?
Don’t do it again
One could start to use a long list inside an
xsl:choose to fetch each extension… But why do a file-extension mapping again when this is already specified in
Give me my icon
- Create your ContentByQueryWebPart (CQWP) and export it.
- Export the webpart
- Locate the
CommonViewFields-property and change it from
- Edit the ItemStyle.xsl or even better create your own My_ItemsStyle.xsl and create a custom template which uses these new
- Change the
ItemStyle-property inside the webpart-definition to match your custom template. (
TitleOnlyWithIconin this case).
- Import the webpart
Again there is some magic
If you refresh your page I’m sure you get an error. The magic here is to tell the xsl-stylesheet where to find the
MapToIcon-function which resides inside the
MapToIcon-function does the mapping of the icon by taking the mapping-information from the
DOCICON.xml. So even if your icon is not named like the SharePoint ones the icon will be found. My icon here was named
icon-pdf.gif for the pdf-file.
The use of the
MapToIcon-function is the best method to get the icons. No need for custom “
xsl:choose-statements” or “
SharePoint Team Lead , Office 365 consultant