Saturday, March 10, 2012

Supercharged Content Query WebPart

For the last 2 weeks, I have been working on a very challenging assignment that another senior SharePoint architect stated that cannot be done. Here is a scenario; we want to dynamically pull the data from a list based upon a hierarchy structure that is tied to the user who has logged in to the system. The most obvious choice would be to use Content Query Web Part. The challenge here is that we are limited by 3 filters and what if we want to bring the data from Parent and its 8 children. This is not possible unless we start writing massive CAML.

Here is the solution I implemented for solving this problem. You create a webpart that inherits directly from CQWP and then in that class you can set your filters depending upon your requirements. Remember we cannot have more than 3 filters with CQWP even when we overwrite via VS. The key part of the solution is to map our hierarchy structure with a Term Store. The reason why this is a very cool idea is because in SP 2010 term store are 100% integrated with CQWP. When you define a column in the list of type managed metadata then you can use that as one of the filters in CQWP. When you use this column in CQWP, it gives you an option to include children as this is now supported in SP2010.

The other key aspect of this solution was to via code to set the filter based on the term that is associated with the current user. In the initialize of the inherited subclass, we will add code something like this, which will dynamically set the filter every time the webpart is executed.

this.FilterField1 = “Taxonomy Field Name”;

this.FilterOperator1 = ContentByQueryWebPart.FilterFieldQueryOperator.ContainsAny;

this.Filter1ChainingOperator = ContentByQueryWebPart.FilterChainingOperator.Or;

this.FilterType1 = "TaxonomyFieldTypeMulti";

this.FilterIncludeChildren1 = ShouldHaveDescedant();

this.FilterValue1 = ResolveFilterValue();

I don't want to get into the details about the power of CQWP on its own, but now when we combine CQWP with taxonomy we are going to produce a supercharged version of CQWP.