SharePoint Content Query WebPart (Customizable, Powerful, and Invaluable to ANYONE who uses SharePoint)

Out of the box SharePoint provides ANYONE with almost everything you could possibly need to present SharePoint data (list items, documents, you name it..) onto a webpage. Whether you have an intranet, extranet, and/or internet SharePoint deployment one of the single most important webparts will be the SharePoint Content Query WebPart.

Not sure what it is? Basically (very generalized description) it allows you to roll up SharePoint data into a customizable presentation. Read more here:

What this really means is two things:

  1. You can roll up pretty much any kind of “query” you can think of for SharePoint data. I could roll up all the policy documents throughout the entire site collection into one page for everyone to see, or maybe roll up the latest 5 news articles and present them to the viewer.
  2. You can CUSTOMIZE the look and feel VERY easily. This can allow some normal list data that looks like a listing of information to be INTERACTIVE for a user, or to look and feel exactly how you want it to!

The second point there is extremely important for those who are using it for internet sites. Want your news articles to roll up on your homepage and show just a thumbnail, title and description? Easy as pie! What if those news articles are all over the site, and are made by many different kinds of users? As long as you structured the data (used content types or something) you can just “Query” it with the content query webpart and voila it will appear.

Now for the part many people miss: But it doesn’t look the way I want it to, and all the options still don’t make it look the way I want.

No problem! If you have anyone who can do basic HTML you should be able to pull it off. (XSL skills are probably a good thing for anything more advanced than minor changes.)

The Content Query WebPart runs off of XSL!
Microsoft released wonderful documentation on how to modify it here:

But as always I will try and provide my own little example to help anyone who hasn’t caught on yet.

  1. Add a content query webpart to a page.
  2. Here is what mine looked like:
    Take a look at the drop down for item style, what I am going to take you through is how to make your own new item style, completely customizing the LOOK and FEEL for how my data items are displayed.
  3. Navigate to the site collection root and visit the StyleLibrary. Within this library is a folder called XSL Style Sheets. We are going to modify one of the files in here called ItemStyle.
  4. You can modify it in notepad, or in visual studio or some other XML/XSL editor. For this demo I will be using visual studio.
  5. Either write your own new template using the others as a guide OR copy one of them as a starting point. In this case we will just copy the “TitleOnly” template and paste it below.Notice that we change the NAME and MATCH of the template we copied. This is what it will show up in the drop down as, and how it will associate the styles and xsl you adjust in the actual content query webpart.
  6. Add some HTML or adjust the XSL as desired. In this sample I just added an image that will appear to the left of each title.
  7. Save and check the XSL change in.
  8. Open the content query webpart you added in step 1 and cause the item style drop down to display.

    You will notice our new template shows up.
  9. Click OK or Apply and VOILA – Customized Presentation (in my case an image of myself showing to the left of each item. Narcissistic I know.)

I hope you all see the power of this WebPart and use it to save you tons of time and effort,
Richard Harbridge

P.S – I highly recommend downloading and trying this solution on a development environment: which is basically a content query webpart enhanced with more properties pushed up to the user. It also has some other slick features, but basically makes the above steps far simpler to execute and adjust.

— UPDATE: This is a codeplex project, so there are some issues, specifically when dealing with non root site collections. I am advising this as something to start from 🙂 —

Explore posts in the same categories: Feature, SharePoint 2007, Tips and Tricks, WebPart

Tags: , , ,

You can comment below, or link to this permanent URL from your own site.

16 Comments on “SharePoint Content Query WebPart (Customizable, Powerful, and Invaluable to ANYONE who uses SharePoint)”

  1. Lauri Vaittinen Says:


    This is pretty cool solution, but I have a problem. We have 4 different calendar which we “combine” with the Content Query web part. I would like to make the icons/images change depending on from which calendar the announcement is from. For instance Soccer match has a sports icon and Finace Reports announcement has finance icon.

  2. rharbridge Says:

    Hello Lauri,

    A quick solution to that could be to add an extra column to those lists which contains specific data. You could make this column, populate it, and set it to hidden with a default value that is say “Calendar1”. Now add a similar column to all of the calendars.

    In the XSL you will have access to that column, so just now adjust the icon images based on this columns data.

    That should work for what you need and requires little effort, not the best solution, but its fast and it works.

    Thank you,
    Richard Harbridge

  3. Ash Says:

    i need to use a content query webpart as a field control and fetch its data into another Content query it possible…?

  4. Ash Says:

    i want to use content query webpart as a field control and fetch its content into another content Query Webpart.
    Is it possible..?

  5. rharbridge Says:

    “i need to use a content query webpart as a field control and fetch its data into another Content query it possible…?”

    A field control basically pushes data to an item’s properties.

    So you definitely can fetch the data of item properties (and items) with the content query webpart.

    In terms of the first part, I don’t think I understand what you are trying to do. The content query webpart basically just displays data. So it’s a read only roll up of data. A field control is typically used to store and configure data. So in my mind I cannot think of a reason that promotes business value of using a content query webpart in a field control.

    That said, you can pretty much add anything with enough effort to a field control, so there’s no reason in my mind that you wouldn’t be able to add very similar functionality when you build the field control. Here’s a microsoft article on creating them:

  6. Eric Says:

    Sorry, new to all this. Thanks for your excellent article.

    How would I go about adding the Name column (i.e. the filename from a document library being queried) so it shows both Name and Title?

  7. rharbridge Says:

    So showing fields is a relatively easy thing to do. You basically export the content query webpart – open it in notepad – change one of the xml values called CommonViewFields so that includes the property you want to display (InternalColumnName, FieldType) – import it back in and it should now be available in the XSL.

    In the XSL you can add lines that look like this:


    Which shows you all the field values. (This would go just under the itemstyle template you are using).

    Ugly long process right?

    My recommendation is don’t go to all this work. There is a codeplex project called enhanced content query webpart that actually rolls this xml field up into a webpart property. So instead of all that exporting/importing you would have to do for each webpart, you can just change the field in the webpart properties.

    Here is an example of XSL where I am rendering fields that I added using the commonViewFields xml property.
    (It’s ugly as sin, but I just wanted to a quick example for you.)


  8. rharbridge Says:

    Damn you wordpress! lol – Lets see if this works.

    <xsl:template name=”CompugenQuickLinks” match=”Row[@Style=’CompugenQuickLinks’]” mode=”itemstyle” xmlns:xsl=””>
    <xsl:variable name=”LinkTarget”>
    <xsl:if test=”@OpenInNewWindow = ‘True'”>_blank</xsl:if>
    <table cellpadding=”0″ cellspacing=”0″ id=”linkitem” style=”width:100%;font-family:Arial;”>
    <tr style=”text-align:left;”>
    <td style=”padding:6px 0px 2px 10px;”>
    <a target=”{$LinkTarget}”>
    <xsl:attribute name=”href”>
    <xsl:value-of select=”substring-before(@URL,’,’)” />
    <img class=”image” src=”/_layouts/images/globe.gif” alt=”{@ImageUrlAltText}” />
    <td style=”padding:6px 10px 2px 10px;font-size:11px; text-align:left; width:100%”>
    <div style=”padding-bottom:2px;text-align:left;width:200px;”>
    <a target=”{$LinkTarget}” title=”{@LinkToolTip}”>
    <xsl:attribute name=”href”>
    <xsl:value-of select=”substring-before(@URL,’,’)” />
    <xsl:value-of select=”substring-after(@URL,’,’)” />

  9. LOVE_MOSS_NOT Says:

    “Out of the box SharePoint provides ANYONE”

    ANYONE? are you sure about that statement? C’mon SharePoint is a PITA, and you know it.

  10. rharbridge Says:

    Great to receive some feedback. When I say Anyone I really do mean it. I am not necessarily specifying that ‘anyone’ should be able to create the xsl templates, or update the webpart (export and re-import). When I say anyone I mean just editing the webpart on the page, configuring it’s query properties and presentation properties.

    I have never in my career had a user I taught to use the content query webpart be unable to use it. In some cases it takes a bit of extra training, but tends to be well worth it.

    Would love to hear what you find difficult or dislike about SharePoint more in depth. Feel free to send me messages on twitter, or comment on here. 🙂

  11. Tamara Says:

    great hint Richard!

    have you ever tried to filter calendar items by added column?

    i guess you know this famous issues with calculated column “Today” trick. Most of the time used to be able to calculate birthday date every year.

    i have a list of employees lets call People Directory (PD)(rejected to import them from AD) with employess profiles. of course would like to have all functionalities in one list. One of them is birthday. decided to move all to Calendar list thus i have recurrent event automatically (you cant add field with function “recurrent event” to a custom list). so, now i have issue to filter it by department using CQWP. i’ve successfully added aditional filter “Department” but when using it, wp returns zero items. same steps and different ways with custom list works fine. in normal calendar list view i am able to filter the list by department. also i am quite sure that i am usign correct addressing to column name.

    maybe you know for sure its impossible to do, or maybe know the trick to make it work?

    thanx in advance.

    • rharbridge Says:

      To help with clarity are you saying that the Department column is what you are filtering on?

      Here are a couple guesses based on what you have described:
      1. The Department field you are filtering on is the wrong field. Your field might be called Department or Dept, but if it’s not the site column “Department” it’s possible you are referencing the wrong field in your filter. Thus creating this confusion.

      2. Alternatively sometimes internal field names aren’t what we expect, so you should triple check the name of that field. Don’t forget that adding that field still means you have to render it in the XSL if you want to see it.

      I am pretty sure it’s the 1st item I mention where you just aren’t using a site column and instead are using one at the list level. CQWP only works with filtering on site columns. 🙂

      Hope this helps,

      • Tamara Says:

        Good day Richard! thnx for your quick reply!

        as for #1.
        “site column” – if i understand you correct then i am sure i am using my newly added column not the stnd one.
        Just will try to clear my situation.
        I have Calendar list. Added to it columns (picture, extension, job title so on). one of them Department (internal name Dept) which is single line of text. want to add this field to filtering field’s list at CQWP. i am able to see at filter list my fiels but cant filter on it. perhaps type is wrong or something. or maybe it shouldnt work at all as that is recurrent events.. and perhaps it returns too many items… but in same i’ve limited it to 100. in same time same cusomization steps works fine with Custom List

        as for #2.
        my internal column name is Dept. i am pretty sure i am addressing to it correctly as i am able to use it at XSL and view the item info successfully.

        any ideas?

  12. Tamara Says:

    little adding:
    column type i am trying to filter is “single line of text”. Internal column name is Dept

    Changes i made to cqwp:

    *this always works with standard columns, e.g. title

    Also tried:

  13. Tamara Says:

    ok… it cuts the code… not sure will paste without brackets. will replace it with “_”

    _property name=”CommonViewFields” type=”string”_Dept,text;_/property_
    _property name=”AdditionalFilterFields” type=”string”_Dept;_/property_

    *this always works with standard columns, e.g. title

    _property name=”CommonViewFields” type=”string”_Dept,lookup;_/property_
    _property name=”AdditionalFilterFields” type=”lookup”_Dept;_/property_

  14. khushi Says:

    Hey Richard.. Great article. I really appreciate it. Well, Yesterday whole day i wasted in figuring out how to show image thumbnail from picture library using CQWP. I know bit of XSL and CQWP. I did all the steps you mentioned in this article before i visited to your article. P: is not showing all the fields. don;t know why. though my picture library is setup in a way that it is deriving from various content types. all content types are based on picture content type. I want other fields along with the image thumbnail. Also like to mention that i want to present result in bulleted form.

    * Title1

    * Title2

    * Title3

    Before posting question i did try lot and i was able get the above display when pulling data(without image) from simple sharepoint list.

    But unable to work with Picture library. 😦 Don’t want to waste another day. Please suggest.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: