I decided to write this post after reading a community question on a forum about how can you export and import a list template programmatically. This post will explain how can you save a list template from an existing list and then copy it to a different site (in a different web application). It is fairly a simple process if you are using SharePoint interface. But how can you accomplish this programmatically? Assume the template you want to save is from a list called "MyList"
using (SPWeb web = SPContext.Current.Web)
{
SPList list = web.Lists["MyList"];
list.SaveAsTemplate("MyListTemplate.stp", "My List", "My List Desc", true);
list.Update();
} Once you have the SPList it is pretty straight forward to save the list template. here I am saving the template as MyListTeplate.stp. and giving the template title as "My List". When you run this bit of code and go to "Site Actions->Create" you will see the newly created list template under Libraries section. Or you can check the List Template Gallery by going to "Site Actions->Site Settings" and clicking on "List templates" under "Galleries" section.
Now how would you import the same list template to another site in a different web application programmatically?
when you save the list template you can read the file in to a byte array without actually saving it in to the hard disk and reading it. Get the "List Template Gallery" list into an SPList. This list contains all the list templates for lists in your site. You can read the list template into a byte array using this code.
byte[] templateArr = lst.RootFolder.Files["_catalogs/lt/MyListTemplate.stp"].OpenBinary();
Open the site you want to copy this list template and get the "List template gallery" list of that site (http://MySiteB). Below code illustrates this operation. However you will have to make adjustment the code to cater for different security issues depending on your site.
using (SPWeb web = SPContext.Current.Web)
{
SPList lst = web.Lists["List Template Gallery"];
//you can open the template file into a byte array
byte[] templateArr = lst.RootFolder.Files["_catalogs/lt/MyListTemplate.stp"].OpenBinary();
SPUser user = web.CurrentUser;
SPUserToken usrToken = user.UserToken;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
//open the site using existing user credentials.
using (SPSite site = new SPSite(http://MySiteB, usrToken))
{
using (SPWeb web2 = site.RootWeb)
{
web2.AllowUnsafeUpdates = true;
//get the existing liste template list of site B
SPList templateList = web2.Lists["List Template Gallery"];
try
{
//add the template file (byte array) to the template list of this site.
templateList.RootFolder.Files.Add("_catalogs/lt/MyListTemplate.stp", templateArr, true);
templateList.Update();
}
catch (Exception er)
{ }
}
}
});
}
Now if you go to http://MySiteB and inspect the list template gallery you will see the newly added template. I'd be interested to know your comments and better ways to do this. So looking forward to know your ideas and suggestions.
Sajana Palanagama
Posted
07-06-2008 7:30 PM
by
Sajana