Add,Update and Delete in SharePoint2013 List.
In this article am going to discuss about insert, edit, delete, update operations by using C# in SharePoint 2013.
To proceede with this example, create a list derived from the CustomList template and name it "MyContacts".
Add some items inside it with the above Columns..
Add some items inside it with the above Columns..
Example:
1.Title 2.Name,3.Address,4.ContactNo
5.Country
Coming To Development part:
Open VisualStudio2012->New Project
->Select Sharepoint2013 EmptyProject
Give the Meaningfull Name and click on ok.
Step2:
Right Click on Solution Explorer and select Add-> NewItem .
From the Templates Select the Visual Webpart and give the Meaningful Name and Click on OK.
Step3:
Select the webpart.ascx
->Paste the HTML tags in Source.
<table>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Title"></asp:Label></td>
<td>
<asp:TextBox ID="textBox1" runat="server" Width="152px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="Name"></asp:Label>
</td>
<td>
<asp:TextBox ID="textBox2" runat="server" Width="152px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label4" runat="server" Text="Address"></asp:Label>
</td>
<td>
<asp:TextBox ID="textBox3" runat="server" Width="152px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label5" runat="server" Text="ContactNo"></asp:Label>
</td>
<td>
<asp:TextBox ID="textBox4" runat="server" Width="152px"></asp:TextBox></td>
</tr>
<tr>
<td>
<asp:Label ID="Label6" runat="server" Text="Country"></asp:Label></td>
<td>
<asp:TextBox ID="textBox5" runat="server" Width="152px"></asp:TextBox></td>
</tr>
<tr>
<td><asp:Button ID="Button1" runat="server" Text="ADD" OnClick="BtnAdd_Click" Width="67px" /></td>
<td><asp:Button ID="Button5" runat="server" Text="UPDATE" OnClick="BtnUpdate_Click" Width="67px" />
<asp:Button ID="Button7" runat="server" Text="DELETE" OnClick="BtnDelete_Click" Width="67px" />
</td>
<td> </td>
</tr>
</table>
Use the following code inside the Button Events and execute it.
Double Click on ADD button
protected void BtnAdd_Click(object sender, EventArgs e)
{
using (SPSite site = new SPSite("URL"))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList list = web.Lists["MyContacts"];
SPListItem Item = list.Items.Add();
Item["Title"] = textBoxTitle.Text;
Item["Name"] = textBoxName.Text;
Item["Address"] = textBoxAddress.Text;
Item["ContactNo"] = textBoxContactNo.Text;
Item["Country"] = textBoxCountry.Text;
textBoxTitle.Text = "";
textBoxName.Text = "";
textBoxAddress.Text = "";
textBoxContactNo.Text = "";
textBoxCountry.Text = "";
Item.Update();
web.AllowUnsafeUpdates = false;
}
}
}
For Update Button:
protected void Button3_Click(object sender, EventArgs e)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite("http://kranthi:57/enterprise/ETemplates"))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList list = web.Lists["MyContacts"];
SPQuery editq = new SPQuery();
editq.Query = @"<where>
<Eq>
<Feild Ref='Name'/>
<value Type='Text'>" + textBoxName + "</Value></Eq></where>";
SPListItemCollection Items = list.GetItems(editq);
if (Items.Count != 0)
{
foreach (SPListItem Item in Items)
{
Item["Title"] = textBoxTitle.Text;
Item["Address"] = textBoxAddress.Text;
Item["ContactNo"] = textBoxContactNo.Text;
Item["Country"] = textBoxCountry.Text;
Item.Update();
}
}
}
}
});
}
For Delete Button:
protected void Button4_Click(object sender, EventArgs e)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite("http://kranthi:57/enterprise/ETemplates"))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList list = web.Lists["MyContacts"];
SPQuery editq = new SPQuery();
editq.Query = @"<where>
<Eq>
<Feild Ref='Name'/>
<value Type='Text'>" + textBoxName + "</Value></Eq></where>";
SPListItemCollection Items = list.GetItems(editq);
if (Items.Count != 0)
{
foreach (SPListItem Item in Items)
{
//Item["Title"] = textBoxTitle.Text;
//Item["Address"] = textBoxAddress.Text;
//Item["ContactNo"] = textBoxContactNo.Text;
//Item["Country"] = textBoxCountry.Text;
Item.Delete();
}
}
}
}
});
}
Once the build succeeded deploy the solution by right click on the solution name.
Follow the below steps for adding Custom Web part;
Enter the URL of particular Site collection->Select the Edit Page From the Settings.
Select the Custom Category and select the Solution and click on Add button.
And Select the Stop Editing button from the TOP-Ribbon and Save it.
Here we can findout out webpart form and enter some text and click on add button.
Then those items will saved in our list.
