February 6, 2012
Developer In-Depth:
prev
next
- Business logic
- governance
- application development
- BizTalk
- SharePoint
- database
- jQuery
- SOA
- programming
- Visual C#
- Visual Studio
- Exchange
- documents
- PHP
- services
- Microsoft Office
- customization
- Exchange server
- security
- collaboration
- .NET
- SharePoint 2010
- CA
- CodePlex
- developer
- search
- document management
- portal
- WSS
- Web development
- Web sites
- authentication
- XML
- Microsoft
- software
- policy
- MOSS
- Web Parts
- Office
- Silverlight
- tools
- sandbox
- SharePoint Service Account
- InfoPath
- ASP.NET
- Windows
- server
- architecture
- Libraries
- SharePoint administration
Content Query Web Part: SharePoint's Swiss Army Knife
SharePoint's Content Query Web Part (CQWP) is the Swiss Army Knife of SharePoint web parts. Not only is it good at efficiently aggregating data from a variety of locations within a site collection, but you can exert a great deal of creative control over how it renders its results.We most normally enhance CQWP output by changing ItemStyle.xsl, which is a located in the site collections root folder as shown:
Figure 1: ItemStyle.xsl.
One typically changes ItemStyle.xsl to renders results in a table format, to add additional metadata to the listing or change fonts and colors. These techniques are well known and understood (check them out here: http://www.heathersolomon.com/blog/articles/CustomItemStyle.aspx or here: http://paulgalvin.spaces.live.com/blog/cns!1CC1EDB3DAA9B8AA!491.entry).
Business Problem
That's powerful stuff but the range of opportunity to customize is more or less scoped to the individual items that the CQWP finds. ItemStyle modifications affect individual items. Many times that's good enough but what happens when you want a higher level customization? This article describes how you can use CQWP customization features to create a nice web browser navigation for long listings. Consider this business scenario:-
* You have a document library with 100 or more documents that contain white papers.
* You want to display all of these on one page and style the output to show title and description.
* You've defined some metadata for these documents (beyond the default title, author, description, etc). Specifically, your white papers have a "domain" metadata field. "Domain" includes values such as "Application Development" and "Quality Control".
* Your 100 documents are spread more or less evenly across eight domains.
* You want to use CQWP to display all 100 white papers grouped by domain and sorted by title.
Figure 2: Using CQWP Default Features.
There is a problem with a pure out-of-the-box approach, however. The web browser will scroll when you show 100 documents and that's an unfriendly interface.
Solution
There's an easy answer to this problem. Broadly speaking, we'll do the following:-
* Modify the XSL style sheet that emits the Group By headers ("Application Life Cycle Management" and "Customer Support" in Figure 2) to emit anchor tags.
* Add a content editor web part on the same page that references those displayed group headers.
Modify ContentQueryMain.xsl
First, let's edit the CQWP's display function for group headers. The file, ContentQueryMain.xsl, is located in the style library (see Figure 1 above). Open it with SharePoint designer and jump to the OutTemplate.GetGroupName section (near the end). Make a change as show in future 3:
Figure 3: Modifying ContentQueryMain.xsl.
That most interesting and key bit of XSL is the following:-
* <a id="{$GroupName}" title="{$GroupName}" href="#{$GroupName}">: Emits the group name as an anchor tab on the page.
Add a Menu
To close the loop on this, we need a navigation panel. Simply add a content editor web part on your page and create a series of links such as:-
* <a href="#Application Life Cycle Management">Application Life Cycle Management</a><P></P>
* <a href="#Customer Support">Customer Support</a>
* Add one of these for each grouping that you need
Conclusion
CQWP is a versatile web part indeed. Microsoft's architecture allows us to change the look and feel of individual items and even show entirely separate metadata via ItemStyle.xsl modifications. Take it one step further and higher and modify the enclosing ContentQueryMain.xsl to decorate the group names with anchor tags in order to enable intra page navigation. Couple that with Content Editor Web Part and you can overlay a nice navigational system on top of it.
Networking Solutions

Discover how to start developing for the Android platform with this extensive guide, which provides a reference to the Android platform as well as a look at developing your first Android application. You'll explore the top 10 features for developers as well as learn design and development tips that go beyond the phone and target tablet development as well.