header

SharePoint: Discussion Board Style For The Content Query Web Part

Posted by Elio Struyf on 10/05/2012.

When you want to create a rollup from all discussion board (or a specific one) in SharePoint, you will notice that the Content Query Web Part does not have an appropriate style for it.

Every out of the box style from the Content Query Web Part returns the following result.

As you can see in the image above, the content query web part returns (Blank) messages. These (Blank) messages are the replies users have made.

So why do messages return as (Blank)?

As you may or may not know, the discussion board contains two content types.

  1. Discussion;
  2. Message.

Message can only be used when you do a reply on a discussion (which creates folders), and they do not contain a Title/Subject field (this is hidden by default).

Subject (Title) Field is hidden

 Reply on a discussion

Because you do not need to add the subject in your reply, the title/subject field automatically gets (Blank) as field value.

Subject (Title) field gets blank values

Discussion Board Content Query Web Part Style

The result of the Discussion Board Style will be the following:

Discussion Board Style Result

As you can see the (Blank) messages are replaced with the discussion subject. To do this, you can retrieve the subject from the LinkUrl of the message.

Discussion subject can be retrieved from the URL

The subject of the discussion exists in the LinkUrl, because when a discussion is started, it creates a folder with the subject as folder value.

The XSL style looks like this:

<!-- Discussion Style -->
            <xsl:template name="DiscussionStyling" match="Row[@Style='DiscussionStyling']" mode="itemstyle">
            <!-- Retrieve the content type: 'Content Type' -->
            <xsl:variable name="Type" select="@Type" />
            <!-- Retrieve the discussion/message creatore: 'Created By' -->
            <xsl:variable name="Creator" select="@Creator" />
                 
            <xsl:choose>
             <!-- Check if item is a reply -->
             <xsl:when test="$Type='Message'">
              <!-- Retrieve the ReplyId, ex: '7_.000' -->
              <xsl:variable name="MessageId">
                <xsl:call-template name="StripSlash">
                  <xsl:with-param name="text" select="@LinkUrl"/>
                </xsl:call-template>
              </xsl:variable>
             
              <!-- Remove the ReplyId from the discussion URL -->
              <xsl:variable name="DiscussionUrl" select="substring-before(@LinkUrl, concat('/', $MessageId))" />
             
              <!-- Retrieve the discussion title from the URL -->
              <xsl:variable name="DiscussionTitle">
               <xsl:call-template name="StripSlash">
                  <xsl:with-param name="text" select="$DiscussionUrl"/>
                </xsl:call-template>
              </xsl:variable>
             
              <span style="font-style:italic;">  
               <a href="{$DiscussionUrl}">
                <xsl:value-of select="$DiscussionTitle" />
               </a> (Replied by: <xsl:value-of select="$Creator" />)
              </span>
             </xsl:when>
             <xsl:otherwise>
              <a href="{@LinkUrl}">
               <xsl:value-of select="@Title" />
              </a> (Posted by: <xsl:value-of select="$Creator" />)
             </xsl:otherwise>
            </xsl:choose>
            </xsl:template>
            <xsl:template name="StripSlash">
            <xsl:param name="text"/>
            <xsl:choose>
              <xsl:when test="contains($text, '/')">
                <xsl:call-template name="StripSlash">
                  <xsl:with-param name="text" select="substring-after($text, '/')"/>
                </xsl:call-template>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="$text"/>
              </xsl:otherwise>
            </xsl:choose>
            </xsl:template>

For the messages the ID (12_.000) will be removed from the LinkUrl, otherwise you will end up on a page that does not exist.

The XSL style can be added to the ItemStyle.xsl, which can be found at the following location: http://your_sitecollection_url/Style Library/XSL Style Sheets/ItemStyle.xsl.

When you add the content query to you page, change the Styles and Fields settings to match as in the following image.

Content Query Web Part - Settings

When you save these settings, the result should be like this:

Discussion Board Style Result

gab

Posted on 10/05/2012

Hi Elio.
You blog is very interessing but xsl code is missing . :(
Could you please share it . Pllllss

Many thanks ii am quiet stuck

Elio Struyf

Posted on 10/05/2012

gab,

thank you for letting me know this.

It looks like we had a problem in the system and the code was removed. I re-added the code snippet to the post.

Regards,
Elio

Jussi Palo

Posted on 27/08/2012

It doesn't really work if you have special characters in the title.

Elio Struyf

Posted on 27/08/2012

Jussi,

What is the problem you are experiencing? Can you explain this a little bit, so that I'm able to reproduce it.

Thank you,
Regards,
Elio Struyf

Jussi Palo

Posted on 25/09/2012

If you have special chatacters in the URL, they will get removed, e.g., åäö, making URL not the same as title.

Elio Struyf

Posted on 26/09/2012

Jussi,

I'm sorry but I cannot reproduce the problem. Is the URL the same as the one in the discussion list? Does it work in the discussion list?

What you could try to do is encode the URL via Javascript.

Regards,
Elio Struyf

Leave your comment

Name
E-mail
Comment
Captcha