Introduction
Create list item, Update list item, Read data from list, Delete list item in SharePoint using C#
Code
Create list item, Update list item, Read data from list, Delete list item in SharePoint using C#
Code
- .ascx page code with some basic fields
- <table>
- <tr>
- <td> </td>
- <td>List CRUD</td>
- <td> </td>
- </tr>
- <tr>
- <td>Title</td>
- <td>
- <asp:TextBox ID="txtTitle" runat="server"></asp:TextBox>
- </td>
- <td> </td>
- </tr>
- <tr>
- <td>Name</td>
- <td>
- <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
- </td>
- <td> </td>
- </tr>
- <tr>
- <td>Age</td>
- <td>
- <asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
- </td>
- <td> </td>
- </tr>
- <tr>
- <td>Update</td>
- <td>
- <asp:TextBox ID="txtUpdate" runat="server"></asp:TextBox>
- </td>
- <td> </td>
- </tr>
- <tr>
- <td> </td>
- <td>
- <asp:Button ID="btnAdd" runat="server" OnClick="btnAdd_Click" Text="Add" Width="75px" />
- <asp:Button ID="btnCancel" runat="server" Text="Cancel" Width="75px" />
- <asp:Button ID="btnUpdate" runat="server" OnClick="btnUpdate_Click" Text="Update" Width="75px" />
- <asp:Button ID="btnDelete" runat="server" OnClick="btnDelete_Click" Text="Delete" Width="75px" />
- <asp:Button ID="btnRetrieve" runat="server" OnClick="btnRetrieve_Click" Text="Retrieve" Width="75px" />
- </td>
- <td> </td>
- </tr>
- </table>
- .ascx.cs Page code
- protected void btnAdd_Click(object sender, EventArgs e)
- {
- //Create SPSite object
- using (SPSite RootSite = new SPSite(SPContext.Current.Site.Url))
- {
- //create SPWeb object
- using (SPWeb RootWeb = RootSite.OpenWeb())
- {
- //create splist object
- SPList spList = RootWeb.Lists["ListCRUD"]; //ListCrud is a sharepoint list name
- SPListItem spListItem = spList.AddItem(); //create splistitem object to add item
- spListItem["Title"] = txtTitle.Text;
- spListItem["Name"] = txtName.Text;
- spListItem["Age"] = Convert.ToInt32(txtAge.Text);
- spListItem.Update(); //item update
- }
- }
- }
- protected void btnUpdate_Click(object sender, EventArgs e)
- {
- using (SPSite RootSite = new SPSite(SPContext.Current.Site.Url))
- {
- using (SPWeb RootWeb = RootSite.OpenWeb())
- {
- int id = Convert.ToInt32(txtUpdate.Text);
- SPList spList = RootWeb.Lists["ListCRUD"];
- SPListItem spListItem = spList.GetItemById(id);
- spListItem["Title"] = txtTitle.Text;
- spListItem["Name"] = txtName.Text;
- spListItem["Age"] = Convert.ToInt32(txtAge.Text);
- spListItem.Update();
- }
- }
- }
- protected void btnRetrieve_Click(object sender, EventArgs e)
- {
- using (SPSite RootSite = new SPSite(SPContext.Current.Site.Url))
- {
- using (SPWeb RootWeb = RootSite.OpenWeb())
- {
- int id = Convert.ToInt32(txtUpdate.Text);
- SPList spList = RootWeb.Lists["ListCRUD"];
- SPQuery spQuery = new SPQuery();
- spQuery.Query = string.Concat("<Where>",
- "<Eq>",
- "<FieldRef Name='ID' />",
- "<Value Type='Counter'>" + id + "</Value>",
- "</Eq>",
- "</Where>");
- spQuery.ViewFieldsOnly = true;
- spQuery.ViewFields = @"<FieldRef Name='Title' /><FieldRef Name='Name' /><FieldRef Name='Age' />";
- SPListItemCollection spListColl = spList.GetItems(spQuery);
- DataTable dt = new DataTable();
- dt = spListColl.GetDataTable();
- txtTitle.Text = dt.Rows[0]["Title"].ToString();
- txtName.Text = dt.Rows[0]["Name"].ToString();
- txtAge.Text = dt.Rows[0]["Age"].ToString();
- }
- }
- }
- protected void btnDelete_Click(object sender, EventArgs e)
- {
- using (SPSite RootSite = new SPSite(SPContext.Current.Site.Url))
- {
- using (SPWeb RootWeb = RootSite.OpenWeb())
- {
- int id = Convert.ToInt32(txtUpdate.Text);
- SPList spList = RootWeb.Lists["ListCRUD"];
- SPListItem spListItem = spList.GetItemById(id);
- spListItem.Delete();
- spList.Update();
- }
- }
- }