Andrew Connell [MVP SharePoint]
1728 Posts |  46 Articles |  5281 Comments
.NET  |  MCMS  |  SharePoint  |  Office System
AC's Blog Quick Links
SharePoint Quick Links
Article Categories
Archives
February, 2012 (2)
January, 2012 (12)
December, 2011 (6)
November, 2011 (3)
October, 2011 (17)
September, 2011 (8)
August, 2011 (6)
July, 2011 (7)
June, 2011 (13)
May, 2011 (9)
April, 2011 (15)
March, 2011 (1)
February, 2011 (6)
January, 2011 (5)
December, 2010 (11)
November, 2010 (6)
October, 2010 (12)
September, 2010 (5)
August, 2010 (4)
July, 2010 (5)
June, 2010 (6)
May, 2010 (11)
April, 2010 (11)
March, 2010 (9)
February, 2010 (9)
January, 2010 (3)
December, 2009 (10)
November, 2009 (15)
October, 2009 (15)
September, 2009 (7)
August, 2009 (4)
July, 2009 (10)
June, 2009 (8)
May, 2009 (2)
April, 2009 (9)
March, 2009 (6)
February, 2009 (16)
January, 2009 (6)
December, 2008 (12)
November, 2008 (12)
October, 2008 (27)
September, 2008 (13)
August, 2008 (14)
July, 2008 (14)
June, 2008 (12)
May, 2008 (23)
April, 2008 (12)
March, 2008 (15)
February, 2008 (13)
January, 2008 (12)
December, 2007 (10)
November, 2007 (8)
October, 2007 (15)
September, 2007 (20)
August, 2007 (21)
July, 2007 (16)
June, 2007 (8)
May, 2007 (25)
April, 2007 (16)
March, 2007 (18)
February, 2007 (18)
January, 2007 (12)
December, 2006 (16)
November, 2006 (13)
October, 2006 (18)
September, 2006 (22)
August, 2006 (27)
July, 2006 (23)
June, 2006 (23)
May, 2006 (23)
April, 2006 (9)
March, 2006 (17)
February, 2006 (15)
January, 2006 (23)
December, 2005 (31)
November, 2005 (32)
October, 2005 (38)
September, 2005 (53)
August, 2005 (30)
July, 2005 (63)
June, 2005 (30)
May, 2005 (59)
April, 2005 (29)
March, 2005 (74)
February, 2005 (27)
January, 2005 (22)
December, 2004 (32)
November, 2004 (42)
October, 2004 (39)
September, 2004 (20)
August, 2004 (14)
July, 2004 (27)
June, 2004 (40)
May, 2004 (5)
April, 2004 (6)
March, 2004 (16)
February, 2004 (26)
January, 2004 (23)
December, 2003 (7)
November, 2003 (14)
October, 2003 (20)
September, 2003 (4)
Post Categories
Add to Technorati Favorites

Managed Windows Shared Hosting

The Content Query Web Part (CQWP) is an absolutely killer for MOSS 2007 Publishing sites. It is one of the best performing rollup Web Parts available to developers and should always be considered before rolling your own solution. You can even do some really sick customization to the presentation of the results produced by the CQWP as shown in this post on the ECM team blog by George Parantatos, a PM on the product team:

» ECM Team Blog: Configuring & Customizing the CQWP

However there are two things I always hear about the CQWP in terms of wish list type stuff:

  1. It doesn’t contain any built-in paging mechanism… yeah, I too wish for this.
  2. It doesn’t support dynamic filtering. For instance, I have to manually specify the filtering values rather than have them be dynamically set.

Before you rush off and roll your own, stop, get a cup of coffee, and saddle up next to your laptop. With just a few lines of code you can utilize the provided CQWP but add your own customizations. Take the second pain point: no dynamic filtering. Let’s say you have a slew of content and you want to dynamically set the filtering values, say for example, using values on the query string.

So, we start with a site filled with pages. In the below screen shot, you’ll see a subsite called Widgets that contains 10 pages. Each of these pages contains some bogus content, but there is a field called Division that has one of a few different values in it.

PagesLibrary

When I add the OOTB CQWP to the page, the default view gives me all pages in the site:

NoCustomizations

I could tweak the filtering values in the Task Pane by setting the Division field to Europe. But… what if I want to make this dynamic?

OK, time to whip open Visual Studio. What I’ll do is create a new Web Part that inherits directly from the CQWP: Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart. Let’s take a look at the properties available to us. Hey, I see two that are good candidates: FilterField1 & FilterValue1. No guessing involved, the names say exactly what they do. So, what if I set the values of those properties on the fly, but let the rest of the CQWP do its work? Ah… now it’s party time!

What I’ll do is override the OnLoad() method and do a quick check to see if there is something expected on the query string… if not, then I need to just stop here… nothing to do. But, if there IS something there… ah… now that’s where I want to add my special sauce. Here’s the code, followed by an explanation.

Code

First, I need to set the chaining operator. That is the AND/OR switch you set when you are setting filter values using the Task Pane for the CQWP. In this case, I just want to set the Filter1ChainingOperator to OR the two values (line #15). Next, I need to get the name of the field & value to filter by from the query string (lines #18 & 22). Before going further, we need to deal with a little curve ball the CQWP throws at us. When it filters on a specific field, it does so not using the field by name, but by the field’s ID. So we need to get the field’s GUID which we can do by looking at the current lists’s Fields property (or, more specifically, we can look at any item in the list as I do in the code above) (line #20). Last but not least, set the values on the CQWP’s fields (lines # 25 & 26).

Now… package everything up and deploy it just like any other Web Part. All that’s left is to add it to a page. Initially it does the exact same thing as the stock CQWP because no query string filter values are set. But when you add them manually (or through some link on your site), you’ll see it does the filtering as shown in the following image! You can even see the values are set when you look at the query builder in the Task Pane.

Customizations

And there you have it… a subclassed CQWP implementing dynamic filtering in less than 30 lines of code. Hey, if you look at the guts of the solution, the whole thing is really only 8 lines of code in the OnLoad() method! Now that’s easy!

You can download the source of the project I created to pick through it yourself. There’s already a WSP in the ZIP that you can deploy to see this guy in action.

» ContentQueryExWebPart – dynamic filtering sample source

posted on Monday, February 18, 2008 4:18 PM

Feedback

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 2/19/2008 1:17 AM Waldek Mastykarz
Gravatar Great post presenting the power of extensibility of SharePoint.
CQWP is definitely a great part of SharePoint: especially when you look at the performance considerations. What holds me back is the way you would deploy it including the custom XSL styles. As either overwriting or extending the existing ItemStyle.xsl isn't an option, it seems like the only one left out there is exporting the CQWP, changing the references to point to some CustomItemStyle.xsl and then use that instance. Am I right or am I missing something?

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 2/19/2008 2:21 AM Kristof Eschberger
Gravatar hooray! you're my hero!
great post, thanks!

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 2/19/2008 8:31 AM AC [MVP MOSS]
Gravatar Waldek-
That's how I do it.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 2/19/2008 4:41 PM Chris
Gravatar Great post. Was wondering if you have come across how to be able to expand/collapse a grouping header? I am able to get everything else working correctly but also want the functionality to be able to expand/collapse headers with the CQWP.

Thanks

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 2/22/2008 6:10 PM George Perantatos
Gravatar Waldek, what is preventing you from editing the ItemStyle.xsl to customize styles?

As a side note, you also have the option of making a web part available in the web part gallery that's pre-configured to point to your own XSL, so when users drag/drop it on their pages it's picking up CustomItemStyle.xsl and not ItemStyle.xsl.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 2/23/2008 6:00 AM Waldek Mastykarz
Gravatar George-

As it is totally plausible you will manually edit the ItemStyle.xsl in your dev environment, it is not something you are likely to do while deploying your solution to the production env. As you are not allowed to modify through your custom Feature any file provisioned by another Feature, the only possibility left out there is provisioning your own CustomItemStyle.xsl containing the required templates.
I know that you can create your own preconfigured instances of CQWP. Yet it doesn't always seem right if you compare the amount of work you need to do in order to make the CQWP does slightly something else than expected. I think that understanding the way the CQWP works would allow SharePoint developers to create their own Web Parts leveraging the great performance approach used by the CQWP and yet providing a higher flexibility level.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 2/23/2008 9:52 AM AC [MVP MOSS]
Gravatar George/Waldek-
While customizing the ItemStyle.xsl file is certainly an option, in the real world I don't see it as a good one. The primary reason is because customizing it affects all instances that use it which I find most people don't like in the real world. They would rather customize it for a specific instance, at least 50%+ say this from my exp. Thus, it's basically manditory that you have to create custom XSL files. One negative thing about this is not being able to put an absolute link in the Web Part definition... you have to put a relative link back to the file which is a challenge in bigger sites.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 3/17/2008 11:07 AM Joaquín
Gravatar Very nice article.

It happens to me that filtering properties are used only after I've edited web part properties and saved them.

It looks like "customization engine" knows no user has modified webpart properties so it uses its default values all the time (no filtering).

If after inserting the web part in may page I check its properties, I see they are correctly set.
I then I jus press apply button filtering happens from then on.

How could I solve this. Am I doing anything wrong?
Any help would be appreciated.

Thanks,
Joaquín.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 4/10/2008 2:37 AM Bo Huge
Gravatar Hi,

Which properties do I have to change in order to make my customItemStyle.xsl visible to the WP? An how do I reference it? I tried changing ItemXslLink to /Style Library/XSL Style Sheets/MyCustomItemStyle.xsl, but the CQWP tells me it can't find the xsl. Am I forgetting something?

Thanks in advance.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 4/10/2008 10:07 AM AC [MVP MOSS]
Gravatar Bo-
That's the correct property (ItemXslLink). If the CQWP is nested in a subdirectory you might have to get creative pointing to the the XSL file.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 4/17/2008 9:19 AM Troy
Gravatar Hi!

Thanks for the great tips, Andrew! Do you know of any way to modify the CQWP to work outside of the Site Collection scope? There is a big demand in our company for a rollup like CQWP part that works across different Site Collections .

Thanks again!
Troy

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 4/17/2008 1:02 PM AC [MVP MOSS]
Gravatar Troy-
No... it is not possible for the CQWP to be scoped outside the current site collection. However I'd recommend you check this Web Part out: http://www.lightningtools.com/pages/lightning-conductor-web-part.aspx. It does a great job of acting like a cross-site collection CQWP.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 4/18/2008 2:09 PM Troy
Gravatar Thanks Andrew!

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 4/22/2008 12:38 AM Anthony Kasses
Gravatar Hi Andrew,

Really nice article.

What if I want to return random pages/items from that site? Can this be done by overriding the CQWP as well, or do I need to start from scratch with a custom web part. Seems a pity to do that because this OOB web part is exactly what I need, ..I've written the xslt, ....just missing, like you say, more dynamic filter options.

thanks.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 4/22/2008 9:00 AM AC [MVP MOSS]
Gravatar Anthony-
You might be able to do this with some sort of fancy XSLT that randomized the result set passed down by the CQWP. Or you could take the CQWP an make it do some randomized filter to pull the content back too.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 5/7/2008 6:43 AM superolaf
Gravatar Hi, I have some problems while trying to sort my CQWP based on my own fields, added under commonfields in the web part. I get the following error:

Value does not fall within the expected range.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Value does not fall within the expected range.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[ArgumentException: Value does not fall within the expected range.]
Microsoft.SharePoint.SPFieldCollection.GetFieldByDisplayName(String strDisplayName, Boolean bThrowException) +153
Microsoft.SharePoint.SPFieldCollection.get_Item(String strDisplayName) +61
ContentQueryExWebPart.ContentQueryExWebPart.OnLoad(EventArgs e) +578
System.Web.UI.Control.LoadRecursive() +66
System.Web.UI.Control.LoadRecursive() +191
System.Web.UI.Control.LoadRecursive() +191
System.Web.UI.Control.LoadRecursive() +191
System.Web.UI.Control.LoadRecursive() +191
System.Web.UI.Control.LoadRecursive() +191
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2604

Can someone help me with this?

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 5/7/2008 7:49 AM AC [MVP MOSS]
Gravatar superolaf-
Not possible to say without knowing exactly what's going on. My guess is you are setting a value (or not setting a value) on a field the CQWP needs. Debug that Web Part and find out what property you're trying to set or what property is throwing an error. From the call stack, it's something about a specific field.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 5/23/2008 9:43 AM mark
Gravatar i want to cehck page column with cqwp list field for filter. pls help me out

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 5/23/2008 10:28 AM AC [MVP MOSS]
Gravatar Mark-
From within the same class you're subclassing, just look at the current page's fields using SPContext.Current.ListItem.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 5/26/2008 5:18 AM Bo Huge
Gravatar Regarding customitemstyle.xsl. Changing the reference using the property ItemxslLing works, if I create my site coll from the root, but if I create the site coll. under sites (server/sites/sitecoll) the link doesn't work. I then get the error: Xsl cannot be found...

Has anyone found a way to handle this?

Best regards.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 6/2/2008 5:24 PM Mdeveloper
Gravatar Anyone can help me how to add sorting to the content query webaprt?

~Mdeveloper

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 6/3/2008 7:34 AM AC [MVP MOSS]
Gravatar Mdeveloper-
It's there OOTB

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 6/6/2008 11:08 AM arec
Gravatar Great MAN!!! Clear and Simple

Thanks!!

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 6/27/2008 2:02 PM Steve
Gravatar Hi, thx for this..its a great post, really helpful.

I wondered if you have experienced any problems with spaces e.g. If the value in the list has a space in, this method doesnt work.
I have %20 in the URL for the space, and when I trace the value it shows the space, but does not return and results.
Search for values without spaces...works perfect.

Any ideas??

Thx.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 7/3/2008 6:07 AM bhargavi
Gravatar a very good post... i wanted to check if we can achieve the expand/collapse of the grop headers in CQWP. Is this possible with content query webpart?



# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 7/3/2008 7:32 AM AC [MVP MOSS]
Gravatar bhargavi-
Sure... you can do that but it won't be done with a subclassed CQWP. Instead you want to look at doing something with custom styles and injecting your own DHTML that knows how to collapse groups.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 7/7/2008 5:21 PM paisley
Gravatar Steve -- RE: the "space" issue
the encoded value is _x0020_ not %20


# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 7/9/2008 2:04 AM Meera
Gravatar Great post.....great idea....thanx a lot !!
Is there any way we can use CQWP to display the latest 5 documents....to be more precise....the most recent 5 documents added to document libraries in a site??
How can we use a timer in conjunction with CQWP?

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 7/9/2008 9:55 AM AC [MVP MOSS]
Gravatar Meera-
Sure you can do that. Just sort the modified/created date in descending order, select only libraries of type Document Library, and set the paging to 5. You don't need any custom code here! I don't get your question about timers.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 7/14/2008 6:19 AM Kieran
Gravatar Hi Andrew,

I've downloaded and installed the wsp. Then activated in my MOSS site. When I try to add it to a page I get a dialog:

Unable to add the selected web part(s)

ContentQueryExWebPart: An error has occured.

Any idea's?

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 7/14/2008 9:11 AM AC [MVP MOSS]
Gravatar Kieran-
Nope... not with that information. Look at the SharePoint logs, turn off the custom errors and turn on the call stack to give you more details.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 7/21/2008 5:59 AM Kieran
Gravatar Andrew,

It seemed I needed to change the trust level to medium to get this to work - Is that the best approach?

All the best.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 8/5/2008 4:30 AM Kieran
Gravatar Hi Andrew,

Would it be possible to make this sub classed web part filter the returned values based upon a parameter passed in on the url that decides which site to display items for?

For example:

?Filter1Field=Division&Filter1Value=Europe&Site=Departments

I'm trying to prevent having to duplicate the contentquerywebpart on different pages in different sites and instead have one page that takes care of things.

All the best

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 8/5/2008 8:51 AM AC [MVP MOSS]
Gravatar Kieran-
Sure... that's just another property of the CQWP. Look at the WebURL property on the CQWP.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 8/13/2008 10:54 AM James McDonnell
Gravatar Hi Andrew,
I'm having the same problem that Joaquín posted back in March:
It happens to me that filtering properties are used only after I've edited web part properties and saved them.

It looks like "customization engine" knows no user has modified webpart properties so it uses its default values all the time (no filtering).

If after inserting the web part in may page I check its properties, I see they are correctly set.
I then I just press apply button filtering happens from then on.

How could I solve this. Am I doing anything wrong?
Any help would be appreciated.


# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 8/14/2008 5:17 AM Tim
Gravatar Nice article thanks, CQWP is indeed very useful. I wonder though if the purpose to which I am trying to apply it now is correct or if I am asking the wrong web part to do something it can't.

I often have clients ask me to build a page that displays sites, or lists or libraries and display the results as a weburl. They are looking for a dynamically created "site" map. If the add a new wiki library then the link to the new library will be added to the page. Add a new discussion and bingo, new link. And of course add a blog or team site and the link to the site is also dynamically created.

I am struggling trying to understand which the right web part is. I can find zero documentation on the "sites in category" web part so have been putting effort into the CQWP. But am I wasting my time? Would appreciate any guidance offered very much.

Cheers

Tim

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 8/15/2008 4:47 PM Mike Sharp
Gravatar Hi Tim,

Have you looked at AccessChecker on Codeplex? This is intended for site administrators, but it's not too much of a stretch to think of this as a site map. It not only provides a site map, but it's security trimmed as well:

http://www.codeplex.com/AccessChecker

Regards,
Mike Sharp

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 10/14/2008 2:29 PM Ketan
Gravatar Hi Andrew,
Can we add the dynamic filter as a dropdown control to the CQWP, so that the user can on the fly change the filter criteria and the CQWP will only display the filtered items. To add one more twist can this be added to a UPDATEPANEL, to get the ASYNC (AJAXED :) ) effect, so that the entire page will not post back.
Appreciate your response.
Any steps towards achieving this.

Regards



# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 10/14/2008 3:30 PM AC [MVP MOSS]
Gravatar Ketan-
I don't see why you couldn't do this... but it's all custom stuff (ala subclassed). Steps? Yeah... go try it yourself :)

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 10/16/2008 9:15 AM Ketan
Gravatar Thanks for the quick response Andrew, I am new to AJAX and MOSS also. Can you please let me know what would be the steps to implement this for AJAX style CQWP. Is it like subclass the CQWP, expose the column property (basically set the column/field that will allow users to select the filter critieria i.e. the field value). Can we put webparts directly into the UpdatePanel of AJAX controls.
I am sorry to bother you again on this, but as your the champ of WCM I thought only guru can answer me the best.

Thank you

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 10/16/2008 12:30 PM AC [MVP MOSS]
Gravatar Ketan-
I'm not sure you can easily throw a CQWP into the UpdatePanel... you'd just have to try it. Sorry I can't provide more guidance here... would require just taking time to try some stuff out.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 10/22/2008 2:47 AM wibrand
Gravatar Hi Andrew.

Great post about the CQWP.

I have implemented it on a page where dynamic filtering is needed, but my problem is that I need to have two filter values. When I have implemented this – the CQWP won’t filter correctly. When I look in the edit webpart settings on the front end, I can see my two filter fields and values, but I have to press apply on for the CQWP to show me the correct result.

I can’t figure out why this is a problem.

Cheers


# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 10/23/2008 11:41 PM MOSSBUDDY
Gravatar AWESOME POST and what else can we expect from a great guy ANDREW.
This is really very good stuff and right on target for one of my requirements, thanks Andrew for sharing this with us.

I would like to ask as we filter the CQWP by clicking on some link and then going to a page that contains this CQWP that filters the results, is there anyway to set the TITLE of the CQWP as the selected FILTER criteria?
i.e. if I am on some page that shows 10 items and if I click on one of the items it takes me to the detailed page for that item (via the filter criteria of CQWP) and also set the title of the CQWP to the selected criteria, can we do this in the Onload event of CQWP?

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 11/6/2008 11:27 AM Thomas
Gravatar I have a DataSheet View webpart. Is there any method to filter it dynamically by reading value from querystring?

Thank You,
Thomas

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 12/1/2008 5:43 PM Yoseph
Gravatar Hi Andrew,

I am trying to use your awesome trick :) , however i haven't got it working yet. My scenario is:
Instead of having, WebPart Pages, i have subsites.
Within that subsites, i have custom lists.
Root
|-- subsites 1
|-- Products
|-- subsites 2
|-- Products
|-- subsites x
|-- Products

I put the Content Query Ex WebPart within the root.
It gives me "Object reference not set to an instance of an object." and i have debugged it, it is something to do with
"Guid filterFieldID = Microsoft.SharePoint.SPContext.Current.Item.Fields[filterFieldName].Id"

It tries to find a current 'item' in a list but it just couldnt find it.
Do you have any ideas ? or am i missing something?

Thanks.



# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 12/2/2008 7:54 AM AC [MVP MOSS]
Gravatar Yoseph-
Sounds like the field you're searching on (that's in the QueryString) is not in the list of fields in your content. Recall you have to use the internal field name, not the display name.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 12/3/2008 12:57 AM Yoseph
Gravatar Hi Andrew,

Thanks for your help. Now it is working 95% as expected :p
The other 5% is :) the style.
If i set the style in ContentQueryEXWebPart as "Bulleted Title", The style doesn't show up.

Thanks for your help and time.



# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 12/4/2008 5:20 PM Yoseph
Gravatar Hi Andrew,

just to give more information, when i declare the class, i put three paramatres for the stylesheets.
I declare it like:
public MyCustomContentQueryWebPart(): base
("/Style Library/XSL Style Sheets/ContentQueryMain.xsl",
"/Style Library/XSL Style Sheets/Header.xsl",
"/Style Library/XSL Style Sheets/ItemStyle.xsl")

Am i missing something somewhere?

Thanks for your time and help.


# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 12/6/2008 2:29 AM AC [MVP MOSS]
Gravatar Yoseph-
Yeah... you don't need to do that. I'm not sure why you're adding those style sheets... they are specified within the Web Part definition.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 12/8/2008 9:57 PM Yoseph
Gravatar Hi Andrew,

I started with no parametres class declaration. As it didnt show my stylesheet, i put those parametres in the class constructor. but still no joy.

As long as i put together the OOTB content query web part, it displays the stylesheet automatically.

do you know why it is happening like that?

Thanks.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 12/8/2008 10:22 PM AC [MVP MOSS]
Gravatar Yoseph-
You don't specify the style sheet in the code... but you can. Custom stylesheets go in the ItemXslLink property.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 12/11/2008 1:06 AM Ibrahim
Gravatar I am using the out of the box Content query web Part in MOSS 2007. The web part is working perfectly with filtering and sorting. But when I try to group the rows using any field, the web part only group the rows in 5 groups. The rest of the groups will be displayed on top of the web part. I try to export the webpart and modify the XML file by changing the "DisplayColumns" from 5 to 50; then export the webpart in Edit view. It work perfectly in Edit group, but when I exist the edit view the web part set back the "DisplayColumns" to 5 and I get the previous look and feel. Is there a way to overcome this and display more than 5 groups in the Content Query Web Part? Thanks

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 1/21/2009 3:33 PM michelle
Gravatar I can't get the filtering to pick up the values in the URL. I used the following:.../Technology/default.aspx?PageView=Shared&fFilter1Field=Investor&Filter1Value=Yes
Not matter what I put on the query string, it still brings back the same results. I was wondering if "publishing sites" was a special type of site or is it just any site in the collection? Thanks.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 2/26/2009 6:21 AM Saranya
Gravatar Hi Andrew,
A very nice article.... I have a doubt. I want to filter a multitext values dynamically based on a querystring value....
for eg) if my productname="Soap" i will get this value in my querystring. I want to filter in my CQWP using this value Is it possible? If so how to do that?
I m badly in need of help.


# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 2/26/2009 2:42 PM AC [MVP MOSS]
Gravatar Saranya-
Not sure how that's much different from what I suggested. If anything, you could dynamically find the field named "productname" and use the GUID of that field as the FilterField1 value.

# Subclassing the Content Query Web Part: Adding Dynamic Filtering 3/2/2009 6:16 AM Ash
Gravatar i want to use content query web part as control and fetch its content into another content query web part.
is it possible...?

# Content Query Web Part: Adding Dynamic Filtering 3/2/2009 6:23 AM Ash
Gravatar i have a scnerio where on the Home page,i have to display the title of sites along with links to its subsites in a content query webpart.
anyone can help me on this..?

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 3/3/2009 12:26 AM Saranya
Gravatar Hi Andrew,

Since its a mutiple lines of text column, i want to filter that column with Contains Keyword. Any idea on how to achieve it?

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 3/6/2009 2:52 AM Boris Gomiunik
Gravatar Almost what I'd need. Is it possible to read multiple filter values? For example from a certain list where user enters multiple items and use them with the "OR" operator? For example: in list FilterValues I'd enter three rows of data with titles "Item 1", "Item 2", "Item 3".

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 3/8/2009 12:53 PM Ray
Gravatar Hi Andrew:
I have Googled a ton of stuff trying to find an OOTB CQWP like this. But, I am having the same problem as Michelle (1/21post). I worked my way through the trust issues and finally got the webpart added to a page. Now no matter what I do, the filters do not work. Did you have any comments to Michelle not posted?

This solution is exactly what I need to make a dynamic filter called from a mapping appliation .. work.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 3/8/2009 3:24 PM AC [MVP MOSS]
Gravatar Ray-
Not sure what's going on for Michelle or you. She mentioned her's wasn't a Publishing site... CQWP isn't supported outsite of Publishing sites.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 3/14/2009 10:28 PM Henry Winkler
Gravatar I tried a similar approach to subclass CQWP and then filter it with values from another web part on the page through web part connections. I tried all sorts of methods to make this happen, but no joy.

Have you ever looked into this?

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 4/21/2009 5:57 AM est
Gravatar Hi,
can it filtered by dateTime too? between their own field? As im trying to get latest dated file , but not latest modified date of the file. so for eg i got a file that is shiftreport20090421 and i want that file to display as it is the latest dated file in the folder (currently). but someone made ammendment to 20090415's file and upload it. However i still need the file to be displayed is shiftreport20090421 instead of the just uploaded (20090415's). is there a way to filter? pls guide, thanks!

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 4/24/2009 12:48 PM Brett
Gravatar I hate posting in such an old topic but I didn't see any other relevant posts. Thanks for such a great blog.

In the CQWP, what are the options to display alternate content when the defined query/filtering returns no rows? I've added a custom column to several blog subsites called "Is Featured", so that content managers can choose which blog(s) to display in a CQWP on the home page. The blogs in question belong to several executives.

I'd like the CQWP to default to showing the CEO's most recent blog post, if the query asking for "Is Featured" doesn't return any items (ie, all of the blog entries are set to "no" for this column).

I'm new to SharePoint and keep running up against problems like this that would be 5 minute problem/solutions in the old days. I'm continually disturbed by the SP approach of using relatively complex XSLT to manipulate the list data, as opposed to the bad old days of simply modifying the query/stored procedure in the database PRIOR to the data being returned to the presentation layer. After hearing "separate business logic from the presentation layer" pounded into our heads for the last decade, it feels like SP is taking us purposefully in the opposite direction.

Sorry, I'll get off my soapbox now. Thanks for any suggestions on a solution.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 5/12/2009 11:37 AM Lisa Adcock
Gravatar AC - I've made some customizations to the OOB ContentQueryMain.xsl which I working. Now, I'm trying to put them into a custom ContentQueryMain.xsl. I've referenced the custom xsl in the .webpart file's MainXSLLink property and readded to the page. I get a 401 not authorized. I've posted this at the http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/b5bc97ca-1846-4dac-8e10-10b2e0ab529a forum but don't have any answers yet.

Then I followed the same process with a purely identical copy of the ContentQueryMain.xsl, renamed it and uploaded it to my Style Library. Updated the webpart file's MainXSLLink property and readded to the page. Same error occurs. I was hoping you could point me in the right direction.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 5/18/2009 12:47 PM Lisa Adcock
Gravatar AC - got the fix. It was a path problem. My url was something like www.sp.company.com/sites/myproj so I added "sites/myproj" in front of the Style Library reference and then it worked.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 6/7/2009 3:48 PM Larry Kuhn
Gravatar @Brett re: displaying alternate text if there are no items returned...

A simple way to handle that would be to modify the XSLT that is used to render the results so that if no items are returned that you conditionally output different text. The Search Best Bets web part does this, so you can review it's XSLT as an example.

HTH
- Larry

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 6/20/2009 3:56 PM Vikas
Gravatar Great article..
1. Can this be used only in a publishing site?
2. The same functionality can be achieved by the query string filter webpart. Any idea, why we need to do this, instead of using the out of the box webpart offered by MOSS 2007?

Thanks.
-Vikas

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 7/10/2009 12:57 PM Roberto
Gravatar Hi Andrew,
great post!

I'm triying to subclass the CQWP in order to put 2 buttons in it, and using their event handlers to filter the CQWP contents (using the techniques from your post). But putting the logic in the 2 button's event handlers, does not set the filter values! It only works if I put the filtering logic in the Load event handler (but in this way cannot reach my goal, since I need to filter by the pushed button). Do you have any idea?

Regards
Roberto

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 7/20/2009 9:47 AM JureS
Gravatar Hi

I had the same problem as Joaquín and James McDonnell.

For this to work you need to set FilterOperator1 and FilterType1.

Sample
this.Filter1ChainingOperator = FilterChainingOperator.Or;
this.FilterField1 = titleField.ToString();
this.FilterValue1 = "Pre";
this.FilterOperator1 = FilterFieldQueryOperator.BeginsWith;
this.FilterType1 = "Text";

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 7/31/2009 11:22 AM Chris
Gravatar This looks cool, but I am not a developer, so it confuses me. My question is can you build a CQWP to dynamically filter against anothe document library using a value from the imported user profile?

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 10/2/2009 5:53 PM Tracy Sterling
Gravatar What about utilizing a Data View/Data Form Web Part instead? It can be completely configured within SPDesigner (for those non-dev peepz), it's available in WSS 3.0 (CQWP's are not), and it includes properties for paging and sorting settings.

I've used them a couple times - Just a thought..

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 12/14/2009 1:06 AM Darryl Clark
Gravatar Hi Andrew,

great web part, however I would like to ask you a question about it. I can get it to work fine if I pass the title as a value to it, however if I use any other field, it throws the following error:

[ArgumentException: Value does not fall within the expected range.]
Microsoft.SharePoint.SPFieldCollection.GetFieldByDisplayName(String strDisplayName, Boolean bThrowException) +153
.........

It doesn't really make a lot of sense why it only works with the one field ( title ) ? Any ideas?

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 12/14/2009 5:16 AM AC [MVP MOSS]
Gravatar Darryl-
Make sure you're using the field's internal name.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 12/16/2009 6:33 PM Darryl Clark
Gravatar Hi Andrew,

I have done some more testing and it only seems to work on OOTB fields ( i.e Title, Order, etc ) Is there something I have to do to make it work with my created content type with create fields? I have tried using the internal names of the fields but this makes no difference.

thanks,

Darryl

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 12/17/2009 10:43 PM AC [MVP MOSS]
Gravatar Darryl-
Haven't seen this issue honestly... pretty sure I've done this without a problem. How about if you add it to the CommonViewFields?

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 12/20/2009 5:04 PM Darryl Clark
Gravatar Done that too but same result unfortunately. If I do the query manually with those fields it works fine, but try passing it by URL, it doesn't work.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 2/9/2010 8:45 AM Raymond
Gravatar Hi Andrew,

In your example the assembly which holds the ContentQueryExWebPart is deployed to the bin directory of a web application. In my case it's the same however I'm getting an exception (taken from a sharepint log): System.Xml.Xsl.XslTransformException: An error occurred during a call to extension function EnsureIsAllowedProtocol. It originates here: Microsoft.SharePoint.Publishing.WebControls.WebPartRunTime.EnsureIsAllowedProtocol. And is caused by something in the xsl file: <xsl:value-of select="cmswrt:EnsureIsAllowedProtocol($Url)"/>

When debugging the webpart, the code runs fine but the exception will ocure anyway. The webparts which holds our custom ContentByQyeryWebPart is added to the CodeAccessSucurity element in the manifest.xml.

In the webpart the method ModifyXsltArgumentList is overridden and a method is bound to ProcessDataDelegate

Every other webpart in that assembly runs fine btw.

Do you have any clue what could be missing? A particular kind of permission?

Hope you can u help.

Regards,
Raymond

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 3/9/2010 2:20 PM Al
Gravatar Very helpful post. Thanks for the tool.

I was able to install the WSP file and get this to work with Querystring.

Now, I want to make modifications to the webpart i.e. change the CommonViewFields and the ItemXslLink properties.

My question is that Will the Dynamic Filtering functionality work if I Export, edit and Imprort the webpart?

Thanks in advance,
Al

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 3/10/2010 3:15 PM Al
Gravatar Hi Andrew, very nice way to handle the dynamic filter which is lacking in OOTB CQWP.

In using the above solution, I am only able to gather results from the top level list even so I have checked my Source to be "all sites in the Site Collection" in the query source. It does not return results created from the subsites using the Content Type. Do you know why. Is there any scope setting need to be set before I package in the WSP file.

Also, Do you know if I export the webpart, modify the CommonViewFields and ItemStyle setting to use different XSL Stylesheet, would that work with querystring filtering.

Please help! Thanks.
Al

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 3/11/2010 7:59 AM AC [MVP SharePoint]
Gravatar Al-
Regarding the scoping question, I've never seen the CQWP not pull content from an entire site collection when you filter by a specific content type and everything is published. Something isn't quite right there & I'd check the publish and approval status as well as your filters.

Regarding the export question, sure those things should still work as long as the subclassed Web Part doesn't override those properties.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 3/11/2010 10:20 AM Himani
Gravatar Hi Andrew, I worked on the chapter11- widgetrollup project of your book. However I get the following error upon specifying the Itemstyle in webpart file. Here's what I am doing.\
1) created a subsite using the widgetcontentbuilder feature.
2) added a CQWP to this subsite. imported webpart definition.
3) changed the ItemStyle to TitleOnly.
4)changed the Itemxsl to <property name="ItemXslLink" type="string">/Style Library/XSL Style Sheets/ItemStyle.xsl</property>
5) I tried to check whether the webpart is able to reference the existing style AT ALL or not.
It seems that the moment I reference the itemstyle using above line, it throws error-

Unable to display this Web Part. To troubleshoot the problem, open this Web page in a Windows SharePoint Services-compatible HTML editor such as Microsoft Office SharePoint Designer. If the problem persists, contact your Web server administrator.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 3/11/2010 12:07 PM Al
Gravatar Thanks for your help. I was able to get to work and now it is failing at

Guid filterFieldID = Microsoft.SharePoint.SPContext.Current.Item.Fields[filterFieldName].Id;

During the debug, it throws "Value does not fall within the expected range." Its has to do with the field name I am passing in the querystring and I am using the internal name of the field in my querystring. Any ideas.

Thanks for your time.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 3/12/2010 7:05 AM AC [MVP SharePoint]
Gravatar @Himani - Usually there's an issue with your style sheet changes.
@Al - Impossible for me to debug this. Break it down and debug... you also don't need the .Fields part in there.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 3/16/2010 2:11 PM Sara
Gravatar Would you know why a CQWP shows the body that I added to its display for a user with contribute rights, and not for one with read-only rights?

Thanks so much!!

Sara

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 3/30/2010 12:16 PM Todd Thornhill
Gravatar Great Post Andrew! It seems like a solution to my ever-annoying problem, which I'd like your advice on. I'm trying to implement a provincial structure to my new site (I'm in Canada) and I'd like content on certain pages to change based on the province the user selects from a dropdown. This province should be global, and once they make a choice, other areas of the site will change when viewed. I was thinking on implementing this extended webpart as the 'heart' of this functionality, and maybe, if possible, hold a global variable for the province, instead of basing it off the query string. I'm not 100% familiar with SharePoint but do you think this sounds like a viable solution to you? Is there something in sharepoint that would better suit this type of logic (Audience targeting wouldn't work, as this is an internet site, not intranet). Thanks, oh SharePoint God, for your help in advance......

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 4/1/2010 3:34 PM AC [MVP MOSS]
Gravatar @Todd - This is something that would work possibly. There are filter Web Parts in SharePoint that other Web Parts connect to and consume the filtering selection. You could follow this same model.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 4/6/2010 4:36 PM Todd Thornhill
Gravatar Perfect Andrew! Thanks again for your much appreciated advice. I'd like this provincial value to be persisted across all pages on the site. I'd love this value to be held in a webpart to be in line with the SharePoint way of thinking, but I think going the global/session variable might be the only way, unless there's an easy solution for webparts to hold a value persisted across all pages (if I have to include this 'value' webpart on all page layouts, then so be it). I don't think there is though, and understand the downsides to why session variables are turned off OOTB, but I may have to use them...grrrr. Thanks soooooo much!

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 7/23/2010 2:11 AM Andreas
Gravatar Hi, I have the same experience as Daryl: it seems to work fine on the default fields, but I can't get the filtering to work on any of the custom fields. They are included in the CommonViewFields, they display in the Content Query Web Part, but the filtering always returns the error: "Value does not fall within the expected range".

It's a pity, this would be exactly what I need.

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 8/4/2010 11:19 PM Ishai Sagi
Gravatar For everyone experiencing the "Value does not fall within the expected range." error, it is because the code is assuming the filter field is part of the current context item's fields. The problems you are having is when connecting to a list that is not the current Page's list, and then, because the additional fields do not exist in the current pages list, the code crashes with the error mentioned.

To fix, you have to either get the field from the Web.Fields (if it is a site column) or from the list using WebUrl and ListName properties of the web part.
For example:
change the following line:

Guid filterFieldID = Microsoft.SharePoint.SPContext.Current.Item.Fields[filterFieldName].Id;

To either of the following:

Guid filterFieldID = Microsoft.SharePoint.SPContext.Current.Site.RootWeb.Fields[filterFieldName].Id;

or

Guid filterFieldID = Microsoft.SharePoint.SPContext.Current.Web.Fields[filterFieldName].Id;
depending on where you set up that column.

If the web part is connected to a specific list (is not an aggregation) and the column is not a site column (or it is - doesnt matter) you can use the following instead:
using (SPSite site = new SPSite(this.WebUrl))
{
using (SPWeb web = site.OpenWeb())
{
SPList targetList = web.lists[this.ListName];
Guid filterFieldID = targetList.Fields[filterFieldName].Id;
}
}

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 8/18/2010 12:26 PM Nagesh
Gravatar Hi Sagi,
I Have also same error with custom fields and i tried passing the filters as mentioned. but i failed in all the three ways. When i debug and get the filed refrences, for Title and Description only it is showing the values. For all other columns it is throwing the error ""Value does not fall within the expected range."

Is there any other way to filter custom fileds?

# re: Subclassing the Content Query Web Part: Adding Dynamic Filtering 8/24/2010 10:08 PM AC [MVP SharePoint]
Gravatar @Nagesh - Please see the abve post by Ishai.

Comments have been closed on this topic.

Copyright © 2003 - 2012 Andrew Connell
Creative Commons License 
This work is licensed under a Creative Commons License
Site design by Heather Solomon.

 
 
SharePoint 2010 Training
Looking for SharePoint 2010 training for developers, administrators, SharePoint Designer 2010 and end users? Look no further! My company, Critical Path Training offers the best SharePoint training around!