Print Shortlink

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=”http://schemas.microsoft.com/sharepoint/”>
<ListInstance FeatureId=”{00bfea71-de22-43b2-a848-c05709900100}”
TemplateType=”100″ Title=”MyList” Description=””
Url=”Lists/MyList” CustomSchema=”Files\Lists\MyList\Schema.xml” RootWebOnly=”FALSE”
xmlns=”http://schemas.microsoft.com/sharepoint/” />
</Elements>

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=”0×01″ Name=”Item” Group=”List Content Types” Description=”Create a new list item.” FeatureId=”{695b6570-a48b-4a8e-8ea5-26ea7fc1d162}”>
<Folder TargetName=”Item” />
<FieldRefs>..</FieldRefs>
<XmlDocuments>
<XmlDocument NamespaceURI=”http://schemas.microsoft.com/sharepoint/v3/contenttype/forms”>
<FormTemplates xmlns=”http://schemas.microsoft.com/sharepoint/v3/contenttype/forms”>
<Display>ListForm</Display>
<Edit>ListForm</Edit>
<New>ListForm</New>
</FormTemplates>
</XmlDocument>
</XmlDocuments>
</ContentType>

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=”http://schemas.microsoft.com/sharepoint/”>
<Module Name=”ListsMyList_pages” Url=”Lists/MyList” SetupPath=”pages” RootWebOnly=”FALSE” Path=”Files\Lists\MyList” xmlns=”http://schemas.microsoft.com/sharepoint/”>
<File Url=”EditForm.aspx” Type=”Ghostable” Path=”form.aspx”>
<AllUsersWebPart WebPartOrder=”1″ WebPartZoneID=”Main” >
<![CDATA[<WebPart xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd=http://www.w3.org/2001/XMLSchema xmlns="http://schemas.microsoft.com/WebPart/v2">
<Title />
<FrameType>Default</FrameType>
<Description />
<IsIncluded>true</IsIncluded>
<ZoneID>Main</ZoneID>
<PartOrder>1</PartOrder>
<FrameState>Normal</FrameState>
<Height />
<Width />
<AllowRemove>true</AllowRemove>
<AllowZoneChange>true</AllowZoneChange>
<AllowMinimize>true</AllowMinimize>
<AllowConnect>true</AllowConnect>
<AllowEdit>true</AllowEdit>
<AllowHide>true</AllowHide>
<IsVisible>true</IsVisible>
<DetailLink />
<HelpLink />
<HelpMode>Modeless</HelpMode>
<Dir>Default</Dir>
<PartImageSmall />
<MissingAssembly>Cannot import this Web Part.</MissingAssembly>
<PartImageLarge />
<IsIncludedFilter />
<Assembly>Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
<TypeName>Microsoft.SharePoint.WebPartPages.ListFormWebPart</TypeName>
<ListName xmlns="http://schemas.microsoft.com/WebPart/v2/ListForm">{$ListId:Lists/MyList;}</ListName>
<ListId xmlns="http://schemas.microsoft.com/WebPart/v2/ListForm">$ListId:Lists/MyList;</ListId>
<PageType xmlns="http://schemas.microsoft.com/WebPart/v2/ListForm">PAGE_EDITFORM</PageType>
<FormType xmlns="http://schemas.microsoft.com/WebPart/v2/ListForm">6</FormType>
<ControlMode xmlns="http://schemas.microsoft.com/WebPart/v2/ListForm">Edit</ControlMode>
<ViewFlags xmlns="http://schemas.microsoft.com/WebPart/v2/ListForm">Default</ViewFlags>
<ListItemId xmlns="http://schemas.microsoft.com/WebPart/v2/ListForm">0</ListItemId>
<TemplateName xmlns="http://schemas.microsoft.com/WebPart/v2/ListForm">BlogCommentsForm</TemplateName>
</WebPart>]]></AllUsersWebPart>
</File>
.. Display Form ..

.. New Form ..

</Module>
</Elements>

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!