Building a Calendar Content Query Web Part (CQWP) for SharePoint


I am happy to release Calendar CQWP for MOSS for use when trying to roll-up calendars or re-use calendars within a site collection. In essence, I created a view for the default content query web part that is relevant for calendars – it presents events by start date, title, and description. The content query web part (CQWP) is central to SharePoint’s content re-use story.

In fact, when building modern digital marketing web sites on SharePoint, like HedKandi.com, content query web parts let authors manage content centrally while re-using content in multiple locations. Unfortunately, the out of the box styles aren’t useful outside of the document management use case – so light customization is often required. While customizing content query web parts is more difficult than setting up content types – it’s within reach even if you have no coding or branding experience.

Working with content query web parts has three distinct parts: building the query, communicating query results, and presenting the results. The content query web part is made up of an XML .webpart file that handles building the query and communicating the results. Presenting the results is handled by a style sheet (the default style sheet can be found at [site collection root]/Style%20Library/XSL%20Style%20Sheets/ItemStyle.xsl).

While the governing MSDN content is great for details, I thought I’d walk through the basic approach I used for the Calendar CQWP. It is important to note that content query web part functionality is dependent on the Site Collection feature “SharePoint Server Publishing Infrastructure”.

Building the Query

Building a query  is configured through the browser by “Modifying Shared Web Part” properties for the content query web part. When site columns are used in multiple content types or exist in multiple categories, managing filters and grouping and sorting settings can get challenging. In addition, complexity increases if a site column is renamed. An easy way to identify how a field is referenced is to edit the respective column in the document library and look at the URL for the value of “Field”. When all else fails, create your own custom columns and custom content types (Good information architecture trumps all).

Communicating Query Results

Most fields that will be used in presentation need to be explicitly defined (by default Title, ImageUrl, and LinkUrl fields will render). In order to define the fields, export a content query web part to your desktop and modify it in notepad. Fields are defined in the “CommonViewFields” property. In addition, you can define the governing XSL (so you don’t have to modify the default style sheet) by using the “ItemXslLink” property. After you’ve made the required changes you’ll need to import the updated web part for use.

For reference, below are snippets from the Calendar CQWP:

<property name=”CommonViewFields” type=”string”>EventDate;Description</property>

<property name=”ItemXslLink” type=”string”>/sites/avisuj-team/Style Library/XSL Style Sheets/calendar.xsl</property>

Presenting the Results

This is controlled by the XSL file governing the Content Query Web Part. I typically start by copying a template that best matches what I am trying to achieve. Most often, I modify the html that follows variable declaration. Fields defined by the “CommonViewFields” property can be reference by @FieldName even if they are not defined as a variable. Common HTML rules apply.

Summary

In summary, you don’t need to be a coder to create your own content query web parts. Making your own web parts will make it easy to re-use and aggregate content throughout a SharePoint site collection. In addition, this skill becomes critical when you start using SharePoint as Web Content Management engine.

About these ads

7 thoughts on “Building a Calendar Content Query Web Part (CQWP) for SharePoint

  1. Downloaded the xsl and webpart, made the change to the webpart for the sitecollectionroot and uploaded both to the correct folders. I added the webpart to page and no content is displayed – there are tow events in the calendar. When I edit the webpart I get a yellow screen – object reference not set etc etc etc. I’m using Sharepoint 2010.

    Any ideas?

    • Hi Adrian,

      The webpart was originally created for SharePoint 2007. In 2010, fields defined in the xslt automatically display in the web part pane; as a result it appears that specifying common view fields creates the error you referenced.

      I went ahead and created a new web part. For the most part, the OOB settings work great. In order to query Calendar Lists, I used the ListsOverride property.

      At any rate the updated web part is available at https://skydrive.live.com/redir?resid=756B7CB9E93C55A0!9666&authkey=!APg90Jymi1kk-30

      • The web part totally ignores the list I choose to display and instead shows events from all calendars in the site collection.

      • That’s to be expected because I use the ListsOverride property to aggregate events. If you wanted to specify a particular calendar, you can use the out of the box content query web part.

  2. Pingback: Transparent Information Governance with SharePoint « @avisuj

  3. Pingback: Web Design, SharePoint, and Adoption – Oh My! « @avisuj

  4. Pingback: SharePoint Document Library Best Practices « @avisuj

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