Configuring the Content Query Web Part to display library contents

Content Query Web Part
Prepared 5/27/11 by Tom Sears
Contents
Core Concepts ............................................................................................................................................... 3
Configuration Options Explained .................................................................................................................. 4
Query ........................................................................................................................................................ 4
Source ................................................................................................................................................... 4
List Type ................................................................................................................................................ 4
Content Type ......................................................................................................................................... 4
Audience Targeting ............................................................................................................................... 4
Additional Filters ................................................................................................................................... 4
Presentation .............................................................................................................................................. 5
Grouping and Sorting ............................................................................................................................ 5
Styles ..................................................................................................................................................... 5
Feed....................................................................................................................................................... 5
Examples ....................................................................................................................................................... 6
EMCC News ........................................................................................................................................... 6
EMCC Calendar.................................................................................................................................... 10
EMCC Forms/Documents .................................................................................................................... 15
Conclusion ................................................................................................................................................... 20
Content Query Web Part
Page 2
Core Concepts
At the heart of the Content Query Web Part (CQWP) is the notion of a Content Type. A Content Type is a
collection of site columns that have been grouped together. Site columns are named instances of the
field types that can be used, such as Single line of Text, Number, and Date/Time. Content Types serve as
the “blueprints” for SharePoint list items. Therefore we can say that every SharePoint item is an
instance of a Content Type.
Here is an example of the concept using Taco Bell as an analogy. Taco Bell has become the mega chain it
is by combining the same dozen ingredients in different ways and calling it something new like doubledecker burrito or crunch-wrap supreme. The different ingredients can be thought of as field types and
the recipes created using the ingredients can be thought of as the Content Type. A SharePoint list item
in a list is then the actual Chalupa they hand you at the drive through window. The Chalupa is made of
actual ingredients; the site columns.
So then there is the abstract concept of a Content Type, and its concrete manifestation: the List Item.
Every List Item is of exactly one Content Type. Because of this, it is possible to scan all the lists in all of
SharePoint and display all of the items of a particular Content Type in a single place. This is exactly what
the Content Query Web Part does.
Note that Content Query is actually flexible enough that it can be directed to only display items from a
single list, or only lists from a particular sub-site or its children, or items from every list in the site
collection. In the case of EMCC’s news functionality, we leverage the ability to point CQWP to a single
news list at the base of the site and place the web part on any page in the site collection.
Content Query Web Part
Page 3
Configuration Options Explained
Query
Source
 Show items from all sites in this site collection
Scans all the lists in this particular SharePoint site collection.
 Show items from the following site and all subsites
Scans all the lists starting from a particular sub-site as well as its
descendants. For example consider a site structure like this:
/subsite1/subsite2/subsite3/subsite4. If subsite 2 is selected, items
will be displayed from subsite2, subsite3, and subsite4.
 Show items from the following list
Scans the items in a single list.
List Type
A concept that wasn’t touched on in the core concepts is that lists can
contain items of multiple content types. There is a special kind of list
template called a list definition, and list definitions are what appear in this
dropdown box. This gives content query the ability to display all items of
multiple content types.
Content Type
 Show items of this content type group
Related content types can be grouped together to make the easier to
locate. Choosing an item in this box determines what will show up in
the following box.
 Show items of this content type
The specific content type to be displayed.
 Include child content types
Content types can be children of content types, inheriting all of the site
columns in the parent as well as adding their own unique ones.
Audience Targeting
This is an advanced topic that won’t be covered here.
Additional Filters
 The first dropdown will shows a selection of fields available to be
filtered on.
Note that custom fields may not necessarily be displayed as filter
options. If the desired site column isn’t displayed some hoops will
have to be jumped through to make it available.
 The second dropdown give a selection of different comparison types
(e.g. equals, contains, less than, greater than).
 The text box is the value that would make the condition true
Content Query will display those items that make the condition
specified in the filter(s) true.
Content Query Web Part
Page 4
Configuration options explained continued…
Presentation
Grouping and Sorting
 Group items by
The field that should be used to group items together. As an
example, consider a page displaying all news items. It may be
desirable to group items of related topics together (e.g. sports
stories clump together, alumni stories clump together, etc.)
 Sort items by
Enter the field that should be used to sort items within a group.
Note that items are grouped before they are sorted.
Styles
 Group style
This is how headings for groups are displayed
 Item style
This is how specific items are to be laid out. SharePoint provides
several “out of the box” options for this – adding custom layouts
requires modifying a special file, and making a mistake in that
file can break the display of every Content Query Web Part in
the site collection.
 Fields to display
Maps fields associated with the item style to fields from the list
item – advanced topic, see examples.
Feed
This is an advanced topic that will not be covered here.
Content Query Web Part
Page 5
Examples
EMCC News
The following example will demonstrate how to configure a CQWP to display news items falling under a certain
category.
1. On the desired page, select Edit Page from the ribbon.
2. Web parts can be added to “Web Part Zones” or in to content areas. The former case will be used for
demonstration purposes.
3. In the Web Part Zone click the Add a Web Part link.
4. In the ribbon, select Content Rollup from the Categories box. Then select Content Query from the Web Parts
box. Click Add.
A blank CQWP appears on the page.
5. Click the open the tool pane link in the blank CQWP.
Content Query Web Part
Page 6
6. Scroll up to the upper right of the page. Expand the Query section.
7. Select the Show items from the following list radio button then click Browse.
8. Under the Home site, select the News item. Click OK.
/Lists/News appears in the box below the Show items from the following list heading.
9. Under the Content Types heading, choose EMCC Content Types from the Show items of this content type
group dropdown.
Because this list has only a single content type the second dropdown is automatically populated.
Content Query Web Part
Page 7
10. Under the Additional Filters heading select News Category from the first Show items when dropdown.
11. From the second dropdown choose Contains as the filter comparator.
Note: the news category column is a multiple-choice column. The items that are actually checked on the list
item are represented internally as “option 1;#option 3”. This is why it is necessary to use Contains.
12. In the open-entry text box type in the exact name of the category.
This can be verified by going to the home site, clicking view all site content from the site actions menu, locating
the news list, and viewing the actual news item.
13. Expand the Presentation section.
14. Under the Sort items by heading select Created from the first dropdown.
15. Make sure the Show items in descending order radio button is selected.
16. Check the Limit the number of items to display box.
17. Enter 3 for the Item Limit.
18. Under the Styles heading select HomePageNews from the Item style dropdown.
Content Query Web Part
Page 8
19. In the Description box, enter Summary
20. Expand the Appearance section.
21. Select None from the Chrome Type dropdown.
This will prevent SharePoint for putting a border around the web part, as well as hide the web part title.
22. Click OK
23. Publish the page.
The filtered news stories appear on the page.
Content Query Web Part
Page 9
EMCC Calendar
The EMCC calendar configuration is very similar to the EMCC news configuration. This section will demonstrate how to
correctly configure the Content Query Web Part to display a listing of events.
1. On the desired page, select Edit Page from the ribbon.
2. Web parts can be added to “Web Part Zones” or in to content areas. The latter case will be used for
demonstration purposes (the former case was demonstrated for a News display).
3. Place the cursor at the point in the content area where the CQWP should be displayed.
4. Select the Insert tab from the ribbon. Click the Web Part button.
5. In the ribbon, select Content Rollup from the Categories box. Then select Content Query from the Web Parts
box. Click Add.
A blank CQWP appears on the page.
6. Click the open the tool pane link in the blank CQWP.
Content Query Web Part
Page 10
7. Scroll up to the upper right of the page. Expand the Query section.
8. Select the Show items from the following list radio button then click Browse.
9. Under the Home site, select the Events item. Click OK.
/Lists/Events appears in the box below the Show items from the following list heading.
10. Under the Content Types heading, choose EMCC Content Types from the Show items of this content type
group dropdown.
Because this list has only a single content type the second dropdown is automatically populated.
11. Under the Additional Filters heading select Event Category from the first Show items when dropdown.
Content Query Web Part
Page 11
12. From the second dropdown choose Contains as the filter comparator.
Note: the event category column is a multiple-choice column. The items that are actually checked on the list
item are represented internally as “option 1;#option 3”. This is why it is necessary to use Contains.
13. In the open-entry text box type in the exact name of the category.
This can be verified by going to the home site, clicking view all site content from the site actions menu, locating
the events list, and viewing the actual news item.
14. From the second Show items when dropdown select Event End Time.
15. From the second comparator dropdown select is greater than or equal to.
16. From the radio buttons that appear select [Today]
The second filter will make it so that events that have already passed “fall off” the display.
17. Expand the Presentation section.
18. Under the Sort items by heading select Event Start Time from the first dropdown.
19. Select the Show items in ascending order radio button.
Content Query Web Part
Page 12
20. Uncheck the Limit the number of items to display box.
21. Under the Styles heading select EMCCEvents from the Item style dropdown.
22. In the $Resources:Time,Event_x005F_x0020_Start_x005F_x0020; box, enter Event Start Time
23. In the $Resources:Location,Event_x005F_x0020; box, enter Event Location
24. Expand the Appearance section.
25. Select None from the Chrome Type dropdown.
This will prevent SharePoint for putting a border around the web part, as well as hide the web part title.
Content Query Web Part
Page 13
26. Click OK
27. Publish the page.
28. The calendar events appear on the page.
Note for that this screenshot the list was left unfiltered.
Key Similarities with News Configuration Example

As with news this Content Query pulls its data from a single SharePoint list.
Notable Differences from News Configuration Example




This example demonstrated adding the Calendar to the content area on a page as opposed to a web part zone in
the news example.
The Events list was selected instead of the News list in the Show items from options.
An additional second filter was added so that events that are in the past are not shown.
Because the calendar displays more fields from the SharePoint list an additional entry had to be made in the
Fields to display section.
Content Query Web Part
Page 14
EMCC Forms/Documents
This configuration marks a significant difference from the previous two. In this configuration, Content Query will be
configured to display items from any library in the site collection on any page.
Just like with Picture Galleries there is the concept of separation between storage and display. This section will also
cover how to add the storage component.
Adding an EMCC Document Library to a site
For the purposes of this example a fictional subsite has been created at /sandbox
1. From the Site Actions menu select More Options.
2. In the Create window select EMCC Document Library.
Content Query Web Part
Page 15
3.
4.
5.
6.
Click the More Options button on the right side of the window.
Give the library a name e.g. Forms
Select No under Display this document library on the quick launch
Click Create
A document library has been created on the site. Following creation, SharePoint automatically moves to the Library’s All
Item page where documents can be uploaded. To manipulate its contents in the future go to Site Actions then View All
Site Content. The library will appear under the Document Libraries heading.
In the create window there was an option for a Document Library as opposed to an EMCC Document Library. The
difference is in the content type that the library holds. A special content type was created that inherits from
SharePoint’s Document content type to support an extra description field that will describe the contents of the
document.
Adding documents to the document library
1. Click the Add Document link.
2. Click the Browse button.
Note: it’s recommended to avoid using the Upload Multiple Files functionality as this will not prompt for a form
description.
Content Query Web Part
Page 16
3. Locate the desired file. Select it and click Open.
4. Click OK.
The file will be uploaded. When uploading is complete, a new window will pop up prompting for some additional
data.
5. Enter a brief description of the document’s contents in the Form Description box.
Note that the Title field isn’t used for any display purposes and can be ignored.
6. Click Save.
The document now appears in the library.
Content Query Web Part
Page 17
Configuring the Content Query Web Part to display library contents
Inserting a Content Query Web Part on a page has been covered in the previous two examples and will be omitted here.
Begin by inserting a Content Query Web Part on the desired page and open the tool pane.
1. Expand the Query section.
2. Select the Show items from the following list radio button then click Browse.
3. Expand the appropriate sub sites (in this example, sandbox is used) and select the appropriate library. Click OK
The site-relative path appears in the box below the Show items from the following list heading.
4. Under the Content Types heading, choose EMCC Content Types from the Show items of this content type
group dropdown.
Because this list has only a single content type the second dropdown is automatically populated.
5. Expand the Presentation section.
Content Query Web Part
Page 18
6. Select EMCCForms from the Item style dropdown
7. In the $Resources:Description,Form_x005F_x0020; box enter Form Description
8. Click OK.
9. Publish the page.
The contents of the library are displayed on the page. Note that the document title will never be wrapped. File names
should be kept short and to the point and the description field should be used to describe the contents of the document.
Notable Differences


In the previous examples, a single repository was kept for all items of the particular content type (i.e. News or
Events). In this example repositories can be placed on any site and CQWP is configured to display from there.
In this example filters were not necessary. Filtering should be implicit based on the site the library lives on and
the title of the library.
Content Query Web Part
Page 19
Conclusion
There’s a big intuitive leap to be had here that should be pointed out. Any one of these examples can be made to
behave like any other through a small amount of work now. For example, there could be several independent calendar
lists spread throughout the site and CQWPs that are configured to display their contents just like the main calendar.
Same goes for news. It is also trivial now to change how forms and documents are handled and change it to a single
global list even if it doesn’t make sense to do so.
Furthermore, it is possible through changing the query options to “Roll Up” the form libraries on child sites to a parent
site and have them group together. Content Query is an extremely powerful tool in the toolbox.
Content Query Web Part
Page 20