Understanding SharePoint’s ddwrtDataBind

SharePoint list form has 2 types:
1-    XSL from.
2-    Web Part Form.
In XSL form i found myself looking at the XSLT for a DataFormWebPart wondering how to decipher the auto-generated _designer:bind attribute.
<SharePoint:FormField runat="server" id="ff2{$Pos}" ControlMode="Edit"
                                      FieldName="Color"
                                      __designer:bind="
                                                 {ddwrt:DataBind(
                                                       'u',
                                                       concat('ff2',$Pos),
                                                       'Value',
                                                       'ValueChanged',
                                                       'ID',
                                                       ddwrt:EscapeDelims(string(@ID)),
                                                       '@Color')}"/>


In Microsoft.SharePoint dll there is internal class, Microsoft.SharePoint.WebPartPages.DataFormDdwRuntime. The DataFormDdwRuntime.DataBind method returns an empty string, so it’s likely that the "__designer:bind" attribute is just a dummy attribute for SharePoint Designer, if at all.  Remember that curly braces inside an attribute is an XSLT expression. The real magic is that what the DataBind method actually does.
Turns out that the DataBind method is basically a wrapper to the DataFormWebPart method, AddDataBinding.  This method has the following signature: 
public override void AddDataBinding(string op,
                                                          string controlId,
                                                          string propertyName,
                                                          string eventName,
                                                          string keyField,
                                                          string keyValue,
                                                          string dataField
                                                          )

Operation Type ("op")
This is the type of operation that will be used when the form is submitted. There are three possible values:  "i” (Insert), “u” (Update") and “d” (Delete).
Bound Control ("controlId")
This the id of the control that will be bound to the SharePoint list item.  The auto-generated value attempts to duplicate the value of the “id” attribute using an xslt function concat.  You can use your own format, but the chosen format for the id’s are “ff + field index + record index” where the field index is an arbitrary number, usually the order in which the fields appear in the designer (1,2,3, etc), and the keyword $Pos is an XSLT variable defined earlier in scope, referring to the position() of the row in the result set.
Bound Control Property ("propertyName")
This is the property on our bound control that will ultimately be used to represent the SharePoint field value.  For SharePoint FormField controls, this property is "Value".  Most ASP.NET controls can be used, assuming that the propertyName and eventName are properly set.
Bound Control Event ("eventName")
This the event on the bound control that you want to be handled when the form posts information back to the server.  How this works is really interesting: it uses reflection to get the Event of your control, then binds the postback event to the DataFormWebPart's HandleChangedField method.  This method essentially fetches the value of your control (via the PropertyName) and then tracks that the field has changed or needs to be inserted.
For SharePoint FormField controls, this event is "ValueChanged".
Data Source Primary Key ("keyField")
Based on examples, the "key field" refers to the attribute in the dsQueryResponse XML which represents the primary key for this record.  This is likely always going to be "ID".  (ie, 
/dsQueryResponse/Rows/Row/@ID)
Data Source Primary Key Value ("keyValue")
The value of the primary key, “ID”.  The ddwrt:EscapeDelims method is used to convert certain characters (,=;'{}") to a SharePoint unicode string equivalent (ie, "," is converted to "_x002C_").  For SharePoint Designer XSLT, this is likely always going to be ddwrt:EscapeDelims(@ID). 
Data Source Field Name ("dataField")
This is the name of the SharePoint field that we want to bind.  Since we’re pulling this value from the dsQueryResponse XML, it’s represented as it appears in the dsQueryResponse XML.

Demo:

<asp:TextBox runat="server" id="TextBox1{$Pos}" Text="{@Color}"
             Value="{@Color }" __designer:bind="{ddwrt:DataBind(
                                                                'u',
                                                                concat('TextBox1', $Pos),
                                                                'Text',
                                                                'TextChanged',
                                                                'ID',
                                                                ddwrt:EscapeDelims(@ID),
                                                                '@Color)}" />

Post a Comment