Thursday, May 21, 2015

Show Multiple Columns like table in Content Query Web Part in SharePoint 2013

Showing multiple columns in content query web part was what I was working on this week in our SharePoint 2013 environment.
This post is a quick summery of what I learned through the whole work.
  1. My platform is SharePoint 2013
  2. I’m using content type syndication, so the hub is another site collection and in a second site collection I’m going to use CQWP based on a specific content type with its own site columns
  3. Step1, to know what itemstyle.xsl is and how it works. then customize it
  4. step2, to know what ContentQueryMain.xsl is and how it works, then customize it
  5. A trick to insert CQWP on your SharePoint web page
For this search for the following lines



<xsl:call-template name="OuterTemplate.CallItemTemplate">
   <xsl:with-param name="CurPosition" select="$CurPosition" />
</xsl:call-template>
And change it to:




<xsl:call-template name="OuterTemplate.CallItemTemplate">
    <xsl:with-param name="CurPosition" select="$CurPosition" />
    <xsl:with-param name="LastRow" select="$LastRow" />
</xsl:call-template>
Now that we pass the parameter we also have to change the template to accept the parameter.
Go to lines and you see the “CallItemTemplate”. Copy the second line and past it directly beneath it and make it look like this:



<xsl:template name="OuterTemplate.CallItemTemplate">
<xsl:param name="CurPosition" />
<xsl:param name="LastRow" />
Because we want to use this within our custom item template we also have give the parameter through to the template by adding a when statement just before the <xsl:otherwise> within the CallItemTemplate:






<xsl:when test="@Style='ProjectsReport'">
 <xsl:apply-templates select="." mode="itemstyle">
  <xsl:with-param name="CurPos" select="$CurPosition" />
  <xsl:with-param name="Last" select="$LastRow" />
 </xsl:apply-templates>
</xsl:when>
In this statement we specify that it only has to pass-through the parameter when the item template is ProjectsReport. So our custom template is going to be called “ProjectsReport”.

and my final custom code is like this. I tried to use SharePoint light banded rows style to have a better view. Don’t forget there’s a little customize for ContentMainQuery.xsl as mentioned above.




































































<xsl:template name="ProjectsReport" match="Row[@Style='ProjectsReport']" mode="itemstyle">
    <xsl:param name="CurPos" />
    <xsl:param name="Last" />   
<xsl:variable name="tableStart">
      <xsl:if test="$CurPos = 1">
       <![CDATA[
        <table width="100%" class="ms-rteTable-6" cellpadding="1" cellspacing="0">
         
<tr class="">
            <th class="">Name</th>
            <th class="">Client</th>
            <th class="">Status Summary</th>
            <th class="">Project Phase</th>
            <th class="">This Reporting Period</th>
            <th class="">Next Reporting Period</th>
            <th class="">Key Issues and Risks</th>
          </tr>]]>  
      </xsl:if>
    </xsl:variable>
  
    <xsl:variable name="tableEnd">
      <xsl:if test="$CurPos = $Last">
        <![CDATA[ </table> ]]>
      </xsl:if>
    </xsl:variable>
    <xsl:variable name="rowStart">
        <xsl:if test="$CurPos mod 2 = 0">
            <![CDATA[
              <tr class="ms-rteTableEvenRow-6">
            ]]>
        </xsl:if>
        <xsl:if test="$CurPos mod 2 != 0">
            <![CDATA[
              <tr class="ms-rteTableOddRow-6">
            ]]>
        </xsl:if>
    </xsl:variable>
    <xsl:variable name="rowEnd">
               <![CDATA[
              </tr>
            ]]>
    </xsl:variable>
    <xsl:value-of select="$tableStart" disable-output-escaping="yes"/>
    <xsl:value-of select="$rowStart" disable-output-escaping="yes"/>
        <td>
            <xsl:value-of select="@ProjectName" />
        </td>
        <td>
            <xsl:value-of select="@ProjectClient" />
        </td>
        <td>
            <xsl:value-of select="@StatusSummary" disable-output-escaping="yes" />
        </td>
        <td>
            <xsl:value-of select="@ProjectPhase" />
        </td>
        <td>
            <xsl:value-of select="@ThisReportingPeriod" disable-output-escaping="yes" />
        </td>
        <td>
        <xsl:value-of select="@NextReportingPeriod" disable-output-escaping="yes" />
        </td>
        <td>
            <xsl:value-of select="@KeyIssuesRisks" disable-output-escaping="yes" />
        </td>
    <xsl:value-of select="rowEnd"/>
    <xsl:value-of select="$tableEnd" disable-output-escaping="yes"/>
</xsl:template>


0 comments:

Post a Comment