Second Life of a Hungarian SharePoint Geek

August 8, 2011

Template based or not? An issue with the description of the Person or Group field control

Filed under: SP 2010 — Tags: — Peter Holpar @ 22:23

Recently I had to make some customizations of the SharePoint field controls on a specific list form. For example, the field descriptions should have been displayed as hints on mouse-over instead of the standard location below the field entry control. It was not really hard to achieve this goal using custom rendering templates and a minimal jQuery script to hide empty tooltips.

The default rendering template for CompositeField is defined in the DefaultTemplates.ascx as follows:

  1. <SharePoint:RenderingTemplate id="CompositeField" runat="server">
  2.     <Template>
  3.         <td nowrap="true" valign="top" width="190px" class="ms-formlabel"><h3 class="ms-standardheader"><SharePoint:FieldLabel runat="server"/></h3></td>
  4.         <td valign="top" class="ms-formbody">
  5.         <!– FieldName="<SharePoint:FieldProperty PropertyName="Title" runat="server"/>"
  6.              FieldInternalName="<SharePoint:FieldProperty PropertyName="InternalName" runat="server"/>"
  7.              FieldType="SPField<SharePoint:FieldProperty PropertyName="Type" runat="server"/>"
  8.           –>
  9.             <SharePoint:FormField runat="server"/>
  10.             <SharePoint:FieldDescription runat="server"/>
  11.             <SharePoint:AppendOnlyHistory runat="server"/>
  12.         </td>
  13.     </Template>
  14. </SharePoint:RenderingTemplate>

To get the desired results, our view may use a custom form rendering template that contains a custom list field iterator template that refers to our custom composite field template. It may be defined like shown next.

  1. <SharePoint:RenderingTemplate ID="MyCompositeField" runat="server">
  2.   <Template>
  3.     <TD nowrap="true" valign="top" width="190px" class="ms-formlabel">
  4.       <H3 class="ms-standardheader">
  5.         <SharePoint:FieldLabel runat="server"/>
  6.       </H3>
  7.     </TD>
  8.     <TD valign="top" class="ms-formbody" width="400px">
  9.       <SharePoint:FormField runat="server"/>
  10.     </TD>
  11.     <TD>
  12.       <IMG src="/_layouts/images/help.png" title="<SharePoint:FieldDescription runat='server'/>" />
  13.     </TD>
  14.   </Template>
  15. </SharePoint:RenderingTemplate>

Well, that was easy and it works like a charm. But wait a minute! What happened with the Person or Group fields? They still display their description in the original way!

What is the reason for this behavior? We can get the answer as we check the source of FieldDescription and UserField classes (Microsoft.SharePoint assembly, Microsoft.SharePoint.WebControls namespace).

The getter of the Visible property of the FieldDescription looks like this:

get
{
    return (((base.ControlMode != SPControlMode.Display) && ((base.Field == null) || (!string.IsNullOrEmpty(base.Field.Description) && (base.Field.Type != SPFieldType.User)))) && base.Visible);
}

The above code makes the description control invisible if it is a Person or Group field. But how the description for this type of field is displayed then? The Render method of UserField answers the question. It contains the following lines:

string str = string.Format(CultureInfo.InvariantCulture, "\r\n<table class=’ms-usereditor’ cellspacing=’0′ cellpadding=’0′ border=’0′>\r\n\t<tr>\r\n\t\t<td width=’90%’>\r\n\t\t    <input type=’text’ class=’ms-input’ size=’55’ value='{0}’/>\r\n\t\t</td>\r\n\t\t<td>\r\n\t\t    <input type=’image’ name=’checkNames’ src=’/_layouts/images/checknames.png’ />\r\n\t\t</td>\r\n\t\t<td>\r\n\t\t    <input type=’image’ name=’browse’ id=’browse’ src=’/_layouts/images/addressbook.gif’ />\r\n\t\t</td>\r\n\t</tr>\r\n    <tr><td colspan=’3′ class=’ms-descriptiontext’>{1}</td></tr>\r\n</table>\r\n", new object[] { SPHttpUtility.HtmlEncode(builder.ToString()), SPHttpUtility.HtmlEncode(base.Field.Description) });
output.Write(str);

It means the displaying of the description of the Person or Group field is hard coded that is not a very nice solution.

If we need to customize that we can do that

  • Via inheritance and define our custom field with overriding the Render method of the parent class, and creating its own rendering template, but it means the field type should be deployed globally to the farm instead of customizing a single list edit form, and we will end up a new Person or Group field type that has really no additional value and might confuse the users.
  • Alternatively we can extend our jQuery script to hide the description at its default location and display it as a hint on our image HTML tag.
  • Or as a third option, we can replace the default UserField field control to a custom one from the code of our custom form before it is rendered.

But it would be much easier if the Person or Group field would have followed the standard rendering templates approach of SharePoint form components.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: