SharePoint 2010 Content Query webpart

Content Query webpart series:
Part 1: SharePoint 2010 Content Query webpart
Part 2: CQWP PageQueryString and PageFieldValue
Part 3: CQWP XSL customizations
Part 4 : CQWP QueryOverride
Content By Query Webpart aka CQWP is a very powerful SharePoint webpart used for showing the content from across a site collection. In this series we will look at how to configure and customize SharePoint 2010 CQWP. This series is intended for users who either have not worked on CQWP or have very less experience with it.
CQWP is a part of SharePoint publishing framework. If your site is not using the publishing feature you will not be able to see CQWP in the list of available webparts. So go to Site collection features and activate SharePoint Server Publishing Infrastructure.
Once the feature is activated, the webpart will be available 
In this series we will use a custom content Type which inherits from out of the box Announcements content type. Go to Site Settings --> Site Content Types and click on Create. Name the content type "Company Announcements'"  as shown
Notice, that we have created a new group named My Content Types to hold our "Company Announcements" custom content type. Now add a choice column  "Announcement Category" to the Company Announcements content type. Provide the following choices:
Sports
Health
Entertainment
Technology
Business
The final content type will look like this:
Create a list based on Announcements template and use the Company Announcements content type in this list. This can be done by going to list settings->Advanced Settings. Click Yes in "Allow management of content types?" and click OK. Now In List Settings click on Add from existing site content types. Select My Content Types group and add Company Announcements as shown 
Now add different company announcements items in your list
Now let's get down to business. Open a SharePoint webpart page and insert Content Query Webpart into the page. You will find CQWP in Content Rollup Category section. After adding the webpart click on Edit webpart to configure the webpart. The webpart has two main sections, Query section and Presentation section. Query section determines what kind of data will be shown and presentation section determines how the data will be shown. Drilling down the Query section shows additional sections which are self explanatory. So, let us select the following in Query Section:
Source: Show items from all sites in this site collection
List Type: Announcements
Content Type:Show items of this content type group : My Content Types

Content Type:
Show items of this content type: Company
 Announcements
Leave the audience targeting for time being.
Additional Filters: Announcement Category is equal to Sports
And  
Expires is greater than today.

In the Presentation section the first part is Grouping and sorting. As the name suggests data retrieved by the Query can be presented in proper grouping and sorting order. Next is the Styles part. The styles can be applied to Group and Items. These are xsl styles which can be customized and we will look into that in future article. For the current example let's keep the default values for everything in Presentation section except in the Field to Display section. Change the link field value to blank, otherwise you will get the following error."URL Path" is not a valid hyperlink field or URL Path. Leave the field blank to automatically generate a link to the view properties form of the returned items. 

Finally click on Apply and then OK. This was the result I got with my configured CQWP.
As can be seen all the results are from Sports category. Also, in order to fully test the results returned by the CQWP, create some items in your list from Announcement content type. Those items should not return in CQWP as we are restricting our CQWP to get data from Company Announcements content type only. Also, check your expired items should not be returned. In the next article we will work on dynamically configuring the CQWP using PageQueryString and PageFieldValue.


profile for Nadeem Yousuf at SharePoint Stack Exchange, Q&A for SharePoint enthusiasts

+