Display comments and likes in CQWP

Display comments and likes in CQWP

This is an update from my former blog post about showing likes and comments in a news web part. And I also get questions about how I have done this, and people saying that they never managed to do the same. So even if I updated the former blog post with this solution, it may not have been clear enough how I did it.

First of all, I changed from using the CSWP (Content Search Web Part) to use the CQWP (Content Query Web Part) as I never got the Comments to work without always running a Full crawl (and we don’t want that!).

Then I create the news on a separate Blog site. The news will then be rolled up on the intranet front page in the following format:

As shown in the picture above, the news item is rolled up displaying Published Date, Title, Likes and Comments. Sometimes I also add the Category, but that part is easy and if you can add one you can add another of these fields, so I will mostly focus on how I did the Like and Comments part.

To modify the CQWP you must first add your own xsl files.
NOTE: do NOT modify the existing files – they might get overwritten in an update OR you might write something totally wrong and nothing will work on your site. You always create a new folder called “Custom” (or whatever you want), then you COPY existing files and paste them into your folder and there you can modify them without taking a risk.

Easiest is to open your site in SharePoint Designer, browse to the folder Style Library > XSL Style sheets > Create your own folder “Custom”. Then copy the file called “ItemStyle.xsl” and paste into this new folder.

Now go to the site where you first add the CQWP and then export that same web part. You export a web part by clicking the arrow on the web part and then select “Export”:

Open the file, it might be called “News.webpart” or whatever you named the web part, and find the row with the property name “ItemXslLink” and change the path to the new that was created above:

Tag: property name=”ItemXslLink” type=”string”
New value: /Style Library/XSL Style Sheets/Custom/ItemStyle.xsl

It should look like this in the webpart file now:

Then add the fields that should be displayed in the web part, they need to be added in a special way and you must find out their GUID. Find the property tag called “DataMappingViewFields” and the fields must be added in this syntax: {GUID}, Type and then separated with ;

To find the GUID of the fields that should be displayed, go to the list where the columns are and hover each column or click on it and look in the address field. The URL will contain the GUID. Be sure to remove the ASCII for % and – since they are in the URL.
Example:
I want to get the field where Comments are, (which is a Lookup Field) so I go to my news blog and open the Posts list. Go to the List settings and scroll down to find the Column field. Click on it and copy the URL and then paste that into Notepad. You will get something like this:

Then in Notepad, copy only the GUID from the URL:

%7B6026020B%2D0EB4%2D42D0%2DAA51%2D2BBA3A3CD9DF%7D

And then replace all ascii characters with the normal character like

%7B which is {
%7D which is }
%2D which is –
and you will get

{6026020B-0EB4-42D0-AA51-2BBA3A3CD9DF}

Do this for all fields that you want to display in the web part, like Comments, Likes, Category etc

Before you upload the web part file, go back to SharePoint Designer and open the custom xsl file. Add a new xsl:template in the file like this (it is an image):

The entire code in this is here, but the < tags are exchanged to [] so that the code can be displayed here on the page (just replace the [] with the regular < tags):

[?xml version=”1.0″ encoding=”utf-8″ ?]

[xsl:stylesheet
  version=”1.0″
  exclude-result-prefixes=”x d xsl msxsl cmswrt”
  xmlns:x=”http://www.w3.org/2001/XMLSchema”
  xmlns:d=”http://schemas.microsoft.com/sharepoint/dsp”
  xmlns:cmswrt=”http://schemas.microsoft.com/WebParts/v3/Publishing/runtime”
    xmlns:ddwrt=”http://schemas.microsoft.com/WebParts/v2/DataView/runtime”
  xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” xmlns:msxsl=”urn:schemas-microsoft-com:xslt”]
[xsl:param name=”ItemsHaveStreams”]
    [xsl:value-of select=”‘False'” /]
  [/xsl:param]
[xsl:variable name=”OnClickTargetAttribute” select=”string(‘javascript:this.target=”_blank”‘)” /]
[xsl:variable name=”ImageWidth” /]
[xsl:variable name=”ImageHeight” /]

[xsl:template name=”BigImage” match=”Row[@Style=’BigImage’]” 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]      
     
[div id=”Item_LargePicture” style=”padding-bottom:10px”]

        [div class=”cbs-largePictureContainer” id=”_#= containerId =#_” data-displaytemplate=”ItemLargePicture”]
            [div class=”cbs-largePictureImageContainer” id=”_#= pictureContainerId =#_”]
                [xsl:if test=”string-length($SafeImageUrl) = 0″]
[a href=”{$SafeLinkUrl}”]
[img src=”/_layouts/15/images/custom/emptynews.png” class=”cbs-pictureImgLink” /]
[/a]
[/xsl:if]
[xsl:if test=”string-length($SafeImageUrl) != 0″]
[a href=”{$SafeLinkUrl}” class=”cbs-pictureImgLink” ]
[xsl:if test=”$ItemsHaveStreams = ‘True'”]
[xsl:attribute name=”onclick”]
                          [xsl:value-of select=”@OnClickForWebRendering”/]
                        [/xsl:attribute]
[/xsl:if]
[xsl:if test=”$ItemsHaveStreams != ‘True’ and @OpenInNewWindow = ‘True'”]
[xsl:attribute name=”onclick”]
                        [xsl:value-of disable-output-escaping=”yes” select=”$OnClickTargetAttribute”/]
                        [/xsl:attribute]
[/xsl:if]
[img src=”{$SafeImageUrl}?RenditionID=5″ title=”{@ImageUrlAltText}” class=”cbs-pictureImgLink” id=””/]
[/a]
[/xsl:if]

            [/div]
                     
            [div class=”cbs-largePictureDataOverlay”  id=”_#= dataContainerOverlayId =#_”][/div]
            [div class=”cbs-largePictureDataContainer” id=”_#= dataContainerId =#_”]
             
                [a class=”cbs-largePictureLine1Link”]

[h2 class=”cbs-largePictureLine1″]

[a href=”{$SafeLinkUrl}” title=”{@LinkToolTip}” class=”cbs-largePictureLine1Link”]
[xsl:if test=”$ItemsHaveStreams = ‘True'”]
[xsl:attribute name=”onclick”]
                  [xsl:value-of select=”@OnClickForWebRendering”/]
                    [/xsl:attribute]
[/xsl:if]
[xsl:if test=”$ItemsHaveStreams != ‘True’ and @OpenInNewWindow = ‘True'”]
[xsl:attribute name=”onclick”]
                      [xsl:value-of disable-output-escaping=”yes” select=”$OnClickTargetAttribute”/]
                    [/xsl:attribute]
[/xsl:if]
[xsl:value-of select=”$DisplayTitle”/]
[/a][/h2]

[/a]
           
                [span title=”_#= $htmlEncode(line4) =#_” id=”_#= line4Id =#_” ]
                [xsl:value-of select=”ddwrt:FormatDateTime(string(@PublishedDate) ,1053 ,’dd MMMM’)”/][br /]
                [xsl:value-of select=”@PostCategory”/][/span]
                [span title=”_#= $htmlEncode(line5) =#_” id=”_#= line5Id =#_”]
                [span style=”padding-left:180px;”][xsl:if test=”string-length(@LikesCount) = 0″] 0[/xsl:if]
[xsl:value-of select=”@LikesCount” /][span style=”padding-left:3px;vertical-align:text-bottom”][img src=”/_layouts/15/images/custom/like.png” border=”0″ /][/span][/span][span style=”padding-left:10px;”][xsl:value-of select=”@NumComments” /][span style=”padding-left:3px;vertical-align:text-bottom”][img src=”/_layouts/15/images/custom/comment.png” border=”0″ /][/span]
[/span]
[/span]            

            [/div]
        [/div]
    [/div]      

[/xsl:template]
[/xsl:stylesheet]

Okey so now we have a style for the news items. But there still are some things more to do.

Enable Image Rendition and Blob Cache to make the images render in the correct size, like thumbnails.

First upload this new web part into the sites WebParts gallery (its better to do that than to just upload it straight into the page, and you can also give it a proper name and put it in its own folder so its visible in the gallery when you add a new web part)

Then go to the start page and add this new web part. You have to modify it to point out the news blog location, and to select the correct display for it. And to make other settings that you need, like how many items that should be shown, what sorting order you want, if you want to add any filters etc.

In the web part settings, also go down to the section where the fields are mapped. Then add the names of the columns that you want to display, like PublishedDate, Category, Likes etc and you will find that there are new placeholders for these as we added those in the webpart file:

Yes it is in Swedish, but if you are familiar with CQWP (which I really hope you are, since this is quite complex to setup) then you should recognize the fields I am talking about. So, type in the name of the fields you want to display and DONE. But what? No, you cannot find a field for Comments? How surprising 😉 This is where we need to go to Central Administration on the server and a new Mapping property in the Search app.

Go to Central Admin, open the Search service app, click on the link “Search schema” in the left menu and there add a “New managed property”:

Call it what you want, like “NumberOfComments”. Then set it to be Searchable, Retrievable, and then scroll down to the mapping. Add a new mapping and find the field called “ows_NumComments” and then Save this. You MUST run a full crawl for this to apply.

Okey, that was it. Simple right?? 🙂 🙂

0 0 votes
Rate this article!
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

28 Comments
newest
oldest most voted
Inline Feedbacks
View all comments
Jordy

Hi Lisa,

Very nice article and it works like a charm but i want to add a description field so it also shows the description.

Do you know how to do that ?

Anonymous

Hi ,

I tried the same steps as you listed but for some reasons i couldn't. I am trying to do it for a blog that should appear to the main site.

thanks

Divya

Lise Rasmussen

Hi Divya!

Please explain more to me, what is not working?
/Lise

papalazarou1975

Can you help me please I've followed your steps and I am getting an error when loading the webpart to the page.

THE FILE YOU IMPORTED IS NOT VALID, VERIFY THAT THE FILE IS WEB PART DESCRIPTION FILE (*,WEBPART OR *.DWP) AND IT CONTAINS WELL FORMED XML

Please help!! Thanks,

Lise Rasmussen

Hi Papalazarou975. Please make sure that the xml code is correctly written in your .webpart file. You have to replace the [] characters with <> so you get the correct syntax. And if you copy text from my site, first dump it into Notepad and then copy/paste it into your .webpart file so that you dont get any special characters or spaces in your file. Let me know! Best regards Lise