Second Life of a Hungarian SharePoint Geek

January 20, 2010

Displaying a customizable message when Content Query Web Part returns no result

Filed under: CQWP, SharePoint — Tags: , — Peter Holpar @ 22:23

The default behavior of the CQWP is to show nothing in the display mode when there is no item matching the query. Sometimes it is adequate, but sometimes it would be better to show at least some kind of message to the visitors.

You can alter the OuterTemplate.Empty XSLT template in the ContentQueryMain.xsl file, as described in this post of Johnny Harbieh’s blog. It’s relatively easy and straightforward, requires no custom code and deployment of assemblies, can be done using a simple SharePoint Designer 2007.

This method has a drawback that it displays a fixed message for the visitors, no matter what item or grouping style the web part is configured to use. That could have been improved by adding XSLT conditions (using if or choose/when/otherwise) on the @Style or @GroupStyle attributes on the item, but it still does not allow the message to be set by users on the web part level.

If you really need this kind of flexibility, you should derive your own custom web part that is inherited from the CQWP, as show below.

  1. using System;
  2. using System.Text;
  3. using Microsoft.SharePoint.Publishing.WebControls;
  4. using System.Web.UI.WebControls.WebParts;
  5. using System.Web.UI;
  6. using System.Data;
  8. public class ContentByQueryWebPartEx : ContentByQueryWebPart
  9. {
  10.     private String _emptyHtml = null;
  12.     [Personalizable(), WebBrowsable(true), WebDisplayName("EmptyHtml"), WebDescription("EmptyHtml")]
  13.     public string EmptyHtml
  14.     {
  15.         get
  16.         {
  17.             return _emptyHtml;
  18.         }
  19.         set
  20.         {
  21.             _emptyHtml = value;
  22.         }
  23.     }
  25.     protected override void Render(HtmlTextWriter writer)
  26.     {
  27.         if (((Data == null) || (Data.Rows == null) || (Data.Rows.Count == 0)) && (!String.IsNullOrEmpty(EmptyHtml)))
  28.         {
  30.             // No matching items, render the configured text
  31.             writer.Write(EmptyHtml);
  32.         }
  33.         else
  34.         {
  35.             // Use the default Render method
  36.             base.Render(writer);
  37.         }
  38.     }
  39. }

After deploying this web part, you can use it as a replacement for the CQWP, and configure the HTML that should be displayed when there is no result for the query.



  1. u rock brother… thanks

    Comment by Raghu — February 2, 2010 @ 14:30

  2. Very good option. Thanks for the credit and thanks for sharing.

    Comment by Johnny Harbieh — March 31, 2010 @ 03:57

RSS feed for comments on this post. TrackBack URI

Leave a Reply

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

You are commenting using your 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

Blog at

%d bloggers like this: