Tuesday, May 29, 2007

Using Parameters in the SelectCommand of a Data View Web Part

The SelectCommand attribute of the SPDataSource element within a SharePoint DataView web part can use parameters supplied to the web part - and these parameters can come from a varity of sources, including the querystring in the page request.

This opens the way to easily control the data displayed in the data view; for example, via an ID supplied in the querystring.

The following code snippet shows some of the DataFormWebPart elements that use a value in the querystring to select a single list item to be displayed (the parameter is named SelectedRisk). Notice how the parameter is referenced in the SelectCommand:


<SharePoint:SPDataSource runat="server" SelectCommand="<View><Query><Where><Eq><FieldRef Name="ID"/><Value Type="Counter">{SelectedRisk}</Value></Eq></Where></Query></View>" ...>


<WebPartPages:DataFormParameter PropertyName="ParameterValues" ParameterKey="SelectedRisk" DefaultValue="0" Name="SelectedRisk"></WebPartPages:DataFormParameter>





<ParameterBinding Name="SelectedRisk" Location="QueryString(ID)" DefaultValue="0"/>



newis said...

Help pls:
[WebPartPages:DataFormParameter PropertyName="UserId1" ParameterKey="UserId1" Name="UserId1" /]
[ParameterBinding Name="UserId1" Location="ServerVariable(LOGON_USER)" DefaultValue="0"/]

I got next:
Error while executing web part: System.Web.HttpException: DataBinding: 'Microsoft.SharePoint.WebPartPages.DataFormWebPart' does not contain a property with the name 'UserId1'

Alessandro said...

hi, I've the same error: DataBinding: 'Microsoft.SharePoint.WebPartPages.WebPartPage' does not contain a property with the name 'ParameterValues'.
could you help me?
thank you in advance


whats.to.learn.today said...

Hi Alessandro,

I think you may have the values in your custom DataFormParameter wrong.

Every custom WebPartPages:DataFormParameter that you add requires the PropertyName attribute set to "ParameterValues". This is then used when parsing the values.

Set the ParameterKey and Name attributes to the name of your new parameter - the name of "SelectedRisk" in my example demonstrates the values you need to change in the SPDataSource, the DataFormParameter and the ParameterBinding elements.

Hope that helps

Leon said...

What about using parameters to filter. I can't seem to "unfilter" a dataview using a form control without a page refresh.

jack said...

Really interesting post. I realise this is a relatively old post but...

I have a web part that I'd like to dynamically change the part of my selectcommand based on the querystring of the page's URL.
If my querystring variable, say 'X', equals 1, I want the sort order to be ColumnX, ColumnY. If X equals some other value, I want the sort order to be just ColumnY.

1. Is it possible to parameterise the entire , so can be parameterised as, say, {myOrderBy} ?
2. Is it possible to conditionally test the querystring and to determine what to store in the parameter

..or perhaps there's an alternative way to solve my problem.

whats.to.learn.today said...

Hi Jack,

Do you need you need to use a data view, or is there any chance that a list view might do? Reason I ask is that the standard list view gives you the option to filter by fields in the querystring. Here's an example of the querystring derived by selecting to filter "TrustStatus" by the value "Active" and "Office" by "Auckland":


Maybe you could build this type of URL to achieve your aim?

jack said...

Thanks for the quick response and sorry for failing to check that my post rendered properly.

I have a page that I've inherited which contains this DataFormWebPart and works for 90% of the content to be rendered. However, the new content needs to be rendered without the first sort field (there are 4 subsequent sort fields).

So this is my problem area. How do I alter the selectcommand (the Order By bit) based on the URL (which already contains a parameter which I can use to identify between the two types of content)?

Is it possible to evaluate the content of the querystring, before the substitution into the selectcommand takes place, like:
<ParameterBinding Name="mySortOrder" Location="EVALUATEIF(QueryString(ID)=='3','<Order By><FieldRef Name="F2" Ascending="TRUE"/><FieldRef Name="F3" Ascending="TRUE"/></Order By>"/>','')" DefaultValue="<Order By><FieldRef Name="F1" Ascending="TRUE"/><FieldRef Name="F2" Ascending="TRUE"/><FieldRef Name="F3" Ascending="TRUE"/></Order By>"/>

And is it actually possible to substitute this much (i.e. from the start to the end Order By tags) into the select command?

Once again, thanks.