Max length of a multi-line text (note) field on a SharePoint publishing page

Sometimes it’s the seemingly easy things in SharePoint that turn out to be a little more complex than you expect.

Recently I was creating a publishing page layout (and associated content type) for a client. They wanted a “Short Description” field added to the page and wanted it to allow up to 500 characters. If you’ve ever worked with multi-line text fields (fields of type SPFieldMultiLineText) in SharePoint, you know they only allow up to 255 characters by default when used in a document library (like the ‘Pages’ library). Typing more than 255 characters will yield an error.

I should also mention it’s not really the field itself that has this limitation. It’s actually the NoteField control that SharePoint uses to represent multi-line text fields in the UI. The Validate() method of that control checks whether the field is being used in a document library, and if so, flags an error if it has more than 255 characters. The ONLY exception is if the UnlimitedLengthInDocumentLibrary property of the field is True. But setting that to True has its own drawback: there’s no validation of the text length at all. The SPFieldMultiLineText class doesn’t have a ‘MaxLength’ property like its single-line cousin.

So this built-in behavior leaves us with one extreme or the other. Either we have a 255 character limit, or we have no limit at all. Not exactly flexible.

[ Quick Tangent: The reason for the 255 character limit is the Document Information Panel in Microsoft Office. The OLE provider it uses to stream properties from SharePoint only supports up to 255 characters for text fields. And yes, that still applies even in Office 2010. ]

So how did I solve this? Well, I did two things. First, in the <field> tag for my multi-line text field in my feature, I set the ‘UnlimitedLengthInDocumentLibrary’ attribute to TRUE. Second, to set the 500 character limit in my publishing page layout, I changed the rendering template for the NoteField as follows:

<SharePointWebControls:NoteField
    FieldName="{InternalFieldName}"
    InputFieldLabel="Short Description"
    runat="server">

   <!-- Override the rendering template for the field -->
   <Template>
        <asp:TextBox id="TextField" TextMode="MultiLine" runat="server"/><br />
        <asp:RegularExpressionValidator
            ControlToValidate="TextField"
            ValidationExpression="(?:[\r\n]*.[\r\n]*){0,500}"
            Text="Value cannot exceed 500 characters."
            runat="server" />
   </Template>

</SharePointWebControls:NoteField>

I wanted the field template to look exactly like the original but contain my text length validation. To get the original template, I looked in the DefaultTemplates.ascx file under {SharePointRoot}\TEMPLATE\CONTROLTEMPLATES. I did a search on “NoteField,” copied its rendering template (simple textbox), and then added a standard ASP.NET regular expression validator.

If you’re wondering about the validation expression itself, it basically says “allow up to 500 characters, where each character can have zero or more newline sequences on either side of it.” If I hadn’t taken the newlines into account, then the ‘\r’ and ‘\n’ characters would count against the character limit each time they appear, and I didn’t want that.

Lastly, I should mention I was looking for a “no-code solution” here.  Another option I could’ve used was to create a new class deriving from Microsoft.SharePoint.WebControls.NoteField and override the Validate() method with custom validation logic.

About these ads

9 thoughts on “Max length of a multi-line text (note) field on a SharePoint publishing page

  1. there is a drawback in this way as we can’t use this note field again in this page that will validate it self again although u didn’t override it

    • Actually that should not be a problem. ASP.NET scopes validator controls to their current naming container, which in this case is the “template” tag in the NoteField control. The validator doesn’t affect anything else outside of that tag. If you use another NoteField control somewhere else on the page and don’t override its rendering template, then it will work just like it normally does (with a 255 character limit or none at all). I tested it just to be sure and verified that’s how it works.

  2. Dear Bart,

    I´ve tried your solution, but the control still validates the original 255 characters…
    I do get a validation error trying to enter more than 500 characters, but I still get the original validation error when saving above 255 characters. Any ideas?

    • Verify that the “UnlimitedLengthInDocumentLibrary” property of the underlying field is set to ‘True.’ By default that property is ‘False’, and that’s what triggers SharePoint to enforce the 255 character limit when saving data. The UnlimitedLengthInDocumentLibrary property can be set through code or through the UI.

  3. Hi Bart,

    Thanks for the advice. Works perfectly now! Great no-code solution.

    BTW: I guess you meant: setting the “UnlimitedLengthInDocumentLibrary” property to True (instead of False…).

    Anyway, thanks for the article and the reply.

    Regards,
    Ernst

  4. This is the solution I am looking for! I apologize for the newbie question here. but, how do I get to the SharePointWebControls:NoteField and edit it? do I access it through SharePoint designer? Thanks in advance.

    • Ron — Yes, SharePoint Designer should work. In my case I used Visual Studio (since I was deploying a custom feature), but I mocked up the page layout first in SharePoint Designer to test everything.

Comments are closed.