Sunday 5 January 2014

Table View from Content Query Web Part.

The Content Query web Part displays results in bullet lists by default.

There are times when we have to display results in Blocks or in a table.I too had the same requirement and after struggling for few days implemented it with following steps.


There are two xsl files which should be modified for it.
  1. ItemStyle.Xsl
  2. ContentQuerymain.xsl
There are 3 Changes in ContentQuerymain which  are as following

Step1Add this parameters where OuterTemplate.CallItemTemplate is called.
 <xsl:with-param name="LastRow" select="$LastRow" />
The final op is shown below
<xsl:call-template name="OuterTemplate.CallItemTemplate">
                    <xsl:with-param name="CurPosition" select="$CurPosition" />
                      <xsl:with-param name="LastRow" select="$LastRow" />
                </xsl:call-template>

Step2

Add this parameterto OuterTemplate.CallItemTemplate in order to receive the new parameter value. 

<xsl:param name="LastRow" />

Final op is as shown below


 <xsl:template name="OuterTemplate.CallItemTemplate">
    <xsl:param name="CurPosition" />
    <xsl:param name="LastRow" />

Step 3

Now,add the following block to the OuterTemplate.CallItemTemplate template just before the otherwise block at line position in order to call our customized template

<xsl:when test="@Style='BlockTemplate'">
  <xsl:apply-templates select="." mode="itemstyle">
    <xsl:with-param name="CurPos" select="$CurPosition" />
    <xsl:with-param name="LastRow" select="$LastRow" />
  </xsl:apply-templates>
</xsl:when>

Step4 

In ItemStyle.xsl. add this Template

<!--AddressList Template which displays data in a table-->

  <xsl:template name="BlockTemplate" match="Row[@Style='BlockTemplate']" mode="itemstyle">

    <xsl:param name="CurPos" />

    <xsl:param name="LastRow" />


    <xsl:variable name="DisplayTitle">

      <xsl:call-template name="OuterTemplate.GetTitle">

        <xsl:with-param name="Title" select="@Title"/>

        <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>

      </xsl:call-template>

    </xsl:variable>


    <xsl:if test="$CurPos = 1 ">

      <xsl:text disable-output-escaping="yes">&lt;div&gt;&lt;table&gt;</xsl:text>

    </xsl:if>

    <xsl:if test="$CurPos mod 2 = 1">

      <xsl:text disable-output-escaping="yes">&lt;tr&gt;</xsl:text>

    </xsl:if>



    <td width="50%" valign="top">

      <table width="100%" border="1" class="BlockTemplatetable">

        <tr height="27px" valign="top">

          <!--Columns from which values are fetched ,add columnnames as per your requirement-->

          <td>

            <span>

              <xsl:value-of select="@Title"/>

              <br/>

              <xsl:value-of select="@EmailAdress"/>

              <br/>

            </span>

          </td>

        </tr>

      </table>

    </td>

    <xsl:if test="$CurPos mod 2 = 0">

      <xsl:text disable-output-escaping="yes">&lt;/tr&gt;</xsl:text>

    </xsl:if>

    <xsl:if test="$CurPos = $LastRow ">

      <xsl:text disable-output-escaping="yes">&lt;/table&gt;&lt;/div&gt;</xsl:text>

    </xsl:if>

  </xsl:template>



Hope this posts save your lots of time and you are able to implement it successfully.Thanks :-)

No comments:

Post a Comment