Technicalities:
I will first take you through the classes involved in development.
- SPListCollection
- SPListTemplateType
- SPListTemplate
- SPDocTemplate
SPListCollection has Add method with 7 overloads. Look for the method signatures here.
We will concentrate on the following signature:
public virtual Guid Add(
string title, //Title for the library
string description,//Description about the library
SPListTemplate template,//The list template. We are interested in Document Library.
SPDocTemplate documentTemplate //The document template for library.e.g. Excel, Word or Powerpoint etc.
)
SPListTemplateType is a enumeration. The enumeration has underlying integer values which match the Type attribute of ListTemplate element. Please visit here for details.
SPListTemplate represents the list definition or list template for the list. The list definition/template has the views and fields defined. SPWeb.ListTemplates returns the list definitions for the web site. SPSite.GetCustomListTemplates method returns the list template collection for the site collection. More details.
SPDocTemplate represents the document template. Whenever we create a document library, it is created by using blank Word Document template. There are many document templates available in SharePoint 2010 OOB. The document templates have IDs. The ID is used to filter the document template collection presented by SPWeb.DocTemplates property. Document Template IDs list follows:
Document Template ID | Description |
101 | A blank Microsoft Word 97-2003 document. |
103 | A blank Microsoft Excel 97-2003 document. |
104 | A blank Microsoft PowerPoint 97-2003 document. |
105 | A blank Microsoft basic page ASPX document. |
106 | A blank Microsoft Web Part Page ASPX document. |
111 | A basic Microsoft OneNote 2010 Notebook. |
121 | A blank Microsoft Word document. |
122 | A blank Microsoft Excel document. |
123 | A blank Microsoft PowerPoint document. |
Code:
using (SPSite site = new SPSite(serverUrl)) {
using (SPWeb web = site.OpenWeb()) {
SPListTemplate lstTemplate = web.ListTemplates["Document Library"];
SPDocTemplate docTemplate =(from SPDocTemplate dt in web.DocTemplates
where dt.Type == 122
select dt).FirstOrDefault();
Guid newLibID =
web.Lists.Add("Expense Claims", "Excel Expenses", lstTemplate, docTemplate);
SPDocumentLibrary newLib = web.Lists[newLibID] as SPDocumentLibrary;
newLib.OnQuickLaunch = true;
newLib.Update();
}
}
The code is for console application.(I know you are aware, just for clarity).