Customizing SharePoint 2010 item forms with Visual Studio 2010

About a week ago I was talking about Dynamically changing list rendering templates using SharePoint designer which allows you to personalize your New/Edit and Display/View forma templates or have custom logic or controls on it. In the example last week, I used SharePoint designer to apply the existing rendering template to the list. In many cases using SharePoint designer is not an option. In this post I’ll show you how you can do the same using a Visual Studio solution.

Here are the steps we need to perform:

1. Define a list instance either inheriting from existing list template or from a custom one.
2. Profision a module that will set the form pages on the list.
3. Define forms that will be used on the form pages within a module.

First, I assume your running Visual Studio 2010 RC and already have created a SharePoint 2010 solution in it. Now using a wizard provision a New Item to the solution that will be your custom ListInstance. I have provisioned a list instance based of the Custom List definition and here the Elements.xml I get back:

<?xml version=”1.0″ encoding=”utf-8″?>
<Elements xmlns=””>
<ListInstance FeatureId=”{00bfea71-de22-43b2-a848-c05709900100}”
TemplateType=”100″ Title=”MyList” Description=””
Url=”Lists/MyList” CustomSchema=”Files\Lists\MyList\Schema.xml” RootWebOnly=”FALSE”
xmlns=”” />

Now, you will need to provision a schema.xml that your list will be adhering to. Schema file will define what columns and users will be able to see in the list as well as content type that list items inherit from. Here is a snippet that shows defining a content type right in your schema.xml:

<ContentType ID=”0x01″ Name=”Item” Group=”List Content Types” Description=”Create a new list item.” FeatureId=”{695b6570-a48b-4a8e-8ea5-26ea7fc1d162}”>
<Folder TargetName=”Item” />
<XmlDocument NamespaceURI=””>
<FormTemplates xmlns=””>

Finally, create a new module item in your list instance. The final structure of your list instance, schema, and module will look like this:

The elements.xml file in your module will define all of the forms that will be used in your list. Here is how an Edit Item form would look like:

<?xml version=”1.0″ encoding=”utf-8″?>
<Elements xmlns=””>
<Module Name=”ListsMyList_pages” Url=”Lists/MyList” SetupPath=”pages” RootWebOnly=”FALSE” Path=”Files\Lists\MyList” xmlns=””>
<File Url=”EditForm.aspx” Type=”Ghostable” Path=”form.aspx”>
<AllUsersWebPart WebPartOrder=”1″ WebPartZoneID=”Main” >
<![CDATA[<WebPart xmlns:xsi=”” xmlns:xsd= xmlns=”″>
<Title />
<Description />
<Height />
<Width />
<DetailLink />
<HelpLink />
<PartImageSmall />
<MissingAssembly>Cannot import this Web Part.</MissingAssembly>
<PartImageLarge />
<IsIncludedFilter />
<Assembly>Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
<ListName xmlns=””>{$ListId:Lists/MyList;}</ListName>
<ListId xmlns=””>$ListId:Lists/MyList;</ListId>
<PageType xmlns=””>PAGE_EDITFORM</PageType>
<FormType xmlns=””>6</FormType>
<ControlMode xmlns=””>Edit</ControlMode>
<ViewFlags xmlns=””>Default</ViewFlags>
<ListItemId xmlns=””>0</ListItemId>
<TemplateName xmlns=””>BlogCommentsForm</TemplateName>
.. Display Form ..

.. New Form ..


As you can tell, TemplateName is the value of my desired list form template, in this case : BlogCommentsForm

Now when you deploy your solution, the list will be provisioned along with all of the pages you have defined.

Good luck!

About the author: Yaroslav Pentsarskyy