Pages

Wednesday, October 20, 2010

Pull Unique Values from a List in DataView WebPart

I was recently asked to develop a WebPart which enables searching in a List. This doesn’t sound like a big deal as we can always use the DataView WebPart which takes Query String Parameters as Filter Values to display the search Results.

I am nowhere concerned with the Search Results WebPart as we have multiple ways to implement it. My interest is on the Search Page than anything else. In order to search we should have a Page where the users can select their Parameters and these Parameters should be the Metadata available in the List. Doesn’t sound Complicated? Okay! how about this, When ever a new Record is added to the List an Additional Checkbox with the New Metadata Values should be displayed in the search Page as a parameter. Needless to mention that all the Search Field values present in the Page should be Unique.

We have few ddwrt functions which can help our cause. One such function is “ddwrt:NameChaged” but we have a problem with this function as it always compares with the previous value. Let me illustrate this: Consider you have the data for a Column as below:

Value1, Value1, Value2, Value2, Value2, Value3

In this case you will get the data displayed as Value1,Value2,Value3 which is expected. But, if had the as below then we run into problems:

Value1,Value2,Value1,Value3,Value2 – Here the result would be Value1,Value2,Value1,Value3,Value2 instead of Value1,Value2,Value3.

So for obvious reasons this function is ruled out as we cannot expect the data to be in a specified format and the comparison is always with just the previous value and not all the previous values as a whole.

After a bit more digging into google and other sources found another function which is:

not(@Column1=preceding-sibling::Row/@Column1)

Use this function anywhere you wish to get the Distinct Values. we can use this in the Template where we have the Rows variable defined



The above setting works good for a Drop down or search (based on Just one parameter) . But as explained earlier if we wish to have multiple columns with Checkboxes to select multiple values either from the same column or different columns we need to place this at Individual Row level that is an If condition has to be in place, just before displaying the Data.



Similarly repeat this for different columns but do remember to change it to proper column name to get expected results.