Show a message when your Content Query Webpart has no resultsMay 2nd, 2013 | Posted by 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..
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:
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!
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:
3. select the whole xsl:template element. For those who don’t know anything from html or xml/xsl: it starts with
and ends with
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!).