Creating a SharePoint List Template using a Feature


Building on the last post about creating a content type, in this post we’ll create a custom list template which uses that content type and deploy it using a feature.

If you’ve ever taken a look at the schema definition file for a list template, you may baulk at it’s size and complexity, in fact you can ignore most of this, creating a custom list template turns out to be relatively straightforward.

A list template comprises 2 files;

  1. The list template.xml file
  2. The list template schema.xml file

Create the list template.xml file and add it to your feature manifest file;

 
<?xml version="1.0" encoding="utf-8"?>
<Elements Id="8f1d5d09-6834-40b7-b245-c62c0811596e" xmlns="http://schemas.microsoft.com/sharepoint/">
	<ListTemplate Name="MyTestListTemplate" 
		  DisplayName="TestList" 
		  Description="" 
		  BaseType="0" 
		  Type="100" 
		  OnQuickLaunch="TRUE" 
		  SecurityBits="11" 
		  Sequence="410" 
		  Image="/_layouts/images/itgen.gif" />
</Elements>

Note the Name and BaseType attributes, we will create a subfolder with the same name as the Name attribute and place the schema.xml file there. The BaseType attribute indicates the list type from which this list templates inherits which in this case is the basic custom list.
Add the listtemplate.xml file to the feature manifest;

<?xml version="1.0" encoding="utf-8"?>
<Feature  Id="adc873b4-fe40-4169-8304-c9a1dd697c2b"
          Title="MySiteTypes"
          Description="Description for MySiteTypes"
          Version="12.0.0.0"
          Hidden="FALSE"
          Scope="Site" 
          ActivateOnDefault="FALSE" 
          ImageUrl="GenericFeature.gif"
          DefaultResourceFile="core"
          xmlns="http://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest Location="sitecolumns.xml"/>
    <ElementManifest Location="mycontenttype.xml"/>
    <ElementManifest Location="listtemplate.xml"/>
  </ElementManifests>
</Feature>

Now for the schema.xml file, create a subfolder in your feature named “MyTestListTemplate” and in that folder create a new XML file called schema.xml.
lt01
To populate this file you have some options;

  1. Create it manually (take a look at the huge amount of CAML!!)
  2. Copy the base Custom List schema.xml file and modify it
  3. Craft the list/views etc in SharePoint using the UI and export the template using SharePoint Manager or the SharePoint Solution Generator

Using one of these methods you should then modify your schema.xml file as follows;

Change the Name, Title and URL attributes;

<List Name="TestList" Title="TestList" Description="" Direction="0" BaseType="0" 
		Url="Lists/TestList" 
		FolderCreation="FALSE" 
		EnableContentTypes="TRUE" 
		Type="100" 
		Id="8f1d5d09-6834-40b7-b245-c62c0811596e" 
		xmlns="http://schemas.microsoft.com/sharepoint/">

The contenttypes section should contain only the content type(s) you’ve defined for this list;

	<ContentTypes>
		<ContentTypeRef ID="0x0100928100FAB05A4148BD6F4C8E6A716B40" />
	</ContentTypes>

To the Fields section add only the field definitions defined in the content types list in the above contenttypes section;

<Fields>
	<Field Type="Number" DisplayName="MyReference" Required="FALSE" Decimals="0" Commas="FALSE" Group="My Group" ID="{918BA6B8-60BC-417e-AEE5-F9E1A0A59144}" StaticName="MyReference" Name="MyReference" Customization="" SourceID="{6a05fd42-eda7-4fb7-9bfd-2a35ded20c24}" ColName="float1" RowOrdinal="0">
		<Default>0</Default>
	</Field>
	<Field Type="Choice" DisplayName="MyType" Description="Description of MyType" Required="FALSE" Format="RadioButtons" FillInChoice="FALSE" Group="My Group" ID="{1FDC83BE-D596-45e2-9DD3-AD3338798784}" StaticName="MyType" Name="MyType" Customization="" SourceID="{6a05fd42-eda7-4fb7-9bfd-2a35ded20c24}" ColName="nvarchar3" RowOrdinal="0">
		<Default>Person</Default>
		<CHOICES>
			<CHOICE>Person</CHOICE>
			<CHOICE>Place</CHOICE>
			<CHOICE>Thing</CHOICE>
		</CHOICES>
	</Field>
	<Field Type="Note" DisplayName="MyDetails" Description="Description of MyDetails" Required="FALSE" NumLines="5" RichText="FALSE" Sortable="FALSE" Group="My Group" ID="{AB31BEFA-A178-4a1e-B324-B73E9C15D5A8}" StaticName="MyDetails" Name="MyDetails" AllowHyperlink="TRUE" RichTextMode="Compatible" IsolateStyles="FALSE" AppendOnly="FALSE" Customization="" SourceID="{6a05fd42-eda7-4fb7-9bfd-2a35ded20c24}" ColName="ntext2" RowOrdinal="0">
		<Default>Unknown</Default>
	</Field>
</Fields>

Modify the ViewFields section of each View (there are 2 views defined by default, a HTML view and the AllItems view) to include the columns required;

	<ViewFields>
		<FieldRef Name="Attachments" />
		<FieldRef Name="LinkTitle" />
		<FieldRef Name="MyReference" />
		<FieldRef Name="MyType" />
		<FieldRef Name="MyDetails" />
	</ViewFields>

You might also want to use the LinkTitleNoMenu column which is a straight link column to the proprty page of the list item, rather than the LinkTitle column which includes the dropdown ECB menu.

If you have exported the schema.xml file using one of the methods mentioned, you should check that your View element(s) (except the view with BaseViewID of 0 zero) have the SetupPath attribute set correctly.

<View DefaultView="TRUE" 
      Type="HTML" DisplayName="All Items" Url="AllItems.aspx" Level="1" BaseViewID="1" ContentTypeID="0x" 
      SetupPath="pages\viewpage.aspx" 
      ImageUrl="/_layouts/images/DECISION.GIF" WebPartZoneID="Main">
   ...
   ...
</View>

Finally check that the Forms section is as follows;

    <Forms>
      <Form Type="DisplayForm" Url="DispForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
      <Form Type="EditForm" Url="EditForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
      <Form Type="NewForm" Url="NewForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
    </Forms>

Build and Deploy the feature to your SharePoint site and activate the MySiteTypes feature.
lt02
The new “Test List” list template will now appear in the Custom Lists section of a sites Create page as shown.
lt03
You can now create a sample list based on this template.
lt04

Published by

Phil Harding

SharePoint Consultant, Developer, Father, Husband and Climber.

2 thoughts on “Creating a SharePoint List Template using a Feature

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s