Show a message when your Content Query Webpart has no results

The Content Query Webpart (CQWP) in SharePoint 2010 is a very cool, but sometimes tricky thing. It is used in about every implementation that I know because it is so awesome in letting you pick content and show it the way you want.

When a Content Query Webpart has no results though, it shows nothing by default. Perhaps it would be more user-friendly to let it actually say that it has not results..

No results found

I stumbled on a post from Johnny Harbieh that has a very good explanation and example code for SharePoint 2007, but that didn’t seem to work in my 2010 environment.

Where do we start ?

I used the post from Johnny Harbieh to get me started. He states that the CQWP uses a lot of files that you can alter. This is no different in SP2010:

– ContentQueryMain.xsl

– ItemStyle.xsl

– Header.xsl

Johnny tried the ItemStyle.xsl first (which would be my guess too) but he found out that when there are no items returned, the ItemStyle.xsl doesn’t get called.

Better choice: ContentQueryMain.xsl !

If you browse through the ContentQueryMain.xsl you can see there is a template called “OuterTemplate.Empty”.

That is the one we need!

The code

So, I changed Johnnies code, so if you can’t get my code to work, try his.

1. Open the ContentQueryMain.xsl. You can find the file in your site in “Style Library – XSL Style Sheets”. I choose SharePoint Designer to edit the file, but you can open it in the tool of your choice. Be aware that this library has check in – check out enabled.

2. search the file for this line:

<xsl:template name="OuterTemplate.Empty">

3. select the whole xsl:template element. For those who don’t know anything from html or xml/xsl: it starts with

<xsl:template name="OuterTemplate.Empty">

and ends with


4. Replace that code with:

<xsl:template name="OuterTemplate.Empty">
     <xsl:param name="EditMode" />
               Nope, nothing here to be found.
          <xsl:if test="$EditMode = 'True'">
               <div class="wp-content description">
                    <xsl:value-of disable-output-escaping="yes" select="$cbq_viewemptytext" />

5. Save, check in (and publish)

6. see the results in your CQWP.

If you want to change the message, just alter the text between the <span> tags in the code above.
It things don’t work out as you hoped, or the page shows you an error, you can always go to the version history of the ContentQueryMain.xsl file and restore the old version. (don’t forget to publish every time you alter the file!).

About: Marijn

Marijn Somers (MVP) has over 14 years experience in the SharePoint world, starting out with SP2007. Over the years the focus has grown to Office 365, with a focus on collaboration and document management. He is a business consultant at Balestra and Principal Content Provider for "Mijn 365 Coach" that offers dutch employee video training. His main work tracks are around user adoption, training and coaching and governance. He is also not afraid to dig deeper in the technicalities with PowerShell, adaptive cards or custom formatting in lists and libraries. You can listen to him on the biweekly "Office 365 Distilled" podcast.

2 thoughts on “Show a message when your Content Query Webpart has no results”

  1. How to display this custom message only for a single CQWP webpart which is empty but not for all CQWP’s which are empty.

  2. Very good question!
    The “trouble” with the suggested solution here is that it applies to all CQWP’s in your environment.

    I have no immediate answer as to how you can show this on just a single CQWP. Try using the forums (MSDN or stackexchange or other) or on twitter with the #SPHELP tag.

Leave a Reply

%d bloggers like this: