Adjusting the DisplaySize of your Fields

Yesterday I mentioned I would post an entry on YAUSF (Yet-Another-Undocumented-SharePoint-Feature) however I think this one will be around to stay.

When you create a list you can specify the maximum length on Text fields. This is great and will provide a warning message to the user if they enter text that exceeds this. However it doesn't (always) adjust the length displayed on the screen so most SharePoint lists look like this:

Of course with a simple tweak in SCHEMA.XML you can have your SharePoint lists looking like this:

The list for the first image is your typical field definition for the columns. Here's the snippet from SCHEMA.XML:

<Fields>
   <Field Name="Title" ReadOnly="TRUE" Required="FALSE" Hidden="TRUE"/>
   <Field Name="Name" DisplayName="Name" Type="Text" />
   <Field Name="Address" DisplayName="Address" Type="Text" />
   <Field Name="City" DisplayName="City" Type="Text" />
   <Field Name="Country" DisplayName="Country" Type="Text" />
</Fields>

In SharePoint fields, there's a property called DisplaySize. Yeah, looking through the SDK won't find it in the Field element in SCHEMA.XML. In fact, you don't find it in any SCHEMA.XML file anywhere (which might be why it's not in the SDK, unsupported perhaps?). You will find it however as a Property on the SPField class. This might lead you to think that you have to write a custom program to adjust the display length of all your fields if you want to give your users something more. Not so.

While I was digging around in FLDTYPES.XML yesterday and screwing around with the RenderPattern there was another thing that stood out. DisplaySize was a property being checked and if it was there, it was used in the creation of the HTML tags outputting in the browser. So with a little tweak to SCHEMA.XML I added DisplaySize to a few fields like so:

<Fields>
   <Field Name="Title" ReadOnly="TRUE" Required="FALSE" Hidden="TRUE"/>
   <Field Name="Name" DisplayName="Name" Type="Text" DisplaySize="30" />
   <Field Name="Address" DisplayName="Address" Type="Text" />
   <Field Name="City" DisplayName="City" Type="Text" DisplaySize="20" />
   <Field Name="Country" DisplayName="Country" Type="Text" DisplaySize="15" />
</Fields>

Now you get the much improved version of your list for your users to add and edit items to. This made sense as the property would alter the length of the field in the UI when it gets built in OWS.JS. Through the regular SharePoint interface you can specify the length of your fields. This will prevent the user from entering more than the number of characters here in the field but does it adjust the UI? It depends.

If you create the fields as show in the second image with the field lengths indicated, you'll get the display you see on the screen above. All done through the UI. If you don't specify a length or you change a field so it is longer than 31 characters, it will stretch the UI display out to 50 characters wide (like the Address field above). 50 characters seems to be the max width and while you can create fields that are 255 long, they'll only ever display 50 characters wide. Through the UI. If however you edit your SCHEMA.XML for the list like above and set the DisplaySize tag to whatever you want, it will appear correctly not matter what the value is.

So if you've been paying attention:

  • Creating columns through the UI only respect the length if it is less than 32 characters
  • Columns longer than 31 characters through the UI will always fill out to 50 characters wide in the Browser, no matter what the length
  • Specifying DisplaySize in SCHEMA.XML for a field will display correctly in the Browser for any length

Anyways, mildly bizzare and slightly odd. Differences between what you can do in site definition files vs. what (normally) your users can do isn't always the same.

2 Comments

Comments have been disabled for this content.