SharePoint 2010 Content Query Web Part with paging
Content Query Web Part, SharePoint 2010, Tools, WCM
One of the coolest and probably most frequently used Web Parts in SharePoint 2007 was the Content Query Web Part (CQWP) – a great control that allowed you to display content aggregations to the users. Although it was pretty awesome, it had some serious drawbacks like for example the lack of a paging mechanism – something that almost every single Web Content Management project that I worked on required. Three years later in SharePoint 2010 the CQWP got a facelift and shipped with some seriously cool features like slots and the Content to Content concept. Unfortunately still no sign of paging. So what if you want to create an aggregation of your content and page the contents? Do you have to create a custom and bad performing Web Part? Not really… Introducing Imtech Content Query Web Part v14!
Imtech Content Query Web Part is the solution for the lack of paging in the standard Content Query Web Part. Using the options in the properties of the Web Part, you can specify how many items you want to display per page and what the name of the query string parameter is, that holds the number of the page. So how it works?
The first thing that you need to do is to set the Page Size. By default it is set to 0 which means, that paging is disabled. The other thing that you have to do is to modify the XSL so that the Content Query Web Part becomes aware of paging and will be able to display the paging controls.
Powered by XSL
The support for paging in Content Query Web Part requires changes to two XSL files used by the CQWP: the ContentQueryMain.xsl and ItemStyle.xsl. The first configures the parameters and passes information to the Item Templates that is required in order to display the paging controls. The ItemStyle.xsl is responsible for displaying the pager control. Depending on your scenario you might either want to create a different paging control for every template or create a single one and reuse it across all templates.
To see it all working you can download the examples of the XSL files that enable paging. Simply upload them to your Site Collection and change the links in the properties of the Imtech Content Query Web Part.
Imtech Content Query Web Part is a free Web Part and you can use it without any restrictions. Please note, that it is not supported by Microsoft and you use it at your own risk.
Download: ImtechContentQueryWebPart.wsp (7KB, WSP)
Download: ImtechContentQueryMain.xsl (19KB, XSL)
Download: ImtechItemStyle.xsl (32KB, XSL)
And if you are interested how it all really works stay tuned for the Imtech Content Query Web Part How they do it article!

















February 28th, 2010 at 9:10 pm
Hi Waldek,
Have you experienced some issue while extending SharePoint Content Query Web Part ? When i try to extend ContentQueryWebPart with a kind of NewsContentQueryWebPart, SharePoint says he can't import the webpart… the same wsp works fine with SharePoint 2010 RC. SafeControls, Gac deployment, full trust … i think there is some issue with beta 2.
March 1st, 2010 at 6:30 am
@David: Have you used the SharePoint Web Part Project Template to create the extended CQWP? If so, there was an issue indeed with an assembly attribute. Although I don't know the exact name anymore, you can find it at the bottom of your assembly properties file. Removing the attribute should fix your issue.
March 1st, 2010 at 11:39 pm
[...] SharePoint 2010 Content Query Web Part with paging [...]
April 14th, 2010 at 11:45 am
I can not use your Webpart on SharePoint 2010 Foundation Beta2.
I get an UnsafeControlException (Control can not show or import. Type not found or not secure…)
April 14th, 2010 at 11:46 am
Hi, I can not use your Webpart on SharePoint 2010 Foundation Beta2.
I get an UnsafeControlException (Control can not show or import. Type not found or not secure…)
April 14th, 2010 at 2:50 pm
@Rene: Content Query Web Part ships as a part of SharePoint Server 2010. You cannot use it with SharePoint Foundation 2010 unfortunately.
April 29th, 2010 at 7:40 pm
Hi,
I added a Imtech CQWP on a page in SP2010. It works fine but after editing this page in SPD2010 in advanced mode. This web part shows error. It says "Web Part Error: An unexpected error has occurred"
Has anyone experienced this problem before?
any solution?
May 13th, 2010 at 10:38 am
Hi, Thanks for the job, really nice stuff,
is the web part work with SharePoint Foundataion 2010? I am trying to deploy it on Foundation 2010 but not able to activate the solution.
Any suggestion???
July 16th, 2010 at 5:53 am
Thanks for another great post Waldek,
Its really pleasure to read your blogs..
I am also trying to extend the CQWP in SP 2007, Can you please share the code for this WSP with me. It would be very helpful for me….
Thanks in advance..
July 16th, 2010 at 7:55 am
@JK: Have you checked the ULS logs for more details on the exception?
July 16th, 2010 at 7:55 am
@Nilay: CQWP works only on SharePoint Server 2010.
July 16th, 2010 at 7:56 am
@Binu: Thank you for your feedback, I really appreciate it. As for the source code, I unfortunately cannot share it with you as it belongs to my previous employer. What you could do though, is to download the WSP and open the assembly using reflector so that you can see what changes I have made.
August 23rd, 2010 at 3:00 pm
I love your explanations for WSP – especially your analysis/explanations of Content Query Web Parts with Slots. Here's a situation I'm having that I hope you've either seen, or can help me with:
In our environment, we want to expand the list of columns in a custom item style by four. We want this to apply to all queries, so we've modified the ItemStyles.xsl file to include 4 "CustomColumn"n fields, in a "CustomColumns" item style. The user/administrator should be able to type the column name in the CustomColumn textbox and have that column rendered. That works fine.
Here's the problem: when the information is rendered, some columns need formatting (Due Date, Priority, etc.). As the columns are not known ahead of time and we want the ItemStyles.xsl file to apply universally, how do I apply formatting?
I'm hoping you can help. Thanks in advance.
August 23rd, 2010 at 3:45 pm
@Keith: I would say that you need to approach it from the semantic point of view. What kind of information do you want to present? Is it an overview of events with locations, times and tickets' prices or overview of press releases, with the date and the author. Only such approach will allow you to format the data according to its type. The idea of having a generic template is okay, but unfortunately limits the ability of formatting the data. You could of course extend the CQWP and create a custom XSLT function, which would allow you to retrieve the data type for each field, but it would have a big performance impact on your solution.
August 23rd, 2010 at 4:59 pm
Thanks for the information. I am indeed attempting to extend the CQWP, though it's not clear to me how I am supposed to (or if it's even possible to) format the data before it is rendered. Fortunately, in this instance, performance isn't terribly critical. I was hoping for some direction, or that you may have seen/done something similar in the past. I appreciate your time regardless.
August 23rd, 2010 at 7:25 pm
@Keith: as I said, I would definitely stick with different templates for different types of data. Otherwise you have the risk of it getting really complex. Your custom rendering would have to deal with all out of the box value types plus some custom ones that might be created in the future.
August 25th, 2010 at 7:37 pm
Thanks for the advice, Waldek. Given your comments and the issues we were already having, we decided that formatting could be handled in custom columns – columns that would then be accessed by the CQWP. Like you said – it was much too difficult to format through the web part and/or an XSLT function.
What I'm finding now is that I'm having a deployment problem. Once I was satisfied that I could get the slots working correctly, I copied the ItemStyles.xsl to a CustomItemStyles.xsl file and returned the ItemStyles.xsl to its original form (I was editting it in Sharepoint Designer). We decided to deploy this as a Feature, so we created a module for custom web parts and added the custom content query files, to include the Elements.xml, SharepointProjectItem.spdata file, the content query extension cs file, the webpart file and a folder with the xsl files in it (CustomContentQueryMain.xsl and CustomItemStyles.xsl). My understanding is that the webpart file will specify the properties for the web part, so the property entries here are where the xsl file will reside:
CustomContentQueryWebPart
Custom Content Query Web Part
/Style Library/XSL Style Sheets/CustomItemStyles.xsl
The problem is that when I deploy, activate and try to add this webpart, I get the message:
The web part references an untrusted XSL file. Only XSL files contained in this site's Style Library may be referenced.
When I look in All Files on the server (through Sharepoint Designer), and navigate to the /Style Library/XSL Style Sheets folder, I don't see CustomItemStyles.xsl there – obviously.
Should it be there? Do you know what I'm missing?
Thanks in advance.
August 25th, 2010 at 7:37 pm
Thanks for the advice, Waldek. Given your comments and the issues we were already having, we decided that formatting could be handled in custom columns – columns that would then be accessed by the CQWP. Like you said – it was much too difficult to format through the web part and/or an XSLT function.
What I\'m finding now is that I\'m having a deployment problem. Once I was satisfied that I could get the slots working correctly, I copied the ItemStyles.xsl to a CustomItemStyles.xsl file and returned the ItemStyles.xsl to its original form (I was editting it in Sharepoint Designer). We decided to deploy this as a Feature, so we created a module for custom web parts and added the custom content query files, to include the Elements.xml, SharepointProjectItem.spdata file, the content query extension cs file, the webpart file and a folder with the xsl files in it (CustomContentQueryMain.xsl and CustomItemStyles.xsl). My understanding is that the webpart file will specify the properties for the web part, so the property entries here are where the xsl file will reside:
<data>
<properties>
<property name=\"Title\" type=\"string\">CustomContentQueryWebPart</property>
<property name=\"Description\" type=\"string\">Custom Content Query Web Part</property>
<property name=\"ItemXslLink\" type=\"string\">/Style Library/XSL Style Sheets/CustomItemStyles.xsl</property>
</properties>
</data>
The problem is that when I deploy, activate and try to add this webpart, I get the message:
The web part references an untrusted XSL file. Only XSL files contained in this site\'s Style Library may be referenced.
When I look in All Files on the server (through Sharepoint Designer), and navigate to the /Style Library/XSL Style Sheets folder, I don\'t see CustomItemStyles.xsl there – obviously.
Should it be there? Do you know what I\'m missing?
Thanks in advance.
August 25th, 2010 at 9:21 pm
@Keith: I'd say you should create a separate Module for the XSLT and provision it separately from the Web Part which can be added directly to the Feature without being wrapped in a Module. Adding the XSLT file using a folder within the Web Part SPI would provision it to the Web Part Gallery instead of to the Style Library.
August 27th, 2010 at 7:04 pm
We were able to get around the deployment issue by updating the module node in the Elements.xml to designate where the XSL file should go (namely, to the /Style Library/StyleSheets folder) and setting the File node Type to GhostableInLibrary.
I could see it and open it for edit in SP Designer, but I still get the following error when I try to access it through the CQWP:
"The web part references an untrusted XSL file. Only XSL files contained in this site's Style Library may be referenced."
After exhausting several methods to fix this situation, we decided that we'd take an even closer look at your IMtech CQWP, so we imported it to our environment. We then attempted to use your CQWP to open our ItemStyles.xsl. That threw the same error message. We even tried pushing your ItemStyles.xsl to that location and got the same error.
Do you know how to fix this error, or know of an alternative for deploying/accessing an xsl file?
As always – your input is greatly appreciated.
August 27th, 2010 at 7:04 pm
We were able to get around the deployment issue by updating the module node in the Elements.xml to designate where the XSL file should go (namely, to the /Style Library/StyleSheets folder) and setting the File node Type to GhostableInLibrary.
I could see it and open it for edit in SP Designer, but I still get the following error when I try to access it through the CQWP:
\"The web part references an untrusted XSL file. Only XSL files contained in this site\'s Style Library may be referenced.\"
After exhausting several methods to fix this situation, we decided that we\'d take an even closer look at your IMtech CQWP, so we imported it to our environment. We then attempted to use your CQWP to open our ItemStyles.xsl. That threw the same error message. We even tried pushing your ItemStyles.xsl to that location and got the same error.
Do you know how to fix this error, or know of an alternative for deploying/accessing an xsl file?
As always – your input is greatly appreciated.
August 27th, 2010 at 7:27 pm
@Keith: I haven't really tried deploying the CQWP XSL files to location other than the Style Library. Are you saying that you're getting the error even when deploying the XSL files to Style Library? Could you post the exact repro steps so that I can see if I'm getting the same error or if it's something specific to your site?
August 27th, 2010 at 8:42 pm
Sorry for being so vague – and also for not specifying the exact path. We're deploying the XSL files to the "/Style Library/XSL Style Sheets" folder, which is the default location for the CQWP XSL files. When I began experimenting with CQWP Slots, I modified the ItemStyles.xsl in this folder. Everything worked great. Unfortunately, despite deploying the CustomItemStyles.xsl file to the same folder, I am unable to access it through WSP – even though I can see and modify it through Designer in that location.
Here are the steps I follow:
1. Create a module in a WSP Solution.
2. Add the CustomItemStyle.xsl (which is a copy of the IMTechItemStyle.xsl) to the module.
3. Be sure that the Elements.xml file in the module includes the following:
4. Create a feature in the Solution that includes the module just created.
5. Deploy the solution.
6. Verify through Designer that the file has been pushed and/or updated.
7. Add the IMTech CQWP to a page.
8. Go through the standard CQWP setup operations.
9. In the IMTech CQWP section of the web part, enter the location of the Item XSL Link textbox as "/Style Library/XSL Style Sheets/CustomItemStyles.xsl".
10. Hit the Apply button
At this point I get one of two messages – sometimes I get a Server Error with "Could not load XSL file" and other times I get a trapped error in the CQWP that says "The web part references an untrusted XSL file. Only XSL files contained in this site's Style Library may be referenced".
Please let me know if you need more information, or if something's not clear.
Thanks.
August 27th, 2010 at 9:00 pm
The xml was cut out of step 3, but it's basically as follows:
Within the Elements.xml file, in the Elements node,
a Module node with Name="CustomStyles" Url="Style Library/XSL Style Sheets", with a File child node containing Path="CustomStyles\CustomItemStyles.xsl" Url="CustomItemStyles.xsl" and Type="GhostableInLibrary"
I hope that helps.
September 1st, 2010 at 1:56 pm
I've stepped away from this project a few days to concentrate on other things, but I'm coming back to it today. As a relative Sharepoint newbie, I conceed that my reproduction steps might not make a lot of sense – technically and/or semantically. For that I apologize. I've pieced together steps I've read from different sources.
Anyway, I'm still getting the same error, which I've found out MIGHT mean that the custom CQWP may not be able to resolve the CustomItemStyles.xsl path, and may not have anything to do with the file being "untrusted", as the message implies.
Is it possible for you to post your deployment steps, so I can compare your procedure to what I'm doing?
It would be much appreciated.
September 2nd, 2010 at 6:08 am
@Keith: the deployment steps are just as if it was a general WSP:
1) Install the WSP
2) Deploy it
3) Go to the Site Collection and activate the Site Collection Feature to install the CQWP in the Web Parts Gallery
4) Voilla! You're done and you can start using the CQWP with paging.
If you need custom XSLT you can upload them to Style Library. Don't forget to Publish and Approve them. Although if you've tweaked something about the permissions, you might want to double check if everyone can access the XSLT files. Otherwise the Web Part might fail to work for some users.
Hope that helps.