Adding a list is done very much in the same way as
a web. You just have to pay attention to were you should get your list template
from, because depending if it is a native or a custom list template the place where
to get it is different (
SPWeb.ListTemplates vs SPSite.GetCustomListTemplates). Of course that a list can only be added to a web.

So a function to add a list could be like the one
below.

public
SPList AddList(SPWeb parentWeb, String title, String description, String
listTemplate, Boolean onQuickLaunch)
{
    // Get list template object
    SPListTemplate listTemplateSP = null; 

    // Try out-of-box list templates first (Not be best way of doing this)
    try
    {
        listTemplateSP =
parentWeb.ListTemplates[listTemplate];
    }
    catch (System.ArgumentException)
    { // Try custom site templates
        SPListTemplateCollection customListTemplateCollection
= parentWeb.Site.GetCustomListTemplates(parentWeb);
        listTemplateSP =
customListTemplateCollection[listTemplate];
    } 

    if (listTemplateSP == null)
        // Could not found
        throw new Exception("AddList: Could not found
list template"); 

    // Ok. Add list
    Guid newListGuid = parentWeb.Lists.Add(title, description,
listTemplateSP); 

    // Get new list
    SPList newList = parentWeb.Lists[newListGuid];
    newList.OnQuickLaunch = onQuickLaunch; 

    // Return
    return newList;

}

LEAVE A REPLY

Please enter your comment!
Please enter your name here