Sunday, August 11, 2013

Display multiple columns in Content Query Web Part in MOSS 2007

            We can use the Content Query Web Part to create custom, dynamic views of data that is queried from many sources, and present that data all in one place. You can display custom fields with the Content Query Web Part by setting custom properties and editing the default XSLT transformation. This document demonstrates how to display custom fields in the Content Query Web Part by setting the CommonViewFields property and editing the default XSLT transformation, which is used by the Content Query Web Part to format the rendering of items.

Steps:


The following sections show how you can customize the Content Query Web Part to display custom fields. The process includes seven steps:

  1. Adding the Content Query Web Part to a Web page on an Office SharePoint Server 2007 site.
  2. Configuring the Content Query Web Part to work with the Project Status Reports document library and the Project Status Report content type.
  3. Exporting the Content Query Web Part to a .webpart file.
  4. Editing the exported .webpart file and setting the CommonViewFields custom property of the Content Query Web Part to request the Project Status Report content type custom fields.
  5. Editing the default XSLT transformation, this is used by the Content Query Web Part to display the Project Status Report content type custom fields.
  6. Importing the edited .webpart file to a Web page on an Office SharePoint Server 2007 site.
  7. Configuring the imported Web Part to use the new XSL style.

Adding a Content Query Web Part to a Web Page


Note:
The Content Query Web Part is available in Office SharePoint Server 2007. It is not available with Windows SharePoint Services 3.0.

To add an instance of the Content Query Web Part to a Web page


1.       On a Web page in a SharePoint site, click Site Actions, and then click Edit Page. The page is placed in edit mode.

2.       In a Web Part zone, click Add a Web Part.
 
Figure 1. Add a Web Part to a Web Part zone
 
 

3.       In the Add Web Parts dialog box, select the Content Query Web Part check box. Click Add to add the Web Part to the selected Web Part zone.
 
Figure 2. Add Web Parts dialog box
 
 
 

Configuring the Content Query Web Part


This procedure shows how to configure the Content Query Web Part.

To modify the properties of the Content Query Web Part to display content from the Project Status Reports document library


1.       On the Content Query Web Part, click edit, and then click Modify Shared Web Part.
   
 Modify shared Web Part
 

The Content Query Tool Part shows Web Part property categories.

 Content Query Tool Part


2.       Expand the Query category. In the Source section, click Show items from the following list.

Set source property
 


3.      Click Browse. In the Select List or Library dialog box, select the List or document library, and then click OK.

4.       Do the following:

    1. In the List Type section, in the Show items from this list type box, click Document Library.
    2. In the Content Type section, in the Show items of this content type group box, select the specific content type if any or All content types.
Set list type and content type properties
 
In the Additional Filters section, we can give the filter conditions to restrict the results 
  
 
Set filters

















 

5.       Expand the Presentation section. In the Group items by box, Select the column name, if any groupings required.
 
Set grouping property
 


 















6.       Click OK in the tool part to save the changes to the Web Part properties.

Now we will be able to see some list Items in the Content Query WebPart. By Default, The Content Query Web Part will title of the result Items.

To display multiple fields from the Query, follow the below steps.

Export the Content Query Web Part


The Content Query Web Part exposes many properties that you can customize but not all of them are available through the Content Query Tool Part. To modify the custom properties of an existing instance of the Content Query Web Part, export the Web Part to a .webpart file, and then modify the .webpart file.

To export an existing instance of the Content Query Web Part


1.       In the Web Part, click edit, and then click Export.
 
Figure 12. Export Web Part



2.       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.

Edit the Exported .Webpart File


The exported .webpart file is a text file containing XML that describes the properties of the Web Part. You can edit it in any text editor or by using an XML editor such as Microsoft Office SharePoint Designer 2007 or Microsoft Visual Studio. When you edit the custom properties in the .webpart file, remember the following:

  •          You must specify any column names you use by their internal names. The internal name of a column might not be the same as the name that is displayed in the Office SharePoint Server user interface. For example, if a column name contains spaces, each space is encoded as _x0020_ in the internal name. You can see the internal name of a column by viewing the query string parameter in a Site Column page, or by viewing the Content Query Web Part code after filtering on the column. To view the column name in the Web Part code, set the Web Part to filter on the column, export the Web Part, and then view the name of the column as it is referenced in the filter properties.
  •      ALL XML markup that is inside a <property/> tag must be enclosed in a <![CDATA[ ]]> block.       
  • Do not include extraneous spaces between tags in the XML markup.

Steps to Find the Internal Column :


1.       In the List, open List Settings, then select specific column.
2.        Locate the internal column name one of two ways:

a.       Way One: Click the column name.  If you go to the end of URL , the internal column name appears after Field=.  

 
 

b.      Way Two: Right click the link and select Properties

 
 
 
The URL listed in the General tab has the info we need, just hidden below the viewable area.  Select the URL with your cursor, hit Ctrl + A to select the full URL, the copy and paste the URL into Notepad.

 
 
In the pasted URL in Notepad, locate Field=.  The name that follows in the internal column name.
 
  
 
 

You can display custom fields with the Content Query Web Part. This document shows the steps for modifying the properties in the Content Query Web Part .webpart file so that the Web Part displays multiple columns.

To modify the properties in the Content Query Web Part .webpart file to display multiple columns


1.       Set the value of the CommonViewFields property. The Content Query Web Part does not return all fields for all items automatically. You can use the CommonViewFields property to request specific fields to render. Locate the CommonViewFields property in the .webpart file, and then edit it to look like the following.

<property name="CommonViewFields" type="string">

InternalColumnName, FieldType ; InternalColumnName, FieldType;

</property>

InternalColumnName = Column which we find in the previous step and the FieldType = Type of the respective column. Each internal column name is specified, followed by its data type. The name/type pairs are separated by semicolons.

2.        It is very important that the field type listed is the correct type for the the data. If the field type is wrong, it won't break the site, but the Item Style won't display the content. Available values are:

    1. Text
    2. Note
    3. Number
    4. Currency
    5. Integer
    6. Boolean
    7. DateTime
    8. Threading
    9. Lookup
    10. Choice
    11. URL
    12. Counter
    13. DisplayOnly (DisplayOnly field type has no storage of its own)
       
       
       
       

3.       Save the file and import it back into the site. (Add a Web Part - Import - select the .WEBPART file - Upload - Drag and drop the web part onto the page).

Now we have two CQWPs on the page.  We can remove the original one.

Editing the Default XSLT Transformation


You can specify Content Query Web Part group and item styles in the Styles section of the Presentation category in the Content Query Tool Part.

 Styles properties
 






The Content Query Web Part uses the ItemStyle XSLT file that is located in the site collection's Style Library to render items. By editing the ItemStyle XSLT file, you can add a custom item style to enable the Content Query Web Part to render the Project Status Report content type custom fields.

To edit the ItemStyle XSLT file by using Office SharePoint Designer 2007


1.       Start Office SharePoint Designer 2007.

2.       On the File menu, click Open Site.

3.       In the Open Site dialog box, type the URL of the Office SharePoint Server site, and then click Open.

4.      On the View menu of the SharePoint site, click Folder List, expand Style Library, and then expand XSL Style Sheets.
 
XSL Style Sheets node

 
 

5.       Locate and double-click the ItemStyle.xsl file to open it. SharePoint Designer prompts that the file is under source control and asks if it should check out the file. Click Yes.

6.        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 new 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:
     
    <xsl:template name="CustomStyle" match="Row[@Style='CustomStyle']" mode="itemstyle">
     
     
  3. For every internal column name that has been included in the web part and needs to display in the Item Style, add it to the template using the xsl:value tag:
     
    <xsl:value-of select="@InternalColumnNameGoesHere" />
     
    For example: <xsl:value-of select="@Comments" />
  1. Wrap HTML around the tag to format the data. 
    Please note!!
     
    When the page renders the CQWP, the site grabs the Item Style for each line of data.  So we can't add a table and expect that each line of data will be in a row.  Instead we get a full table for each line of data.  Keep this in mind as you style the HTML around the data.
     
  2. Save the file, go to the site and refresh the page.  The new styled content will appear. 
 

Importing the Edited .Webpart File


This procedure shows how to import the customized Content Query Web Part.

To import the customized Content Query Web Part to a page on the SharePoint site


1.       Click Site Actions, and then click Edit Page.

2.       On the page editing toolbar, on the Page menu, point to Add Web Parts, and then click Import.
 
Import Web Part
 
 
 
3.       In the Add Web Parts pane, click Browse. In the Choose File dialog box, navigate to the folder that contains the .webpart file you modified earlier, click the file, and then click Open.
 
Add Web Parts
4.       In the Add Web Parts pane, click Upload.

5.       After the Web Part is uploaded, in the Add Web Parts pane, select the Web Part zone to add the Web Part to, and then click Import. The customized Content Query Web Part is added to the selected Web Part zone.

Configuring the Imported Web Part to Use the New XSL Style


At this point, the Web Part looks like it did before: It displays the documents currently in the Project Status Reports document library that have a status of Requires Attention or a status of Might Need Attention, and groups the items by their status.

To make the Web Part use the new XSLT transformation


1.       If the Web page is not already in edit mode, click Site Actions, and then click Edit Page.

2.       On the Content Query Web Part, click edit, and then click Modify Shared Web Part.

3.      In the Content Query Tool Part, expand the Presentation category. In the Styles section, in the Item style box, click CustomStyle.
 
Select custom item style

 
4.       In the Web Part pane, click OK to apply the change.

The Content Query Web Part uses the custom XSLT transformation to render the multiple fields.  Example: ouput

 
 
Related Posts Plugin for WordPress, Blogger...