How to show file/folder icons depending on the type in Content Query Webpart
Requirement:
- How to show file/folder icons depending on the type in Content Query Webpart
POINTS to Remember:
- NOTE:
- In any other Site except of Publishing Type, activate the following two features in your site, to have Content Query Webpart
- In Site Collection Features: Office SharePoint Server Publishing Infrastructure
- In Site Features: Office SharePoint Server Publishing
Attachment:
- The attachment contains Sample_Files zip which in turn contains 2 files for reference:
- webpart file
- ItemStyle.xsl
Steps to REPRO the OOB functionality:
- ADD Content Query Webpart to your Webpart page
- Set the query properties to display the items from specific site or list of any content or specific content type.
- The result are shown, BUT whether the shown item is doc, xls, pdf , folder or any other type is not visible.
- The resolution for the same is below
Resolution / WorkAround:
- There are 2 fields which determine whether its is file or folder. And if it is file then which type:
- File Type
- HTML File Type
- The above field names are the Display Names. Their Internal Names are:
- File_x0020_Type
- HTML_x0020_File_x0020_Type (Note: The space is replaced by "_x0020_")
- Now, add the Content query webpart to the page. (OOB webpart)
- Export the webpart (by clicking on the webpart right side small arrow icon: Export)
- Save the *.webpart file
- Open the file in notepad
- Search for word "CommonViewFields"
- Change the property as below:
<property type="string" name="CommonViewFields">File_x0020_Type, Text;HTML_x0020_File_x0020_Type, Text</property> - Now, save the *.webpart file
- Go the respective SharePoint Site>> Site Settings >> Click on Webpart Gallery
- Now, Upload the *.webpart file . (Make sure to give Name & Title differently)
- Now, open the siet in SharePoint Designer
- Browse to Style Library >> XSL Style Sheets >> ItemStyle.xsl . Checkout the file
- Add the following namespace to the <xsl:stylesheet> ( this is the root node) tag
xmlns:ddwrt="https://schemas.microsoft.com/WebParts/v2/DataView/runtime" - Now, append the following custom style template to the existing templates (add inside the root node of <xsl:stylesheet> )in the ItemStyle.xsl :
<xsl:template name="MyCustomStyle" match="Row[@Style='MyCustomStyle']' mode='itemstyle">
<xsl:variable name="SafeImageUrl">
<xsl:call-template name="OuterTemplate.GetSafeStaticUrl">
<xsl:with-param name="UrlColumnName" select="'ImageUrl'"/>
</xsl:call-template>
</xsl:variable>
<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 id="linkitem" class="item">
<xsl:variable name="CustomVariable1">
<xsl:value-of select="@File_x005F_x0020_Type" />
</xsl:variable>
<xsl:choose>
<xsl:when test="string-length($CustomVariable1) != 0">
<div class="image-area-left">
<a href="{$SafeLinkUrl}" mce_href="{$SafeLinkUrl}" target="{$LinkTarget}">
<img class="image" alt="Type:{@File_x005F_x0020_Type} " src= "/_layouts/images/{ddwrt:MapToIcon(string(@HTML_x005F_x0020_File_x005F_x0020_Type),string(@File_x005F_x0020_Type))}" />
</a>
</div>
</xsl:when>
<xsl:otherwise>
<div class="image-area-left">
<a href="{$SafeLinkUrl}" mce_href="{$SafeLinkUrl}" target="{$LinkTarget}">
<img class="image" src= "/_layouts/images/FOLDER.GIF" alt="Type:Folder" />
</a>
</div>
</xsl:otherwise>
</xsl:choose>
<div class="link-item">
<xsl:call-template name="OuterTemplate.CallPresenceStatusIconTemplate"/>
<a href="{$SafeLinkUrl}" mce_href="{$SafeLinkUrl}" target="{$LinkTarget}" title="{@LinkToolTip}">
<xsl:value-of select="$DisplayTitle"/>
</a>
<div class="description">
<xsl:value-of select="@Description" />
</div>
</div>
</div>
</xsl:template> - Save the file & check in.
- Now, Add the webpart (which is uploaded in Point 11) to the page
- Edit the page and browse the webpart properties (Edit >> Modify Webpart)
- Make sure to select appropriate values in the Query section: for List Type & Source
- Open section Presentation and check for ItemStyle Field
- Select the custom style name (here is "MyCustomStyle") from the dropdown
- Save the changes and check the webpart.
End Result: