Hi all,
Hope you are having a good break over this holiday season.
Say you have a Content By Query Web Part (CBQWP) that is configured to display new Pages in the pages library. And you defined "new" by calculating Today minus 7 days. Now it’s the New Year and several weeks have passed without any new Pages being added to that library. People on vacation… You’re half asleep and you need to quickly wake up before Microsoft goes live with Office 14 🙂 I don’t know, there could be a million reasons why this incredibly new SharePoint site is getting old and fresh content seems to be scarce. So you’re up now and you started to notice that your CBQWP has no items displayed. No rows or results are being returned by its query.
What are we going to do? It looks like we need some sort of a message that gets displayed to our site visitors when no results are returned by the query. So I and my colleague went out to look for a solution. We thought that customizing the XSLT will do the trick. Which XSLT again?
So here’s how I approached this:
1. I started looking into the Item style to see if I can get something going in there. I tried a couple of quick tricks and found out that no way I can display a message in there when no items are returned. I added some IF/ELSE statements. In XSLT, this statement is written using the XSLT Choose element. See reference here –
http://www.w3schools.com/xsl/xsl_choose.asp I found out that this file really don’t matter if there are no items in the results coming back. The template is not getting called. That triggered something in my brain 🙂 I started to wake up 🙂
2. So I moved up to the Header file. I quickly noticed that again, no way we can display a message here.
3. And finally, the gold mine, the Content Query Main file is the way to go. I quickly noticed that this file (See first reference) is responsible for dealing with the templates and which ones get to be called, and especially when there are no items. See snippet below.
<xsl:when test="$IsEmpty">
<xsl:call-template name="OuterTemplate.Empty" >
<xsl:with-param name="EditMode" select="$cbq_iseditmode" />
</xsl:call-template>
</xsl:when>
So I looked down the file and searched for the Outer Template of Empty and found it. I added some code and Voila! Here’s the snippet.
<xsl:template name="OuterTemplate.Empty">
<xsl:param name="EditMode" />
<td>
<!– MESSAGE STARTS HERE –>
<div id="linkitem" class="item link-item">
No Items are currently available. Please check again soon.
</div>
<!– MESSAGE ENDS HERE –>
<xsl:if test="$EditMode = ‘True’">
<div class="wp-content description">
<xsl:value-of disable-output-escaping="yes" select="$cbq_viewemptytext" />
</div>
</xsl:if>
</td>
</xsl:template>
That’s pretty much it. I guess this is one way to display a message to your visitors when the CBQWP returns no rows or items.
Hope this helps you like it helped me 🙂 (Yeah, yeah, I know, I know,… I usually look forward to springing back to 1999 -2000 when XSLT was so cool)
Have a good one!
Leave a comment