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 theContentByQueryWebPart
(CQWP) comes in.
But how do I get the icon of the document?
The result
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 inDOCICON.XML
?
Give me my icon
- Create your ContentByQueryWebPart (CQWP) and export it.
- Export the webpart
- Locate the
CommonViewFields
-property and change it from<property name="CommonViewFields" type="string" />
to
<property name="CommonViewFields" type="string">HTML_x0020_File_x0020_Type,Text;File_x0020_Type,Text</property>
- Edit the ItemStyle.xsl or even better create your own My_ItemsStyle.xsl and create a custom template which uses these new
ViewFields
.<xsl:template name="TitleOnlyWithIcon" match="Row[@Style='TitleOnlyWithIcon']" mode="itemstyle"> <xsl:variable name="SafeLinkUrl"> <xsl:call-template name="OuterTemplate.GetSafeLink"> <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/> </xsl:call-template> </xsl:variable> <xsl:variable name="DisplayTitle"> <xsl:call-template name="OuterTemplate.GetTitle"> <xsl:with-param name="Title" select="@Title"/> <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/> </xsl:call-template> </xsl:variable> <xsl:variable name="LinkTarget"> <xsl:if test="@OpenInNewWindow = 'True'" >_blank</xsl:if> </xsl:variable> <div class="item link-item"> <!-- Here comes the icon --> <span class="icon"> <img src="/_layouts/images/{ddwrt:MapToIcon(string(@HTML_x005F_x0020_File_x005F_x0020_Type),string(@File_x005F_x0020_Type))}" alt="{@DocIcon}" style="vertical-align: middle;margin-right:4px" /> </span> <a href="{$SafeLinkUrl}" target="{$LinkTarget}" title="{@LinkToolTip}"> <xsl:value-of select="$DisplayTitle"/> </a> </div> </xsl:template>
- Change the
ItemStyle
-property inside the webpart-definition to match your custom template. (TitleOnlyWithIcon
in 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 ddwrt
-namespace.
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.
Conclusion
The use of the MapToIcon
-function is the best method to get the icons. No need for custom “xsl:choose
-statements” or “/_layouts/images/ic{@DocIcon}.gif
-hacks”.
Technical Solutions Professional | Modern Workplace Architect Lead | Microsoft 365 | SharePoint | Power Platform | MS Teams | React
Leave a Reply