How to customize rendering of SharePoint list form fields - Part 1

While SharePoint offers great flexibility to users for quickly creating custom lists in various forms, flexibility of forms and fields rendered within is fairly limited on WSS/MOSS UI. In this series, I aim to share some of my findings and, perhaps, come up with wish-list for "SharePoint 2009"!

There are various types of form customization that we often need to make. For example:

  1. Show/Hide specific Fields on a form type.
  2. Show field in read-only mode on Edit form. Not allowing user to edit once field is populated while creating item.
  3. Allow dynamic behavior for a form regarding above, based on users credentials/rights.
  4. When a field is read-only, allowing user to override by clicking a link and letting him enter value.

1. Show/Hide a Field

When using content-types, a user can choose to show/hide a field from forms on content-type edit page. When creating custom solutions, or with list definitions, one can decide with following attributes in Field element to show/hide a form field in New, Edit, or Display form. Same can either be done declaratively in your element xml, or through object model updates to list/field schema. In case you are stuck in a environment where you do not have privilege of making custom deployments, and do have access to your site via web-services then you can use Lists.asmx service to send updates to an existing list schema. Following are 6 example options:

Field Element:

<Field   
    ...   
    ShowInDisplayForm = "TRUE" | "FALSE"   
    ShowInEditForm = "TRUE" | "FALSE"   
    ShowInNewForm = "TRUE" | "FALSE"   
    .../>

FieldRef Element:

<FieldRef   
    ... 
    ShowInDisplayForm="TRUE" | "FALSE"   
    ShowInEditForm="TRUE" | "FALSE"   
    ShowInNewForm="TRUE" | "FALSE"></FieldRef>

Object Model:

instance.ShowInEditForm = value;

Using SharePoint Designer:

You can copy existing form-pages and create new one. Remove existing form web-part and create a new ListForm web-part, with flexibility of showing/hiding fields of choice. Be mindful of not unghosting your pages, for the sake of long term hygiene of your site and forms. Details of steps can be browsed here, and here.

Client-side JavaScript in a Content Editor Web-Part (CEWP):

You can also include CEWP in your ghosted Form pages, without unghosting them, and add JavaScript that will hide controls on client-side based on your set configuration in web-part. Goodness about web-part approach is that it allows utilizing audience set to a web-part to show/hide varying controls per audience. Clever details can best be seen at Clever Workarounds.

Custom Rendering Template

Apart from above, there is a hidden gem in CONTROLTEMPLATES folder, DefaultTemplates.ascx. All of SharePoint forms, be it lists or libraries, are rendered based on RenderingTemplate's that reside in this folder. So if you open the file, you'll notice there is plethora of templates there. Good thing is that we do not need to replace the markup in system files, and they can instead be overridden by creating your own ASCX file and placing the same rendering template (with same ID) in yours. At execution time, your will be preferred and hence will override any system functionality. Having said that, we are interested in a RenderingTemplate named ListForm. This template is responsible for all your list forms (New/Edit/Display). Within the template you'll notice a control ListFieldIterator, which is responsible for iterating through all field at runtime and has a method in its class IsFieldExcluded() which we can use to decide which fields to show on which form at runtime. In the sample below, I'm showing replacement of system control with custom one, <eJugnoo:FormFieldIterator/>:

<%@ Register TagPrefix="eJugnoo" Assembly="eJugnoo.SharePoint, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b5eacb14abbc6805" namespace="eJugnoo.SharePoint.FormSettings" %>

<SharePoint:RenderingTemplate ID="ListForm" runat="server">
    <Template>
        <SPAN id='part1'>
            <SharePoint:InformationBar runat="server"/>
            <wssuc:ToolBar CssClass="ms-formtoolbar" id="toolBarTbltop" RightButtonSeparator="&nbsp;" runat="server">
                    <Template_RightButtons>
                        <SharePoint:NextPageButton runat="server"/>
                        <SharePoint:SaveButton runat="server"/>
                        <SharePoint:GoBackButton runat="server"/>
                    </Template_RightButtons>
            </wssuc:ToolBar>
            <SharePoint:FormToolBar runat="server"/>
            <TABLE class="ms-formtable" style="margin-top: 8px;" border=0 cellpadding=0 cellspacing=0 width=100%&gt;
            &lt;SharePoint:ChangeContentType runat="server"/>
            <SharePoint:FolderFormFields runat="server"/>
            <eJugnoo:FormFieldIterator runat="server"/>
            <SharePoint:ApprovalStatus runat="server"/>
            <SharePoint:FormComponent TemplateName="AttachmentRows" runat="server"/>
            </TABLE>
            <table cellpadding=0 cellspacing=0 width=100%&gt;&lt;tr><td class="ms-formline"><IMG SRC="/_layouts/images/blank.gif" width=1 height=1 alt=""></td></tr></table>
            <TABLE cellpadding=0 cellspacing=0 width=100% style="padding-top: 7px"><tr><td width=100%&gt;
            &lt;SharePoint:ItemHiddenVersion runat="server"/>
            <SharePoint:ParentInformationField runat="server"/>
            <SharePoint:InitContentType runat="server"/>
            <wssuc:ToolBar CssClass="ms-formtoolbar" id="toolBarTbl" RightButtonSeparator="&nbsp;" runat="server">
                    <Template_Buttons>
                        <SharePoint:CreatedModifiedInfo runat="server"/>
                    </Template_Buttons>
                    <Template_RightButtons>
                        <SharePoint:SaveButton runat="server"/>
                        <SharePoint:GoBackButton runat="server"/>
                    </Template_RightButtons>
            </wssuc:ToolBar>
            </td></tr></TABLE>
        </SPAN>
        <SharePoint:AttachmentUpload runat="server"/>
    </Template>
</SharePoint:RenderingTemplate>

You can use above in your custom Feature, apply custom logic to decide which field to show/hide. Above can also be part of a generic solution that is available for all lists, by providing a configuration page (_layouts) and custom actions link on list settings.

Hope above gives you a good idea about various options available to meet form customization needs. Sometime soon, I'm going to share sample code on codeplex that simply provides users a UI to configure form visibility for any list instead of depending on developers each time they need a form customization.

Watch this space for updates...

-- Sharad

UPDATE (July 29, 2008): I have published a follow-up post to my preferred server-side approach to Form Customizations.
http://weblogs.asp.net/sharadkumar/archive/2008/07/28/how-to-customize-sharepoint-forms-part-2.aspx

Published Monday, July 07, 2008 12:00 AM by eJugnoo

Comments

# SharePoint Forms - Improve layout of Field's 'description' to save screen-space and enhance UX

Monday, July 07, 2008 1:01 AM by Sharad Kumar

Default Forms in SharePoint leave you with limited choice sometimes when it comes to customization. One

# re: How to customize rendering of SharePoint list form fields - Part 1

Monday, July 14, 2008 1:58 PM by tom

with the third solution (Custom Rendering Template), once you've deployed the overriding ascx file, where do you go to see the changes you mentioned?  (you mentioned..."You can use above in your custom Feature, apply custom logic to decide which field to show/hide"  what does this mean? could you provide an example?)

thanks,

t

# re: How to customize rendering of SharePoint list form fields - Part 1

Monday, July 14, 2008 2:35 PM by eJugnoo

Tom, all you have to do is:

Create a Feature that includes your ASCX, an app-page with code-behind for capturing which fields to hide/show. Save the information in PropertyBag of root-folder of current list. Use this propertybag values for rendering fields at runtime with a custom class, FormFieldIterator, that inherits from Microsoft.SharePoint.Webcontrols.ListFieldIterator. Override the implementation of member IsFieldExcluded() in your calls based on values saved in PropertyBag for current list reg: which field to show/hide. Not you'll have to implement a switch for 3 ControlMode's, New/Edit/Display, so that you can implement variance of show/hide based on which form you are currently rendering.

Does that makes sense?

-- Sharad

# SharePoint Kaffeetasse #85

Tuesday, July 15, 2008 3:22 AM by Mirrored Blogs

Form Based Authentification Configuring Forms Authentication in SharePoint 2007 Customizing the Login

# re: How to customize rendering of SharePoint list form fields - Part 1

Tuesday, July 22, 2008 8:02 AM by Pierre Joubert

I would like to hide fields based on the refering view, is this possible?

# How to Customize SharePoint Forms - Part 2

Tuesday, July 29, 2008 1:03 AM by Sharad Kumar

In my previous post , I discussed various options available with developers/power-users to modify out

# re: How to customize rendering of SharePoint list form fields - Part 1

Wednesday, July 30, 2008 11:50 AM by eJugnoo

Pierre,

Checkout my follow-up post on better server-side possibilities with Forms. You can download the bits and use them right away.

Regarding views-based conditional Forms - Yes, you can achieve that by saving such conditional config in lists property-bag. Enhancing UI to input such settings, as in my post. Then on the fly setting values in IsFieldIncluded() for each field you want to show/hide.

In fact, I'm considering enhancing the implementation to support conditional forms relative to values in same item. Not sure, if Views-based conditions is something I'd want to do though.

Perhaps, if you'd give use-case example - I'd be able to relate to more generic usage.

Thanks.

-- Sharad

# My favorite links from the last week - 1st Week of August 2008

Sunday, August 03, 2008 7:13 AM by YESChandana -Blog

My favorite links from the last week - 1st Week of August 2008

# re: How to customize rendering of SharePoint list form fields - Part 1

Monday, November 17, 2008 2:50 PM by Antoine

Hello

I have installed the web application feature you created to manage forms and it is a great feature. i am having some issues with one list that does not use your feature. For some reasons, the field description will not display for this list or any other lists on my site. Would you have any idea why?

thanks

# re: How to customize rendering of SharePoint list form fields - Part 1

Monday, December 15, 2008 4:58 PM by Sara*beth

is it possible to display (or hide) one field in the list based on users' response to earlier fields in the same list? i.e. have a dynamic list? I am using MOSS and SharePoint Designer.

# re: How to customize rendering of SharePoint list form fields - Part 1

Wednesday, December 17, 2008 1:20 PM by Ron

Hi

I am interested in hiding a field in the list in edit mode based on what the user input to another field during creation.  Is this possible without using SharePoint designer?

Example:

During lList item creation:

Field 1 = false

Field 2 = "hello"

During editing, I want to show Field 2 only when Field 1 was set to true.

Thanks

# Resource Links &laquo; sharepoinTony

Saturday, September 12, 2009 8:22 PM by Resource Links « sharepoinTony

Pingback from  Resource Links &laquo; sharepoinTony

# re: How to customize rendering of SharePoint list form fields - Part 1

Friday, September 18, 2009 5:41 PM by bazztrap

This windows application uses Web services to hide fields

spiralout.codeplex.com/.../View.aspx

# re: How to customize rendering of SharePoint list form fields - Part 1

Monday, October 12, 2009 6:14 AM by sarangasl

Try this too,

sarangasl.blogspot.com/.../hide-column-in-edit-new-or-display-mode.html

# re: How to customize rendering of SharePoint list form fields - Part 1

Tuesday, November 03, 2009 1:07 PM by mary

If I need to delete the left column (Title, Body, Expires text) of a display form (dispform.aspx) for a news item, is this done by overriding the formFieldIterator?

Thanks in advance.

# How to Customize SharePoint 2007 Forms - Part 3 (Field Validations)

Tuesday, November 03, 2009 8:37 PM by Sharad Kumar

In continuation of previous series of posts, for exploring options to customize SharePoint 2007 list

Leave a Comment

(required) 
(required) 
(optional)
(required)