Becky Bertram's Blog
Perspectives from a SharePoint developer




This blog has moved. You can find the new blog on Savvy Technical Solution's Web site, at www.savtechsol.com. You will be redirected to the following page in 10 seconds:

Becky Bertram's Blog > Posts > Creating Custom List Forms for Creating, Editing, or Displaying a List Item
August 29
Creating Custom List Forms for Creating, Editing, or Displaying a List Item
You might have noticed that when you create a new list or library in SharePoint, a set of forms get automatically created for creating a new list item, editing a list item, or displaying that list item. When you add a new column to that list, the column automatically gets added to those forms. Where is this code generated? And how can you create your own Create, Edit, and Display forms?
 
If you look in the 12 hive, in the CONTROLTEMPLATES folder, you'll see a file called DefaultTemplates.ascx. This file is rather huge, and if you open it up, you'll see snippets of code that define the UI for everything from the label and textboxes you see on the automatically generated forms, to controls that render Blog posts and Calendar views. As we all know, though, it's a no-no to edit core SharePoint files. (After all, what would happen when a service pack got released? Your changes would be overwritten.) SharePoint allows you to create your own control templates, and put them in a different file. As long as they reside in the CONTROLTEMPLATES directory, and use the SharePoint:RenderingTemplate server control, you can reference the template by the ID you assign it. (If you're overriding an existing control, just give it the same ID as the control in the DefaultTemplates.ascx file, and SharePoint will automatically override the default template with your own.) (See How to: Override a Default Control Template for more details.)
 
OK, so how do we use this to our advantage? It's possible to create your own ascx file with your own RenderingTemplate, and reference that template when displaying a form associated with a content type. For instance, when you create a new list item, perhaps you want the fields to show up in two columns of a table, rather than in one long vertical list. You can build an HTML table with two columns in your list, then use the SharePoint:FormField server controls for displaying the list columns that the user will enter their info into. Your code might look like this:
 
<SharePoint:RenderingTemplate ID="CustomContentTypeCreateForm" runat="server">
<Template>
<table width="100%" cellspacing="0" cellpadding="10" border="0">
<tr>
<td><SharePoint:FieldDescription runat="server" ID="TitleDescriptionField" ControlMode="New" FieldName="Title" /></td>
<td><SharePoint:FormField runat="server" ID="TitleField" ControlMode="New" FieldName="Title" /></td>
<td><SharePoint:FieldDescription runat="server" ID="FavoriteColorDescriptionField" ControlMode="New" FieldName="FavoriteColor" /></td>
<td><SharePoint:FormField runat="server" ID="FavoriteColorField" ControlMode="New" FieldName="FavoriteColor" /></td>
</tr>
</table>
</Template>
</SharePoint:RenderingTemplate>

(Make sure you have the right registrations at the top of your .ascx page, like the SharePoint assembly registration and the SharePoint control prefix.)

If you have controls in your template that need compiled code to run, such as showing or hiding fields in the template based on the field values, you can add references in the .ascx page to your own compiled web controls. You'll just need to make sure you add a reference to your compiled web control at the top of the .ascx page, and deploy the dll with your SharePoint solution package.

The next step is to add a reference to your newly-created rendering template to your custom content type. If you take a look at the XML schema for defining a content type, you'll see a node called XmlDocuments. Inside that, you'll add an XmlDocument node, inside which you'll add a FormTemplates node. In the FormTemplates node, you can add a New, Edit, and/or Display node, inside of which you'll put the ID of the rendering template it should use. See FormTemplates Schema Overview for more information.

Based on the example rendering template described above, your XmlDocuments node might look like this:

<XmlDocuments>
  <XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
   <FormTemplates  xmlns="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
     <New>CustomContentTypeCreateForm</New>
   </FormTemplates>
  </XmlDocument>
</XmlDocuments>

If you would like to associate this content type with a custom list definition, you can do it by adding the content type to the list's schema.xml file. You would add the content type to the ContentTypes node. If you make this content type the default content type for the list (or the only content type for that matter), it will be the content type that's automatically used when a user clicks the "New" button on a list's toolbar. If you've created a custom "New" form for that content type, your custom rendering template will be used to render the "New" form the user sees.

Comments

AA

thanks ..
 on 9/1/2008 10:17 AM

Re: Creating Custom List Forms for Creating, Editing, or Displaying a List Item

Another interesting control available from SharePoint Webcontrols assembly is ListFieldIterator. Setting few properties like SiteID, ListID, ControlMode, ItemID etc. generates the custom form. Same can be used in webparts as well as application pages.
 on 9/4/2008 3:43 PM

ControlMode="Edit" does not update fields

Thanks for the article! In my case adding items and displaying the values works fine, but for some reason the SharePoint:FormField  ControlMode="Edit" shows the value fine, but after update, the values turn to null. However the list item gets updated right because the modified field is updated. Any ideas?
 on 10/22/2008 2:25 PM

Solved! ControlMode="Edit" does not update fields

I'm loading controls programmatically inside the custom template so the this.LoadControl("~/_controltemplates/MyCtrl.ascx") code should be in the  OnInit(EventArgs e) function, and not in the Page_Load. Now everything is working fine.
 on 10/23/2008 1:06 AM

Re: Creating Custom List Forms for Creating, Editing, or Displaying a List Item

lifesaver! thanks very much.
 on 11/3/2008 9:19 AM

How much of this solution approach requires Server Admin rights?

Thanks for this solution Becky - it sounds like it'd make for a great solution (e.g. for Lists with dozens of Columns, where we'd like to automatically hide any empty fields in the DispForm.aspx at runtime).  However, I'm concerned that at least some of these steps will require Server Admin access (which I don't have, and which would take 6+ months to negotiate for each such requested change).  Is there any way you could let us know which of these steps would be impossible without Server Admin, and if there's any way that we could attempt similar effect with only SPD + Site Collection Admin rights?  Many thanks for *any* thoughts you had time to share.
 on 11/21/2008 2:54 PM

Brilliant!

way cool
 on 12/4/2008 3:36 PM

Unrelated question - anonymous commenting

I came upon this post when researching a problem I am having with my Sharepoint blog. I am sorry if this is unrelated!   When an anonymous user submits his/her comment, I get a server out of memory error. The comment submits into the list, but the user gets the .NET error. If they go back to the post, all is well.  If the user is logged in, then it works just fine. It is set up (visually) just like your blog here.  I think it has to do with permissions, just have no idea. Any ideas?
 on 2/18/2009 10:35 AM

custom field type not getting saved through the default custom list form(dataformwebpart)

I have a custom field type column which has its values properly saved through a list form but when I use the custom list form (dataformwebpart) the values goes blank
 on 3/10/2009 9:32 AM

Try to modify the Edit form of the custom content type

I used Sharepoint Designer and did the follwing -Create a new ASPX page -Insert Sharepoint Control -Choose Custom List Form... -Pick my custom task list and the content type and click OK.   But after this, nothing happened. I mean no code(columns of fields)has been inserted.  Another strange thing is when I open the default edit form aspx for my custom task list, it only shows three genric columns with names such as Name1, Name2, etc. It is totally different from when I run this edit form from IE or Firefox(in which all the columns shown as I expected).  Please let me know why this happened? What might I miss? Thanks Xiao
 on 4/30/2009 3:39 PM
1 - 10Next

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Comment Title *


Body *


Your Name


Your E-mail Address


Your Website

Type the Web address: (Click here to test)  

Type the description: 

Are you spamming my blog? *


This field is here in an attempt to stop spammers from entering comments. Enter a number, any number

Attachments