How to display a message when no results are returned by the Content by Query Web Part (CBQWP)

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?
 
The CBQWP has several XSLT files that it works with – Content Query Main, Item Style, and Header. See reference here http://msdn.microsoft.com/en-us/library/bb447557.aspx
 
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!
Advertisements

About jharbieh

I'm an IW Solutions Architect with background in requirements gathering, planning, design, architecture, and development (not necessarily in the right order). Currently, my focus is on the Microsoft Cloud, Productivity and Collaboration space. Hope you enjoy what I write about here. Thanks for visiting. Johnny Harbieh
This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to How to display a message when no results are returned by the Content by Query Web Part (CBQWP)

  1. naresh says:

    Thanks for the Nice Post.. very helpfull..Is there any way to get the “Webpart Title” using the XSLT in Content Query Webpart.I can get the PageUrl and Webpart Id using parameters but could not get any of the webpart properties, where I am specifically looking to get the “Web Part Title URL” and “Web part Title”as i have Links in the webpart Body that will link based on values in these two properties. please, please let me know if you have anywork around to get the webpart Title URL and WEbpart Title using XSl from any of the xsl files out there in sharepoint[contentquerymain.xsl, Itemstyle.xsl, Header.xsl]..Thank you.

  2. Johnny says:

    Hi Naresh, Thanks for stopping by and for your kind remarks. Sorry I am currently at the SPC 2009 in Las Vegas and didn’t have a chance to respond to you earlier. By now, you’re probably all set. I hope so. I personally never had a chance or tried to do what you’re looking for. I know that these XSLT files are meant to display properties of items not their container. But lets think of some options here:a. I wonder if there is a way though to grab the value from Client Object Model rendered as JavaScript on the page, orb. Insert a placeholder for the title in the XSLT, then replace it using a JavaScript call placed in a Content Editor Web Part at the bottom of the pageKeep me posted. Take it easy,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s