Sunday, July 19, 2009

Using content query web part

A Content Query Web Part displays a dynamic view of content on a page in your Microsoft Office SharePoint Server 2007 site. The Web Part runs a query that can span lists and sites in your site collection, and then presents the results of that query on the page. You can, for example, use this Web Part to display the five most recently published articles in your Web site, regardless of where they are stored.
By default, the XSL style sheet that controls the presentation of the Content Query Web Part renders the Title, Description, ImageUrl, and LinkUrl columns (if present) of the items the query returns. The Web Part renders the ImageUrl and LinkUrl columns as an image and as a hyperlink, respectively. The Content Query Web Part uses XSL style sheets to render these columns by default.
If you want to query for custom content types and render specific columns from those content types in the Web Part, you need to perform some minor customization in the .webpart file.
Follow these steps in order to display custom fileds in content query web part
1. Add a Content Query Web Part to a page.
· In the Query section, set the Source to show items from the Knowledge Base Articles document library (list). Set the list type and content type accordingly.
· In the Presentation section, set the Item Style to Title and description.
2. Edit the properties in the Web Part file to display custom fields. To access the .webpart file for the Web Part, on the Web Part's edit menu, click Export.
· Office SharePoint Server 2007 generates a .webpart file with the complete set of properties that are available for this Web Part. The .webpart file is an XML file that you can edit using a text editor.
3. In the .webpart file, locate the CommonViewFields property. Use this property to specify the additional fields you want to display in the Web Part. Add the internal names of the columns and the type.
CustomField1,Text;CustomField2,Text;

You can map above field with tile, description, image url and link url or you can use above fields in your custom style.
Please note that following are types available for data type specifications. If you specify wrong type it will not break code but field will not be displayed on site.
· Text
· Note
· Number
· Currency
· Integer
· Boolean
· DateTime
· Threading
· Lookup
· Choice
· URL
· Counter
· DisplayOnly
· RichHTML
· Image
4. Map these internal column names to the columns' Title and Description present in the XSLT transformations. To do this, edit the DataColumnRenames property.
CustomField1,Title; CustomField2,Description
5. Save the .webpart file locally.
6. In the Web page, delete the Content Query Web Part you added in step 1.
7. Import the .webpart file and add the Web Part to your page. To import the .webpart file, click Page, click Add Web Parts, and then click Import. Browse to the .webpart file, and then click Upload.
8. Drag the Web Part to the appropriate zone in the page. The Web Part should display the Knowledge Base Title and the product name.
If you have added multiple fields in CommonViewFields and you need to customize XSL for this please follow steps below
· Open the site in SharePoint Designer (SPD). Navigate to Style Library, then XSL Style Sheets. Open ITEMSTYLE.XSL.
· 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.
· Scroll down to the bottom of the file. Copy the last xsl:template code block and paste it above the closing xsl:stylesheet tag.
· Change the name and match properties in the Template tag to a unique name of your choice:
· Change following

<div class="description" >
<xsl:value-of select="@Description">



With

<div class="description" >
<xsl:value-of select="@">




· Repeat above with title as well and assign custom xsl using Content Query Web Part -> Presentation-> Item Style

2 comments:

  1. HI Danish,

    Nice post ... WE needed to move some content like this from a pages directory.. .

    I had followed the steps you had mentioned but recieving an error

    "The file you imported is not valid. Verify that the file is a Web Part description file (*.webpart or *.dwp) and that it contains well-formed XML."

    Any help would be appreciated.

    Thanks a lot !
    Akshay

    ReplyDelete
  2. Please note that webpart file is an XML file. It is used for definition of your webpart properties.

    The issues that you have posted may occur if your xml is not well formed. Please try changing the extension of your webpart file to .xml and try to open it in Internet explorer to check exact.

    You need to fix issue and rename your file back. I hope this will help. Let me know if you find any further issues.

    ReplyDelete