Show Multiple Columns in Content Query Web Part

Posted: May 23, 2014 in SharePoint

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

An Important note:

Unlike SharePoint 2007, there’s no need to export your web part and modify it when your SharePoint is 2010 or 2013. You just need to custmize itemstyle.xsl and in some cases (like showing as a table) the ContentQueryMain.xsl

Cheers to Waldek Mastykarz based on this resource.

 

to customize itemstyle.xsl, better to first try it simply and add your fields and check if it works and then customize it to view fields as you want. So create a template:


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

 

To add fields that should be set in the webpart:


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

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

<xsl:value-of select="@StatusSummary" disable-output-escaping="yes" />
...

Note: I found that you should not use space in your template name or any of xsl select values. In my case my column names was same as these xsl fields.

In this point you should be able to add content query webpart to a simple wiki page and set the required fileds.

CQWP after customizing itemstyle.xsl

CQWP after customizing itemstyle.xsl

You are almost done. In this case you just need to set the style of viewing your data in itemstyle as you want.

But if you need to show them as a table there a good article here about it. I won’t copy it here so have a look there.

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>

 

1 more trick!

I found that in case of using site content types and site columns (maybe content type syndication) and then querying from multiple sub-sites in the subscriber site collection, at first I should connect the CQWP to one the lists directly in any of sub-sites using third option as the “Source”. Then when it connected succefully and “fields to display” were set correctly, I changed the source to the whole site collection. Setting the whole site collection at first was not working for me and the webpart was giving me an error and fields couldn’t be set.

CQWP2

And here is what I acheived finally:

Querying multiple sub-sites using content query web part

Querying multiple sub-sites using content query web part

 

Some other useful blogs that I reviewed are also this and this.

Hope this could be helpful.

Navid

Advertisements
Comments
  1. hasbulhadi says:

    hi

    how do you display the custom cqwp/

  2. hasbulhadi says:

    how do you able to add a custom field.. i try to change the itemstyle.xsl .. it wont changed

  3. Jesse Loesch says:

    Hello,

    I’ve been able to implement your solution just fine when it comes to selecting the specific list. However when I change it to entire site collection I no longer get all of my columns to populate. I have TaxonomyFields which are displaying fine but the People Picker fields are what seems to disappear when I change it to site collection. I’m wondering if you would have any advice.

    Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s