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.