SharePoint 2013: Content Query Webpart breaks layout

In SharePoint 2013 if you create a new Content Query Web Part (CQWP) and don't edit its properties, or when the webpart returns no results, the HTML structure is broken and it may or may not mess up your design.

In my case it did. I had a few web parts in a page, and by adding a new CQWP my styling went crazy:

Margin after first webpart is gone
Margin after first webpart is gone

With a closer inspection of the code I could see that the first webpart (Content Query [5]) was wrapping all the other web parts that show up after itself.

As you can see below all the divs with id MSOZoneCell_WebPartWPQxx, that should be siblings to each other, are actually inside the first one (the CQWP).

To fix this issue you need to use a custom version of the ContentQueryMain.xsl or edit the default one (as with any change in the default SharePoint files, do this at your own risk).

In case you don't know where to find the ContentQueryMain.xsl file: at a site collection level, click the settings button in the ribbon, then “Site contents”. Go to the Style Library and then to the folder XSL Style Sheets. You’ll find the file there.

What you need to do is update the template OuterTemplate.Empty to handle the display mode of the template.

Replace the template with something like this:

<xsl:template name="OuterTemplate.Empty">
  <xsl:param name="EditMode" />
  <xsl:choose>
    <xsl:when test="$EditMode = 'True' and string-length($cbq_errortext) = 0">
      <div class="wp-content description">
        <xsl:value-of disable-output-escaping="yes" select="$cbq_viewemptytext" />
      </div>
    </xsl:when>
    <xsl:otherwise>
      <div style="display:none">No Results</div>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

And now now my styling is working again:

Now the margins are correct again
Now the margins are correct again

References

When a CQWP returns no results, zones are shifted

Dércia Silva
Posted by Dércia Silva on August 7, 2013

Related articles