tisdag 21 juni 2011

Different CQWP rendering behavior when upgrading to SP2010

In the past I started to love and hate the Content Query Web Part since you can use it with audience targeting. In the MOSS days you had a very good control of what was happening and what to render. A rather annoying behavior though was that - even in rather normal situations - the query didn't give you the result you wanted.

My customer wanted to render an audience filtered calendar that showed the upcoming 7 events from [Now] and beyond, not [Today] and beyond. Since there is no such thing as <Now/> that you can use in a caml query, I used <Today/>, increased the row limit and in the item style (ItemStyle.xsl) filtered out the items not matching the rules. But sometimes I still didn't get the 7 events I wanted. I found out that the audiene filtering is not included in the query, but is made on the result of the query. So if I requested 20 items, the audience filtering maybe took away 15 items and the rendering rules in the xsl maybe took away 2. So instead of the expected 7 items I got 3.

The only simple solution to solve this was to increase the row limit even more and it worked fine.


But now the behaviour of the rendering is quite different. In theold days the result was rendered as a <table> but now it's a <ul><li> thing. Better HTML, but it has issues. When you render a table row with only empty cells it doesn't show anything. but an empty <li> in a <ul> renders an empty row. So when I filter out an item in the item style it still renders an empty row since the <li> tag is rendered in ContentQueryMain.xsl.
My solution to this was to set the style attribute in the <li> to display:none;. And how to do that? First the <li> tag must be rendered as a tag, not as a text string that is the case in the default ContentQueryMain.xsl (the $BeginListItem variable). In the OuterTemplate.CallItemTemplate I rendered an <li class="dfwp-item"> tag instead of <xsl:value-of disable-output-escaping="yes" select="$BeginListItem" /> so that the last rendered tag is a fully qualified xml-tag. In my item style, if the item doesn't match the rules, I set the attribute of thea fully qualified xml-tag. In my item style, if the item doesn't match the rules, I set the style attribute of the last rendered xml-tag (i.e. the <li>-tag) with <xsl:attribute name="style">display:none;</xsl:attribute>.

Works beautifully!

2 kommentarer:

  1. Joakim,

    you have the solution to a problem I also have. I just dont get the solution completely.

    I changed the ContentQueryMain.xsl to this

    -xsl:template name="OuterTemplate.CallItemTemplate"-
    -xsl:param name="CurPosition" /-
    li class="dfwp-item"-
    -!-- -xsl:value-of disable-output-escaping="yes" select="$BeginListItem" /- ---

    Then I go to the rendering of my list item to add the xsl:attribute, but I'm not sure where. I also dont understand how an xsl:attribute would control the styling and get rid of the unwanted li's in my CQWP rendering.

    Here is my list item XSL (just the last part)

    -xsl:variable name="Description"-
    -xsl:call-template name="removeMarkup"-
    -xsl:with-param name="string" select="@Body"/-
    -/xsl:call-template-
    -/xsl:variable-

    -xsl:attribute name="style"-display:none;-/xsl:attribute-.

    -tr-
    -td valign="top"-
    -strong-
    -xsl:value-of select="$DisplayTitle"/-
    -/strong-
    -br /-
    -xsl:value-of select="substring($Description,1,60)" /-...
    -br /-
    -br /-
    -/td-
    -td valign="top" class="meer" style="font-weight:normal;font-size:10px;"-
    -a href="{$SafeLinkUrl}"-
    read more
    -/a-
    -/td-
    -/tr-
    -/xsl:template-

    I hope you can point me in the right direction. I will try to understand properly and report back here when I do.

    SvaraRadera
  2. -- I messes up the XSLT so I just copy/pasted the XSLT from my live SP2007 to SP2010 and the bullets that were being rendered because of LI that appeared with a class dfwp-item have disappeared.

    Maybe this is a working and better solution - I compared XSLT files before, but they are identical .. So now I just checked out, copy/paste and checked in the XSLT through SP Designer and it worked .. hmm.. now I would lke to know why .. maybe V4 styling somewhere still ...

    SvaraRadera