Show a message when your Content Query Webpart has no results

May 2nd, 2013 | Posted by Marijn in code | customization | design | enduser | information | sharepoint 2010 | sharepoint designer | technical | usability | webpart | xsl

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


</xsl:template>

4. Replace that code with:

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

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!).

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

2 Responses

  • Madhuri says:

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

  • Marijn says:

    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: