Insert Items Into SharePoint List Programmatically

Create a new team site in SharePoint.

web part

Create a new list named "Employee Registration"

Create a new list

After the list has been created you need to create columns for saving employee details.

saving employee details

Now the columns have been created successfully.

Next we need to create a new “Empty SharePoint project” in Microsoft Visual Studio named “EmployeeReg”. Click OK.

EmployeeReg

Validate the site. After the validation successful, Deploy as a Farm solution.

Click Finish.

Click Finish

Now right-click on the solution, click Add and select new item. Create a “New Visual Webpart” named “EmployeeRegistration”.

add new item

Webpart

EmployeeRegistration

Now Add the Fields and labels into Employee Registration form.

Employee Registration form

Double-click on the register button to add the code.

Use this namespace.

  1. using Microsoft.SharePoint;  
Use code

Code

Full Code
  1. using Microsoft.SharePoint;  
  2. using System;  
  3. using System.ComponentModel;  
  4. using System.Web.UI.WebControls.WebParts;  
  5.   
  6. namespace EmployeeReg.EmployeeRegistration  
  7. {  
  8.     [ToolboxItemAttribute(false)]  
  9.     public partial class EmployeeRegistration : WebPart  
  10.     {  
  11.         // Uncomment the following SecurityPermission attribute only when doing Performance Profiling on a farm solution  
  12.         // using the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready  
  13.         // for production. Because the SecurityPermission attribute bypasses the security check for callers of  
  14.         // your constructor, it's not recommended for production purposes.  
  15.         // [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]  
  16.         public EmployeeRegistration()  
  17.         {  
  18.         }  
  19.   
  20.         protected override void OnInit(EventArgs e)  
  21.         {  
  22.             base.OnInit(e);  
  23.             InitializeControl();  
  24.         }  
  25.   
  26.         protected void Page_Load(object sender, EventArgs e)  
  27.         {  
  28.         }  
  29.   
  30.         protected void btnregister_Click(object sender, EventArgs e)  
  31.         {  
  32.                 try  
  33.             {  
  34.                 using(SPSite site = new SPSite(SPContext.Current.Web.Url))  
  35.                 {  
  36.                     using(SPWeb web = site.OpenWeb())  
  37.                     {  
  38.                         SPList list = web.Lists.TryGetList("Employee Registration");  
  39.                         if(list!=null)  
  40.                         {  
  41.                             SPListItem NewItem = list.Items.Add();  
  42.                             {  
  43.                                 web.AllowUnsafeUpdates = true;  
  44.                                 NewItem["Employee Name"] = txtempname.Text;  
  45.                                 NewItem["Designation"] =drpdesg.SelectedItem.ToString();  
  46.                                 NewItem["Address"] =txtaddr.Text;  
  47.                                 NewItem["Email"] =txtemail.Text;  
  48.                                 NewItem["Contact No"] = txtcontact.Text;  
  49.                                 NewItem.Update();  
  50.                                 Alert.Text = "Registration Successful";  
  51.                                  
  52.                             }  
  53.                         }  
  54.                         else  
  55.                         {  
  56.                             Alert.Text = "List not found";  
  57.                         }  
  58.                     }  
  59.                 }  
  60.               
  61.              
  62.             }  
  63.             catch(Exception ex)  
  64. {  
  65.                 Alert.Text = ex.Message.ToString();  
  66. }  
  67.         }  
  68.   
  69.         protected void btnclear_Click(object sender, EventArgs e)  
  70.         {  
  71.             txtempname.Text = "";  
  72.             txtemail.Text = "";  
  73.             drpdesg.SelectedIndex = -1;  
  74.             txtcontact.Text = "";  
  75.             txtaddr.Text = "";  
  76.             Alert.Text = "";                    
  77.         }  
  78.     }  
  79. }  
Now build the project and then Deploy solution.

Deploy solution

Go to the site and edit the page, then add a Webpart from the custom tab.

custom tab

Now Register with some details.

Register

The data has been added into the list.

data

Now click Clear.

All the fields are cleared successfully.

Click Clear

I hope this will help SharePoint beginners to start developing custom webparts.