Monday, December 27, 2010

Create list in Feature using list templates

Suppose you have your list templates ready with you. Now you want to deploy the same using a solution. How will you do that using a Feature.

Step 1: Create a Feature folder in your solution
Step 2: Create a folder for your templates say 'ListTemplates' and place all your stp files there.
Step 3: Create a Feature.xml file -

Feature xmlns="http://schemas.microsoft.com/sharepoint/"
Id="93E56B63-EA76-43b7-9068-162DCFA1FAE4"
Title="IMEVUSERLIST"
Description="Deploy user list for Instant Messaging"
Scope="Web"
Hidden="false"
Version="1.0.0.0"
ReceiverAssembly="Eversheds.InstantMessaging,Version=1.0.0.0,Culture=neutral, PublicKeyToken=b0c7276684f0a390"
ReceiverClass="Eversheds.InstantMessaging.FeatureReceiver.ReceiverEVUserList"
ElementManifests
ElementManifest Location="Elements.xml"/
/ElementManifests
Properties xmlns="http://schemas.microsoft.com/sharepoint/"
!-- Instant Messaging Lists --
Property Key="InstantMessagingLists" Value="EVChatImage,EVConfigInfo,EVUserList"/
/Properties
/Feature


Step 3: Create Elements file

?xml version="1.0" encoding="utf-8" ?
Elements xmlns="http://schemas.microsoft.com/sharepoint/"
/Elements

Step 4: Create feature receiver

namespace Eversheds.InstantMessaging.FeatureReceiver
{
public class ReceiverEVUserList : SPFeatureReceiver
{}
}

Override all events. Below is the code for FeatureActivated

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb parentWeb;
SPSite siteCollection;

if (properties.Feature.Parent is SPWeb)
{
parentWeb = (SPWeb)properties.Feature.Parent;
siteCollection = parentWeb.Site;
}
else if (properties.Feature.Parent is SPSite)
{
parentWeb = ((SPSite)properties.Feature.Parent).RootWeb;
siteCollection = (SPSite)properties.Feature.Parent;
}
else
{
throw new Exception("Unable to retrieve SPWeb - this feature is not Site or Web-scoped.");
}

string directory = properties.Definition.RootDirectory;

try
{
if (!directory.EndsWith("\\"))
directory += "\\";
directory += "ListTemplates";

if (System.IO.Directory.Exists(directory))
{
string[] templates = System.IO.Directory.GetFiles(directory, "*.stp", System.IO.SearchOption.TopDirectoryOnly);
SPDocumentLibrary listTemplates = siteCollection.GetCatalog(SPListTemplateType.ListTemplateCatalog) as SPDocumentLibrary;

UploadTemplates(listTemplates, templates);
// Add lists from the List Template Gallery


parentWeb.AllowUnsafeUpdates = true; //get the list of list templates from the web
SPListTemplateCollection customListTemplates = siteCollection.GetCustomListTemplates(parentWeb); //create the connection library using the uploaded list template
//Get list collection
SPListCollection listCollection = parentWeb.Lists;

//Create List EVConfigInfo------------------------
if (ListExists(listCollection, "EVConfigInfo"))
{
//get Guid for list
SPList l = parentWeb.Lists["EVConfigInfo"];
Guid listId = l.ID;
//delete list
parentWeb.Lists.Delete(listId);
}

SPListTemplate stpConfigInfo = customListTemplates["EVConfigInfo"];
parentWeb.Lists.Add("EVConfigInfo", "List for Config Info", stpConfigInfo);


//Create List EVUserList------------------------
if (ListExists(listCollection, "EVUserList"))
{
//get Guid for list
SPList l = parentWeb.Lists["EVUserList"];
Guid listId = l.ID;
//delete list
parentWeb.Lists.Delete(listId);
}

SPListTemplate stpUserList = customListTemplates["EVUserList"];
parentWeb.Lists.Add("EVUserList", "List for User Info", stpUserList);

//Create List EVChatImage------------------------
if (ListExists(listCollection, "EVChatImage"))
{
//get Guid for list
SPList l = parentWeb.Lists["EVChatImage"];
Guid listId = l.ID;
//delete list
parentWeb.Lists.Delete(listId);
}

SPListTemplate stpChatImage = customListTemplates["EVChatImage"];
parentWeb.Lists.Add("EVChatImage", "List for Chat image", stpChatImage);

parentWeb.Update();
}
}
catch (Exception ex)
{
using (StreamWriter w = new StreamWriter("C:\\log.txt"))
{
w.WriteLine(ex.ToString());
}
}
}

#region UploadTemplates
///
/// Method to create Sharepoint Lists from template files
///

///
///
private void UploadTemplates(SPDocumentLibrary templateGallery, string[] templateFiles)
{
if (templateGallery != null)
{
foreach (string template in templateFiles)
{
System.IO.FileInfo fileInfo = new System.IO.FileInfo(template);
SPQuery query = new SPQuery();
query.Query = string.Format("" + "{0}", fileInfo.Name);
SPListItemCollection existingTemplates = templateGallery.GetItems(query);
int[] Ids = new int[existingTemplates.Count];
for (int i = 0; i < existingTemplates.Count; i++)
{
Ids[i] = existingTemplates[i].ID;
}
for (int j = 0; j < Ids.Length; j++)
{
templateGallery.Items.DeleteItemById(Ids[j]);
} byte[] stp = System.IO.File.ReadAllBytes(template);
templateGallery.RootFolder.Files.Add(fileInfo.Name, stp);
}
}
}
#endregion

private static bool ListExists(SPListCollection collection, string title)
{
bool flag = false;
foreach (SPList list in collection)
{
if (list.Title == title)
flag = true;

}
return flag;
}

Step 5: Deploy using wsp builder. The lists will be created

No comments:

Post a Comment