If you would like to discuss anything, please contact me via email: Howard.Bayliss@sequence.co.uk

Thursday, October 27, 2011

DataFormWebPart maximum field lengths

I used a DataFormWebPart to generate a custom edit form. Our client wanted to limit the maximum length of various fields on this form, but didn't want these to be applied site-wide.

The built-in SharePoint fields (FormField, TextField, NoteField, etc) don't have a "maxlength" property.

The solution was to modify the template for the various fields within the DataFormWebPart XSLT, to include RegularExpressionValidators.

Credit to Bart McDonough for this. His blog post relates to creating a new field type, but I tried it within the DataFormWebPart and it worked.

The nice thing about this is that the validation will take place on both the client and the server

Here is a sample from my DataFormWebPart XSLT file. This sets the maximum length of a text field to 80 characters.

Note that the curly braces in the regular expression have to be escaped. If you don't do this, the XSLT will break.



<SharePoint:TextField runat="server" id="ff1{$Pos}" ControlMode="Edit" FieldName="Title" __designer:bind="{ddwrt:DataBind('u',concat('ff1',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Title')}">

<Template>

<asp:TextBox id="TextField" runat="server"/>
<asp:RegularExpressionValidator
ControlToValidate="TextField"
ValidationExpression=".{{0,80}}"
Text="Value cannot exceed 80 characters."
runat="server" />

</Template>

</SharePoint:TextField>

No comments:

Post a Comment