2 minute read
I got a question via email today, the answer to which seemed generally useful:
While you’re not as cool as Old Spice guy (sorry, nobody is that cool) you are the uber CQWP guy. Enough ego stroking this morning? Anyways, quick question. Any idea how to display a custom message in the CQWP when you have no items matching the filter? You can see a message in design mode but I want to set a custom message in runtime mode for users. Thought you might know off the top of your head. Thanks!
Come on, with a question worded like that, I had to post it, didn’t I?
In a DVWP, when you set the “no matching text” message in the Common Data View Tasks Common Dialog, like this
the message ends up in an XSL template called
dvt_1.empty. Wouldn’t it be grand if it were something so consistent in a CQWP? I’m guessing it isn’t, but here’s the logic from a DVWP.
dvt_1 template, after the nodeset is assigned to the variable
$Rows, there’s a variable called
$dvt_RowCount created, which contains the number of items in
$Rows. Next there’s a variable called
$dvt_IsEmpty, which is set to true if the value of
$dvt_RowCount=0 (this part is a little needlessly verbose). If
$dvt_IsEmpty is true, then we call the template
dvt_1.empty, otherwise we do the normal
yadda, yadda, yadda.
<xsl:template name="dvt_1"> <xsl:variable name="dvt_StyleName">Table</xsl:variable> <xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row" /> <xsl:variable name="dvt_RowCount" select="count($Rows)" /> <xsl:variable name="dvt_IsEmpty" select="$dvt_RowCount = 0" /> <xsl:choose> <xsl:when test="$dvt_IsEmpty"> <xsl:call-template name="dvt_1.empty" /> </xsl:when> <xsl:otherwise> yadda, yadda, yadda </xsl:otherwise> </xsl:choose> </xsl:template>
dvt_1.empty template simply outputs the “no items here” message, like this (again, overly verbose):
<xsl:template name="dvt_1.empty"> <xsl:variable name="dvt_ViewEmptyText">There are no items to show in this view.</xsl:variable> <table border="0" width="100%"> <tr> <td class="ms-vb"> <xsl:value-of select="$dvt_ViewEmptyText" /> </td> </tr> </table> </xsl:template>
There are no items to show in this view. message is replaced by whatever you specify. You can adapt these XSL snippets in SharePoint Designer to bend them to your will fairly easily.