N.B. I've not tested this code and may not be written using the best practice (esply in disposing the objects), grab the locgic and implement you own.
This peace of code is used to create 8 document libraries with in the specified document library using the custom document library template.
The doc library name and descriptions are picked from the respective arrays.
Pre-requisits: The custom document library template need to be presented in the list template gallary of the site collection.
This can be extended to create the doc libraries in all the webs in the given site collection by iterating through all the site.webs.
namespace MigrateDocLibrary
{
using Microsoft.SharePoint;
public static class HelperDocLib
{
public static void CreateDocLibrayUsingCustomTemplate()
{
string siteCollURL = string.Empty;
string customDocLibTemplateName = string.Empty;
string docLibName = string.Empty;
string docLibDesc = string.Empty;
SPList olist = null;
string[] docLibNames = new string[] { "one", "two", "three", "four", "five", "six", "seven", "eight" };
//string[] docLibDescs = new string[] { "oneDesc", "twoDesc", "threeDesc", "fourDesc", "fiveDesc", "sixDesc", "sevenDesc", "eightDesc" };
customDocLibTemplateName = "CustomDocTemplate";
siteCollURL = "http://webAppName:2020/siteCollName";
docLibName = "DefaultName";
//docLibDesc = "DefaultDesc";
try
{
using (SPSite osite = new SPSite(siteCollURL))
{
SPWebCollection spWebColl = osite.AllWebs;
//SPWeb oweb = osite.OpenWeb();
foreach (SPWeb oweb in spWebColl)
{
SPListTemplateCollection listTemplateColl = osite.GetCustomListTemplates(oweb);
SPListCollection spListColl = oweb.Lists;
for (int i = 1; i <= 3; i++)
{
docLibName = docLibNames[i];
Guid listGuid = spListColl.Add(docLibName, "", listTemplateColl[customDocLibTemplateName]);
olist = oweb.Lists[listGuid];
olist.OnQuickLaunch = true;
olist.Update();
}
oweb.Dispose();
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
//
}
}
}
}
Monday, July 6, 2009
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment