Cloud

Customizing a Content Query Web Part – Part I

A simple way to avoid using CAML queries to sort content query web part data is to add the column you want to sort on to the web part sort and group drop downs, this is done by adding the following property to your .webpart file:

<property name="AdditionalGroupAndSortFields" type="string">{field name}</property>

Field name is not as straight forward as you would think. The items contained in this property cannot have spaces in it, so what do I do when my column name has a space? Well, you need to replace the space with the following: _x0020_. This means that if I have a column that I created called Start Date, it will look like this:

<property name="AdditionalGroupAndSortFields" type="string">Start_x0020_Date</property>

If you want to call the column something else within code you would have to add the following property to rename it:

<property name="DataColumnRenames" type="string">Start_x0020_Date, StartDate</property>

Here is how the web part should look if we wanted to add a custom column called Publish Date, we want the data to be displayed by the web part, including the ability to sort and group on that column, and we want to refer to the column PublishDate in the ItemStyle.xsl file:

<webParts>

<webPart xmlns="http://schemas.microsoft.com/WebPart/v3">

<metaData>

<type name="Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart, Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />

<importErrorMessage>Cannot import this Web Part.</importErrorMessage>

</metaData>

<data>

<properties>

<property name="Title" type="string">Industry News</property>

<property name="Description" type="string">Content Query Web Part used to display industry news items.</property>

<property name="ChromeType" type="chrometype">Default</property>

<property name="ChromeState" type="chromestate">Normal</property>

<property name="ItemLimit" type="int">10</property>

<property name="SortBy" type="string" />

<property name="SortByDirection" type="Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart+SortDirection, Microsoft.SharePoint.Publishing, Version=12.0.0.0,Culture=neutral, PublicKeyToken=71e9bce111e9429c">Desc</property>

<property name="GroupStyle" type="string">DefaultHeader</property>

<property name="ItemStyle" type="string">IndustryNews</property>

<!–

These are the extra fields that will get pulled back for use in the XSL.

–>

<property name="CommonViewFields" type="string">PublishingPageContent, RichHTML;ArticleStartDate, DateTime;URL, URL;Publish_x0020_Date, DateTime</property>

<property name="DataColumnRenames" type="string">Publish_x0020_Date,PublishDate;</property>

<property name="AdditionalGroupAndSortFields" type="string">Publish_x0020_Date</property>

</properties>

</data>

</webPart>

</webParts>

The above web part assumes there will be custom code added to the ItemStyle.xsl. This custom code will be covered in a subsequent posting.

When you make any changes to an existing web part or a custom web part, the changes will not appear right away. You will need to upload your changes to the web part gallery and re-add the changed web part to each page that it exists on. Once that is done and the web part is configured, your changes to the web part will be noticeable.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to the Weekly Blog Digest:

Sign Up
Follow Us
TwitterLinkedinFacebookYoutubeInstagram