Monday, 15 July 2013

Displaying Custom Fields in Content Query Web Part

The Content Query Web Part (CQWP) allows you to display data from a list anywhere within a Site Collection..


However, by default the CQWP only displays the freaking ‘Title’ field, if you want to display all the other field in the list you have to do all the below modifications,
                                              

In the Tool Pane, expand Presentation and experiment with the drop down options under Styles. Select Apply to see the changes. These are default styles available in MOSS 2007.
All these styles will be available in a single file named “ITEMSTYLE.XSL”. This file is available under Style Library/XSL Style Sheets/.You can use SharePoint designer to customize this file.

Take a minute to look at the file.  Every time you see "<xsl:template..." that is the start of a new Item Style.  There is one for each of the out-of-the-box Item Styles that show up in the Styles drop down in the Web Part Tool Pane.  For every custom Item Style that is needed, a newxsl:template> </xsl:template> block will have to be created.

1)    Scroll down to the bottom of the file. Copy the last xsl:template code block and paste it above the closing xsl:stylesheet tag.

2)    Change the name and match properties in the Template tag to a unique name of your choice:
            Format:
       <xsl:template name="NewsItemStyle" match="Row[@Style='NewsItemStyle']"        mode="itemstyle">
3)  Before we style any content, we need to have the web part pull in the content we want to display. Some content is already pulled in by default, such as Title.  For anything else that is not pulled by default, we need to tell the web part to get those fields. Steps to do that
A.       In the Content Query Web Part you have inserted on the page, click edit, and then click Export.
B.    In the File Download dialog box, click Save. In the Save As dialog box, type a name and location for the .webpart file. Click Save in the Save As dialog box to save the file.
C.    Open the saved file and search for “CommonViewFields” text, you can able to see tags like this,
                 <property name="CommonViewFields" type="string"></property>
D.  Add the list column name and along with column type you need in the new style.

 
And the Field type can be notified below,

E.  After adding the necessary fields inside the tag, save the file.
F.  Finally import this file as web part in moss2007.(Delete the previously added content query web part)

4)      Now customize the ITEMSTYLE.XSL file with necessary UI design format,
Example:
<div class="description">
    <xsl:value-of select="@Rollup" />
    <xsl:value-of select="@Body" />

</div>

Note:
Rollup and Body are the fields we added in the .WEBPART file.

5)      Complete code for NewsItemStyle has been written here.

<xsl:template name="NewsItemStyle" match="Row[@Style='NewsItemStyle']" mode="itemstyle">
  <xsl:variable name="SafeLinkUrl">
            <xsl:call-template name="OuterTemplate.GetSafeLink">
                 <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
            </xsl:call-template>
       </xsl:variable>
       <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:variable name="RowNum" select="count(./preceding-sibling::*)" />
       <table>
  <tr>
  <td style="vertical-align:top;">
     <table>
  <!-- <xsl:if test="$RowNum &lt; 4"> //to restrict the number of rows-->
  <tr>
  <td valign="top">
  <a href="{$SafeLinkUrl}"><img border="0" height="50" width="80">
<xsl:attribute name="src"><xsl:value-of select="substring-before(substring(@Rollup, string-length(substring-before(@Rollup, 

'/St'))+1,string-length(@Rollup)),',')" />
  </xsl:attribute>
  </img>
  </a>
  </td>
  <td style="vertical-align:top;">
  <table>
  <tr>
  <td style="color:#3A80B8; font-family:'Century Gothic'; font-weight:bold; font-size:12px" valign="top">
  <xsl:call-template name="OuterTemplate.CallPresenceStatusIconTemplate"/>
                <a class="NewsTitle" href="{$SafeLinkUrl}" title="{@LinkToolTip}">
                    <xsl:value-of select="$DisplayTitle"/> 
                </a>
  <br />
  <span style="color:#595959; font-family:Verdana; font-weight:bold; font-size:10px">
              <xsl:value-of select="ddwrt:FormatDate(@Created, 2057, 3)"/>
</span>  
</td>
  </tr>
  </table>
</td>
</tr>
  <!--</xsl:if>-->
     </table>
  </td>
  </tr>
       </table>       
 </xsl:template>


6)      Please “check in” the file and Now you can able to view you Custom style in Tool pane and under Presentation heading,

7)      Select all the necessary properties in Content query web part and publish the page.


No comments:

Post a Comment