Monday, March 4, 2013

Create list in SharePoint 2010 using Server Object Model


Here is the code to create a SharePoint list by using SharePoint 2010 server object model.

SPWeb site = SPContext.Current.Web;

string ListTitle = "Our List Name";

string ListDescription = "This is our description for our list";

Guid ListId = site.Lists.Add(ListTitle, ListDescription, 

SPListTemplateType.Contacts); //We are using Contacts list template for our list.

SPList list = site.Lists[ListId];

list.OnQuickLaunch = true; // This is to show the list in the quick launch bar.

list.Update();

Creating a SharePoint List with a List Template Type



To create a new list, use one of the Add() method overloads of the SPListCollection class.
The following example adds a new Generic, Events, or Announcements list, based on user input. A Switch clause is used to determine the type of list that the user specifies and sets the type of list template accordingly.

SPWeb mySite = SPContext.Current.Web;
SPListCollection lists = mySite.Lists;

string listTitle = TextBox1.Text;
string listDescription = TextBox2.Text;
string listType = ListBox1.SelectedItem.Text;

SPListTemplateType listTemplateType = new SPListTemplateType();

switch(listType)
{
    case "Generic List":
    {
        listTemplateType = SPListTemplateType.GenericList;
        break;
    }

    case "Events":
    {
        listTemplateType = SPListTemplateType.Events;
        break;
    }

    case "Announcements":
    {
        listTemplateType = SPListTemplateType.Announcements;
        break;
    }
}

lists.Add(listTitle, listDescription, listTemplateType);
The example instantiates an SPListTemplateType object in order to contain the type of list template that is specified by the user. This object must be passed as a parameter in the Add(String, String, SPListTemplateType) method. The example assumes the existence of two text boxes where the user can type a title and a description, as well as a drop-down list that displays the list types for the user to select from.



In addition to using the SPListTemplateType enumeration to create a list from a template type, you can also create a list from an SPListTemplate object, which represents either a specific list template that has been created in the UI by saving an existing list as a template, or a list schema in %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\FEATURES that defines a list type. The ListTemplates property of the SPWeb class can be used to return a collection of list template objects and a name indexer can be used to specify the list template to use. This is shown in the following example, which assumes the existence of a Decision Meetings Workspace site.

SPWeb mySite = SPContext.Current.Web;

SPListTemplate template = mySite.ListTemplates["Decisions"];
mySite.Lists.Add("My Decisions", "This is a list of decisions",
   template);
Using the GetCustomListTemplates method of the SPSite class, the next example returns the custom list templates for a specified site and creates a new list that is based on a specified list template.
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);
You can add a list to multiple Web sites across a site collection, as seen in the following example, which creates a generic list on every Web site, based on the title and description that is passed from two text boxes to the code. The AllWebs property of the SPSite class is used to return the collection of all Web sites that exist on the site.
The example assumes the existence of two text boxes on the .aspx page that contains a form.

string listTitle = TextBox1.Text.ToString();
string listDescription = TextBox2.Text.ToString();
SPSite mySite = SPContext.Current.Site;
SPWebCollection allWebs = mySite.AllWebs;

foreach (SPWeb web in allWebs){   
    SPListCollection allLists = web.Lists;
    allLists.Add(listTitle,listDescription, SPListTemplateType.GenericList);
}


To delete a list, you must specify the GUID of the list as the parameter for the Delete method. Use the ID property of the SPList class to find the GUID.

SPWeb mySite = SPContext.Current.Web;
SPListCollection lists = mySite.Lists;

SPList list = lists[TextBox1.Text];
System.Guid listGuid = list.ID;

lists.Delete(listGuid);
The previous example assumes the existence of a text box in which the user specifies the name of the list.
To delete a list from multiple Web sites, the following example uses nested loops to drill down to a list that has a title matching the title specified in a text box. The example assumes the existence of a text box on the .aspx page that contains a form.
SPSite mySite = SPContext.Current.Site;
SPWebCollection allWebs = mySite.AllWebs;

foreach (SPWeb web in allWebs){   
    SPListCollection allLists = web.Lists;
  
    for (int i=0; i<allLists.Count; i++)   
    {       
        SPList list = allLists[i];       

        if (list.Title == TextBox1.Text)       
        {           
            Guid listGuid = list.ID;           
            allLists.Delete(listGuid);       
        }   
}}
In the example the Title property of the SPList class is used to identify a list in the collection of lists for each Web site that matches the specified title. The ID property returns the globally unique identifier (GUID) of the list, which is passed as the parameter for the Deletemethod.
The previous examples require a using directive (Imports in Microsoft Visual Basic) for the Microsoft.SharePoint namespace.

No comments:

Post a Comment