Thursday 4 August 2011

Quickest Way to design a List Template

Overview
Any type of deployment on the SharePoint Farm must be done via Features. These features would be contained within a WSP package which would be deployed using stsadm –o addsolution command.
The main gist is to facilitate not only the adding/retraction of these features but also the automation of the entire process by using batch files and window schedulers.


ListTemplate

ListTemplate is simply a schema of a SharePoint List. It is based on XML. It is considered to be the natural way of adding a new type of List.
Its formation includes declaring content types, views, fields, and forms.

Quickest way

There are many ways of creating a ListTemplate. The most easiest way, I believe, is to create the List with the columns that you want using the SharePoint UI[1] (i.e Site Settings  View All Contents  Create). Save it as a List template. Download the stp file, change it to a cab file, export its content and then copy and past the part of that file and paste it into your schema.xml
The schema.xml would be part of your solution it should be located at the same position then in Fig 1. Note that the name of the folder (Vydio) may be different based on the name attribute of your feature.xml

[1] http://office.microsoft.com/en-au/windows-sharepoint-services-help/create-a-list-HA010099248.aspx#BM1


Fig 1. Note that the aspx file may not be needed









Feature.xml

< Feature Id="09e1e9f5-79ee-4eb7-8cc1-094a2506cd8a"
Title="VydioList"
Description="This is the List Template for the Vydeo SharePoint List (Video Conference)"
Version="12.0.0.0"
Hidden="FALSE"
Scope="Web"
DefaultResourceFile="core"
ReceiverAssembly="Macmillan.Greenroom.ListTemplate, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4708d659fa6e9b7f"
ReceiverClass="Macmillan.Greenroom.ListTemplate.VydioFeatureReceiver"
xmlns="http://schemas.microsoft.com/sharepoint/">
< ElementManifests>










Notice: the difference between ElementManifest and ElementFile, also note the scope which is Web. This very important if you which to be able to used the event receiver correctly for that particular scenario.



ListTemplate.xml

< Elements Id="d3774b95-c579-44cc-b4e6-2fc23bb708ac" xmlns="http://schemas.microsoft.com/sharepoint/">
< ListTemplate Name="Vydio"
DisplayName="Vydio"
Description="Create a Vydio List"
SecurityBits="11"
BaseType="0"
Type="100"
FeatureId="09e1e9f5-79ee-4eb7-8cc1-094a2506cd8a"
OnQuickLaunch="FALSE"
Image="/_layouts/images/itgen.gif"/>


Notice the name would determine the name of the folder holding the schema.xml and other files (e.g. aspx)



ListInstance.xml

It is in charge of creating instance of the list template.


< Elements Id="62E9CB07-AAF1-4fa5-A89E-141A954A8B75" xmlns="http://schemas.microsoft.com/sharepoint/">
< ListInstance
FeatureId="09e1e9f5-79ee-4eb7-8cc1-094a2506cd8a"
Title="Vydio 1"
Description="Vydio 1 (Video Confenrencing)"
OnQuickLaunch="FALSE"
Id="1036"
TemplateType="100"
Url="Lists/Vydio 1">

< ListInstance
FeatureId="09e1e9f5-79ee-4eb7-8cc1-094a2506cd8a"
Title="Vydio 2"
Description="Vydio 2 (Video Confenrencing)"
OnQuickLaunch="FALSE"
Id="1037"
TemplateType="100"
Url="Lists/Vydio 2">

< ListInstance
FeatureId="09e1e9f5-79ee-4eb7-8cc1-094a2506cd8a"
Title="Vydio 3"
Description="Vydio 3 (Video Confenrencing)"
OnQuickLaunch="FALSE"
Id="1038"
TemplateType="100"
Url="Lists/Vydio 3">



Notice that you can have as many ListInstance as required by your business requirement.




Feature receiver

A feature can also include a reference to a feature receiver which job consist of handle the following four events: FeatureActivated, FeatureDeactivating, FeatureInstalled, FeatureUninstalling

FeatureDeactiving has been used to have an easy way of cleaning the List created.

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
//throw new Exception("The method or operation is not implemented.");
using (SPWeb currentSite = properties.Feature.Parent as SPWeb)
{
try
{
SPList vydio1 = currentSite.Lists["Vydio 1"];
vydio1.Delete();
SPList vydio2 = currentSite.Lists["Vydio 2"];
vydio2.Delete();
SPList vydio3 = currentSite.Lists["Vydio 3"];
vydio3.Delete();
currentSite.Update();
}
catch (Exception s) { }
}
}

Check the entire solutions at: Macmillan.Greenroom.ListTemplate.

Other references:

http://karinebosch.wordpress.com/walkthroughs/create-custom-list-templates-in-caml/
http://blogit.create.pt/blogs/andrevala/archive/2008/06/17/SharePoint-2007-Deployment_3A00_-List-Instance-Features.aspx

http://platinumdogs.wordpress.com/2009/09/23/creating-a-sharepoint-list-template-using-a-feature/