[WSS 3.0] Using C# to Create Custom List

[WSS 3.0] Using C# to Create Custom List

在 MSDN 上找到用 C# 新增一個 Custom List 在 MOSS 2007 上面的一段程式碼如下(註1):

SPSite siteCollection = SPContext.Current.Site; 
SPWeb mySite = SPContext.Current.Web; 
SPListTemplateCollection listTemplates = siteCollection.GetCustomListTemplates(mySite); 
SPListTemplate listTemplate = listTemplates["Custom List Template"]; 
mySite.Lists.Add("Custom List", "A list created from a custom list template in the list template catalog", listTemplate);

但是我在第4列的時候就已經出錯了,
經查詢之後得知在第3列 GetCustomListTempLates 得到的結果 listTemplates 的 Template count 就已經是0了

所以後來改以 SPTemplateType 的方法解決,整列程式碼也精簡成2列:

SPWeb mySite = SPContext.Current.Web; 
mySite.Lists.Add("Custom List", "A list created from a custom list template 
in the list template catalog", SPListTemplateType.GenericList);

SPListTemplateType 的意思及代碼如下:

InvalidType = -1Not used.
GenericList = 100Custom list
DocumentLibrary = 101Document library
Survey = 102Survey
Links = 103Links
Announcements = 104Announcements
Contacts = 105Contacts
Events = 106Calendar
Tasks = 107Tasks
DiscussionBoard = 108Discussion board
PictureLibrary = 109Picture library
DataSources = 110Data sources for a site
WebTemplateCatalog = 111Site template gallery
UserInformation = 112User Information
WebPartCatalog = 113Web Part gallery
ListTemplateCatalog = 114List template gallery
XMLForm = 115XML Form library
MasterPageCatalog = 116Master Page gallery
NoCodeWorkflows = 117No Code Workflows
WorkflowProcess = 118Custom Workflow Process
WebPageLibrary = 119Wiki Page Library
CustomGrid = 120Custom grid for a list
DataConnectionLibrary = 130 
WorkflowHistory = 140Workflow History
GanttTasks = 150Project Tasks
Meetings = 200Meeting Series (Meeting)
Agenda = 201Agenda (Meeting)
MeetingUser = 202Attendees (Meeting)
Decision = 204Decisions (Meeting)
MeetingObjective = 207Objectives (Meeting)
TextBox = 210Text Box (Meeting)
ThingsToBring = 211Things To Bring (Meeting)
HomePageLibrary = 212Workspace Pages (Meeting)
Posts = 301Posts (Blog)
Comments = 302Comments (Blog)
Categories = 303Categories (Blog)
IssueTracking = 1100Issue tracking
AdminTasks = 1200Administrator Tasks


註1:
How to: Create or Delete Lists