DataFormWebPart Parameters and ParameterBindings
Since my post on Easily Making the DataFormWebPart Reuseable, I’ve had a couple questions on the parameters and how to make use of them. I’m going to attempt to document some of the more useful parameters, but hopefully provide a framework for you to be able to figure out how to do anything you may need with the DFWP.
The first thing you need inside your DFWP is a data source. This goes inside the <DataSources> node in the DFWP markup. You’ll notice I put some “DataFormParameters” inside the <SelectParameters> node.
<SharePoint:SPDataSource runat="server"
DataSourceMode="List"
UseInternalName="true"
selectcommand="<View><Query><Where><Eq><FieldRef Name='Meal'/> <Value Type='Choice'>{MealQs}</Value></Eq></Where></Query></View>"
id="Cafeteria_x0020_Menu1"><SelectParameters> <WebPartPages:DataFormParameter
Name="ListName"
ParameterKey="ListName"
PropertyName="ParameterValues"
DefaultValue="Cafeteria Menu"/> <WebPartPages:dataformparameter
runat="server"
Name="Meal"
ParameterKey="Meal"
PropertyName="ParameterValues"
DefaultValue="Breakfast"/> <WebPartPages:dataformparameter
runat="server"
Name="MealQs"
ParameterKey="MealQs"
PropertyName="ParameterValues"
DefaultValue="Breakfast"/>
</SelectParameters>
</SharePoint:SPDataSource>
In this example I’m using ListName, this allows me to reference the list by name instead of GUID, making this solution re-useable on other SharePoint sites since every time a list is created it get’s a new GUID.
I’ve also added two parameters to pickup the meal I’m trying to display. One of these parameters comes from a drop down list on the page, the other comes from a query string variable. More on this later.
If you want to limit the number of rows in a query you can include the “MaximumRows” parameter in your <SelectParameters> section.
<asp:Parameter Name="MaximumRows" DefaultValue="500"/>
By default, the DFWP will query the current sub site. If you are looking to query items from a list in another sub site you can include the “WebUrl” parameter. Possible options here are “{sitecollectionroot}” or the path to the actual web “/services/cafeteria”.
<asp:Parameter Name="WebUrl" DefaultValue="{sitecollectionroot}"/>
Next is the <ParameterBindings> section. This is where you can bind variables in your query to controls or the querystring.
<ParameterBinding Name="UserID" Location="CAMLVariable" DefaultValue="CurrentUserName"/>
<ParameterBinding Name="Today" Location="CAMLVariable" DefaultValue="CurrentDate"/> <ParameterBinding Name="Meal" Location="Control(ddlMeal)" DefaultValue="Breakfast"/> <ParameterBinding Name="MealQs" Location="QueryString(m)" DefaultValue="Breakfast"/>
“UserID” and “Today” are two variables inserted automatically by SharePoint. I’m not sure what else is available here, if anyone has any more info on that I’d love to know.
“Meal” and “MealQs” are two that I created to bind to the query above. You’ll notice in the “selectcommand” that I’m referencing “{MealQs}”. Here is where I declare that “MealQs” is a value from the querystring variable m. So adding “?m=breakfast” onto the end of my URL will give me only breakfast items.
“Meal” does the same thing as MealQs except it is bound to a drop down list I created called “ddlMeal”.
In summary, to bind to a control the syntax is “Control(id of the control)”. To bind to a querystring variable the syntax is “QueryString(variable name)”. Once you do this step you are free to use these in your “selectcommand” in your SPDataSource.
Hope that was helpful. If there are any other topics or questions you want to know about concerning the DataFormWebPart let me know, if there is interest in something I’ll write up a post about it.
Comments
Anonymous
April 06, 2009
PingBack from http://www.anith.com/?p=26960Anonymous
May 20, 2009
Hi, Thanks for the article, i have to pass the complete CAML query as parameter to selectcommand. But lot of inconsistencies are there. Is there a best way to pass the entire CAML query as paramter to DataFormwebpart SatheeshAnonymous
June 10, 2009
I hope someone reads this. This is the closest I've come to an answer to my dilemma. I want to use precisely that parameter so I can fill the username in a list created by a workflow. In other words, user clicks on form action button in the DFWP, that kicks off a custom workflow that puts the LogonUser in a field of a custom list. Please help!Anonymous
June 24, 2009
Can we have Form element bound to the parameter <ParameterBinding Name="region" Location="Form(Area)" DefaultValue=""/> The above is not workin and works only in the ase of default value. Area is an input type hidden field. Can any one help!!!Anonymous
October 19, 2009
Hi, I have two DataFormWebParts on my page which should display content from two different lists. Both with "ListName" as described. The problem is now that the second DataFormWebPart shows the same conetnt as the first one. What I'm missing? Do I have to set a special ID? Thanks for any help. Regards, MartinAnonymous
December 08, 2010
This does not work. Not sure what Location=Control() is supposed to do, but it does not work. I and other people have tried and it does not read the page controls at all. We have tried this and every other example seen on other sites, but it has never worked.Anonymous
January 30, 2011
declarative code in SPD trying to mimic C#, never works.Anonymous
March 03, 2011
Hi Can this be done for aggregated data sources. I have 4 Lists from which i pull data. If I put list ID's than everything works fine, but if I put list names instead, I get 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." I checked sharepoint's logs and found entry Error while executing web part: System.ArgumentNullException: Value cannot be null. Parameter name: expression at System.Web.UI.DataBinder.Eval(Object container, String expression) at Microsoft.SharePoint.WebPartPages.DataFormParameter.Evaluate(HttpContext context, Control control) at System.Web.UI.WebControls.Parameter.UpdateValue(HttpContext context, Control control) at System.Web.UI.WebControls.ParameterCollection.UpdateValues(HttpContext context, Control control) at System.Web.UI.WebControls.ParameterCollection.GetValues(HttpContext context, Control control) at Microsoft.SharePoint.WebControls.SPDataSourceView.CreateParameterDictionary(ParameterCollection parameters) at Microsoft.SharePoint.WebControls.SPDataSourceView.get_SelectParametersDictionary() at Microsoft.SharePoint.WebControls.SPDataSourceView.ExecuteSelect(DataSourceSelectArguments selectArguments) at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) at Microsoft.SharePoint.WebControls.SingleDataSource.GetXPathNavigatorInternal() at Microsoft.SharePoint.WebControls.SingleDataSource.GetXPathNavigator() at Microsoft.SharePoint.WebControls.SyncMergedDataSource.GetXPathNavigator() at Microsoft.SharePoint.WebControls.AggregateDataSourceView.get_AggregateNavigator() at Microsoft.SharePoint.WebControls.SingleDataSource.GetXPathNavigator() at Microsoft.SharePoint.WebControls.SingleDataSource.GetXPathNavigator(IDataSource datasource, Boolean originalData) at Microsoft.SharePoint.WebPartPages.DataFormWebPart.GetXPathNavigator(String viewPath) at Microsoft.SharePoint.WebPartPages.DataFormWebPart.PrepareAndPerformTransform() Do you have any idea what might be a problem?