Access the SharePoint List Programmatically

By Devil Scorpio

Access the Sharepoint List Programmatically .... like Add New Item into the List, Add New List and Delete the existing sharepoint List

Problem Description

Access the Sharepoint List Programmatically .... like Add New Item into the List, Add New List and Delete the existing sharepoint List

Solution Description

Access the Sharepoint List Programmatically .... like Add New Item into the List, Add New List and Delete the existing sharepoint List


Code Snippet

-----------------------------------------------------------------------------------------------------------------------------------
* * * Add List Items to the Sharepoint List Programmatically * * *
-----------------------------------------------------------------------------------------------------------------------------------
SPSite site = new SPSite("<<Sharepoint Website Name>>");
SPWeb web = site.AllWebs[0];

//"Tasks" is the name of the List
SPList list = web.Lists["Tasks"];

SPListItem newItem = list.Items.Add();

// Add the required data into the list
newItem["Title"] = "Creating New Task";
newItem["Status"] = "Not Started";
newItem["Priority"] = "Normal";

//Save the Item Added to the list
newItem.Update();

-----------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------
* * * Create Sharepoint List Programmatically * * *
-----------------------------------------------------------------------------------------------------------------------------------

SPSite site = new SPSite("<<Sharepoint Website Name>>");
SPWeb web = site.AllWebs[0];

// Create the New List as named "TestTaskList" based on the Task List template
SPListTemplate temps = web.ListTemplates["Tasks"];
Guid newListGuid = web.Lists.Add("TestTaskList", "This List Added Programmatically", temps);

SPList newList = web.Lists[newListGuid];

-----------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------
* * * Delete Sharepoint List Programmatically  * * *
-----------------------------------------------------------------------------------------------------------------------------------

SPSite site = new SPSite("<<Sharepoint Website Name>>");
SPWeb web = site.AllWebs[0];

// Select list which we need to delete e.g. "TestTaskList"
SPList list = web.Lists["TestTaskList"];

// Delete the list
list.Delete();

-----------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------

Access the SharePoint List Programmatically  (1701 Views)