Programatically Insert Items to Sharepoint 2010 List using

14 Nov

  • Open Visual Studio 2010
  • Create New Project
  • Select Your Sharepoint site
  • Create New Visual Webpart
  • Add Textbox,Label and Button as required(NOTE: Controls names You need to update in below code)
  • Add below code in button click event the code


using (SPSite site = new SPSite(SPContext.Current.Web.Url))
using (SPWeb web = site.OpenWeb())

SPList List = web.Lists.TryGetList(“—ADD LIST NAME—“);
if (List != null)

SPListItem NewItem = List.Items.Add();

web.AllowUnsafeUpdates = true;

NewItem[“Title”] = Label1.Text;
NewItem[“Employee Name”] = Label2.Text;
NewItem[“Comments”] = Textbox1.Text;


//int newID = NewItem.ID;

web.AllowUnsafeUpdates = false;

lblInfo.Text = “Item Inserted Successfully.”;



lblErrorMessage.Text = “List not found. Configure the list from webpart settings”;


catch (Exception ex)
lblErrorMessage.Text = ex.Message.ToString();


Posted by on November 14, 2012 in Code Snippets, Uncategorized, Visual web part


Tags: , ,

2 responses to “Programatically Insert Items to Sharepoint 2010 List using

  1. Riezal

    July 23, 2013 at 1:25 PM

    Hi jim, i’m really a beginner to can you please explain a bit on the step above (Select Your Sharepoint site; Create New Visual Webpart …)? From your given step, how to select SharePoint site? TQ.

  2. Riezal

    August 6, 2013 at 12:02 PM

    Hi Jim, may i know how is step 3 (Select Your Sharepoint site)? Can you please explain it a little bit? I’m a beginner in Thanks in advance…


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: