Customising Content Query Web Part (CQWP) and Discussion Boards

I had a requirement to customise the CQWP in a way to display the number of replies on a discussion board item. and change the URL link of the item in CQWP to take the user to the threaded view of the discussion, rather than dispform.aspx  of the discussion item.

this post shows how to create an aggregate view of discussion boards in a site collection, and its sub sites, hence the usage of CQWP with the required customisations.

one objective here is to avoid .custom development, so this post will use the CQWP customisation features: customising item styles and viewing custom fields in a CQWP.

the final result will be similar to this:

image

 

few things that you should learn before attempting to follow the instructions:

1. how to customise item styles for a CQWP: https://msdn.microsoft.com/en-us/library/ms551040.aspx, and https://www.heathersolomon.com/blog/articles/CustomItemStyle.aspx 

2. Where does the number of replies come from?

A: an internal field on the Discussion list named: ItemChildCount. the field has a display name of “Replies”. look into the discussion board list feature schema for a complete list of fields.

3. how to return custom fields in a CQWP, the key is to use the CommonViewFields property to add custom fields, the method is described in details here: https://msdn.microsoft.com/en-us/library/ms497457.aspx

 

To prepare the item style:

1.  customise ItemStyle.xsl to add a new style that will show the number of replies

Replies:(<xsl:value-of select="substring-after(@ItemChildCount,'#') "/>)

 

2.  in the same item style, fix the URL link to point to the threaded view of the discussion item:

<xsl:variable name="vv1">
     <xsl:value-of select="@LinkUrl"/>
</xsl:variable>

<div class="ms-vb">
     <a href="{$vv1}" target="{$LinkTarget}" title="{@LinkToolTip}">
     <xsl:value-of select="$DisplayTitle"/>
</a>
</div >

(or copy and paste the below in your itemstyle.xsl before </xsl:stylesheet>)   

download file

 

Customising CQWP properties:

1. drop a CQWP on your page, build you’re query. In the additional filters part filter on Content Type is not equal to Message (or Content Type is equal to Discussion).

image

 

2. Change the item style to use the new custom style.

image

3. Export the web part and add a line to the webpart manifest file in the CommonViewsFields section to retrieve the ItemChildCount field and then re-import the customised CQWP file:

<property name="CommonViewFields" type="string">ItemChildCount, Text</property>

5.  now use your new content query webpart on the page.

 

 

cheers,
- mutaz