RSS

Tag Archives: code

Programatically Insert Items to Sharepoint 2010 List using C#.net


  • 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

try
{

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;

NewItem.Update();

//int newID = NewItem.ID;

web.AllowUnsafeUpdates = false;

lblInfo.Text = “Item Inserted Successfully.”;

}
}
}

}

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

}
}

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

Advertisements
 
2 Comments

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

 

Tags: , ,