Creating custom new/edit/view forms for list items in SharePoint 2010
As in MOSS, SharePoint 2010 takes the concept of rendering templates to the next level and allows you easily create an instance of your own list view/new/edit form with your fields on it and any rendering logic you might have.
Here is how we go about creating custom rendering template for a new content type based of the existing content type:
1. With Visual Studio 2010 create an Empty SharePoint Solution; this will create a place holder for your features and standard deployment scripts.
2. Create new feature, let’s call it Prerequisites
3. In your SharePoint project Add a new Item and this will be a content type, let’s call it MyPosts
we’re going to inherit it from existing content type – the Post
4. Now, we’re going to create a list definition based of the content type, for this, in your SharePoint project Add a new Item
we’re going to select out MyPosts content to based the definition on. Also we’re going to ask the wizard to create a list instance that will provision the list on our site.
5. Now that the definition and the list are created we can assing our custom rendering template to be used for the form New Item (in this case). Open your content type definition and right under the </FieldRefs> paste the following:
<XmlDocuments>
<XmlDocument NamespaceURI=”http://schemas.microsoft.com/sharepoint/v4/contenttype/forms”>
<FormTemplates xmlns=”http://schemas.microsoft.com/sharepoint/v4/contenttype/forms”>
<Display>MyPostingTemplate</Display>
<Edit>MyPostingTemplate</Edit>
<New>MyPostingTemplate</New>
</FormTemplates>
</XmlDocument>
</XmlDocuments>
Which will ensure our own rendering template will be used for the content type.
6. Now, map a new Mapped folder in your SharePoint project to ControlTemplates where all of our rendering templates reside.
7. Create a new User Control in that folder with any name, make sure you have referenced all of the SharePoint required references:
<%@ Control Language=”C#” AutoEventWireup=”true” CodeBehind=”MyControl.ascx.cs” Inherits=”MyProject.ControlTemplates.MyControl, $SharePoint.Project.AssemblyFullName$” %>
<%@ Assembly Name=”Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>
<%@ Register TagPrefix=”SharePoint” Assembly=”Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” namespace=”Microsoft.SharePoint.WebControls”%>
<%@ Register TagPrefix=”ApplicationPages” Assembly=”Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” namespace=”Microsoft.SharePoint.ApplicationPages.WebControls”%>
<%@ Register TagPrefix=”SPHttpUtility” Assembly=”Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” namespace=”Microsoft.SharePoint.Utilities”%>
<%@ Register TagPrefix=”wssuc” TagName=”ToolBar” src=”~/_controltemplates/ToolBar.ascx” %>
<%@ Register TagPrefix=”wssuc” TagName=”ToolBarButton” src=”~/_controltemplates/ToolBarButton.ascx” %>
RIght under it you can place the default rendering template found in C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\CONTROLTEMPLATES\DefaultTemplates.ascx
The one of our interest is
<SharePoint:RenderingTemplate id=”ListForm” runat=”server”>
Find it in the DefaultTemplates.ascx and copy the entire definition
8. Modify the renderingTemplate ID to the ID you have specified in your content type definition in my case MyPostingTemplate
9. Now you can make changes to MyControl.ascx that suit your needs and those changes will be reflected on the form when users click New/Edit/View item. Of course you can pick separate rendering templates residing in the same or separate controls to suit your needs for each of the new, view and edit views.
Enjoy!
