Add List Item To SharePoint List Using C#

This article shows how to programmatically add a list item to a SharePoint List using a Visual Web Part.

Design

  1. <%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %><%@ Assembly Name="Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %><%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %><%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %><%@ Register TagPrefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %><%@ Import Namespace="Microsoft.SharePoint" %><%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %><%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyFirstWebPartUserControl.ascx.cs" Inherits="MyFirstWebPartSolution.MyFirstWebPart.MyFirstWebPartUserControl" %>  
  2. <div>  
  3.     <asp:Label ID="lblEmployeeId" runat="server" Text="Employee ID" Width="100px" Height="50px"></asp:Label>  
  4.     <asp:TextBox ID="txtEmployeeId" runat="server" Width="150px"></asp:TextBox>  
  5.     <br />  
  6.     <asp:Label ID="lblState" runat="server" Text="State" Width="100px" Height="50px"></asp:Label>  
  7.     <asp:TextBox ID="txtState" runat="server" Width="150px"></asp:TextBox>  
  8.     <br />  
  9.     <asp:Label ID="lblCity" runat="server" Text="City" Width="100px" Height="50px"></asp:Label>  
  10.     <asp:TextBox ID="txtCity" runat="server" Width="150px"></asp:TextBox>  
  11.     <br />  
  12.     <asp:Label ID="lblFirstName" runat="server" Text="First Name" Width="100px" Height="50px"></asp:Label>  
  13.     <asp:TextBox ID="txtFirstName" runat="server" Width="150px"></asp:TextBox>  
  14.     <br />  
  15.     <asp:Label ID="lblLastName" runat="server" Text="Last Name" Width="100px" Height="50px"></asp:Label>  
  16.     <asp:TextBox ID="txtLastName" runat="server" Width="150px"></asp:TextBox>  
  17. </div>  
  18. <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />  
  19. <asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancel_Click" />  
Step 1

Create a Web Part using Visual Studio 2013.


Figure 1: Visual Studio

Step 2

Open the ascx (MyFirstWebPartUserControl.ascx) and design your UI. Here I have created Employee details information.


Figure 2: User Control


Figure 3: User Control2

Step 3

Open the C# file (MyFirstWebPartUserControl.ascx.cs) and write your functionality. Once entering the Employee information, click the Save button; the employee details information is then added to the SharePoint list.


Figure 4: .CS

Step 4

Build and deploy your solution.


Figure 5: Build

Step 5

Go to your SharePoint site and create one page under site pages. Add your web part to that page.


Figure 6: Web Part

Step 6

Enter the Employee details and click the Save button.


Figure 7: Enter employee detail

Step 7

Display the items.


Figure 8: Item will display

Code
  1. using Microsoft.SharePoint;  
  2. using System;  
  3. using System.Web.UI;  
  4. namespace MyFirstWebPartSolution.MyFirstWebPart   
  5. {  
  6.     public partial class MyFirstWebPartUserControl: UserControl   
  7.     {  
  8.         protected void Page_Load(object sender, EventArgs e) {}  
  9.         protected void btnSave_Click(object sender, EventArgs e)   
  10.         {  
  11.             using(SPSite oSite = new SPSite("http://ils-jacobr:35526")) {  
  12.                 using(SPWeb oWeb = oSite.RootWeb)   
  13.                 {  
  14.                     SPList oList = oWeb.Lists["EmployeeDetails"];  
  15.                     SPListItem oSPListItem = oList.Items.Add();  
  16.                     oSPListItem["Employee ID"] = txtEmployeeId.Text;  
  17.                     oSPListItem["City"] = txtCity.Text;  
  18.                     oSPListItem["State"] = txtState.Text;  
  19.                     oSPListItem["First Name"] = txtFirstName.Text;  
  20.                     oSPListItem["Last Name"] = txtLastName.Text;  
  21.                     oSPListItem.Update();  
  22.                 }  
  23.             }  
  24.         }  
  25.         protected void btnCancel_Click(object sender, EventArgs e)   
  26.         {  
  27.             txtEmployeeId.Text = string.Empty;  
  28.             txtCity.Text = string.Empty;  
  29.             txtState.Text = string.Empty;  
  30.             txtFirstName.Text = string.Empty;  
  31.             txtLastName.Text = string.Empty;  
  32.         }  
  33.     }  
  34. }  
Summary

In this article we have explored how to programmatically add a list item to a SharePoint List using a Visual Web Part.