Programmatically insert an item in sharepoint list using ASP.NET user control

21 Apr

  • Create a new web project in Visual studio
  • Add reference to Microsoft.SharePoint.dll  (you can find from C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI)
  • Create an new user control(.ascx)
  • Import below namespaces
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Search;
    using Microsoft.SharePoint.WebControls;
    using Microsoft.SharePoint.WebPartPages;
  • Insert Textboxes, buttons….whatever u required
  • On button click paste the code below
  • To host the user control in sharepoint use smartpart from codeplex.




                //create the object of SPSite
             SPSite oSiteCollection = SPContext.Current.Site;

           // SPSite oSiteCollection = new SPSite(“http://mysite:3434“);

                //create the object of SPWeb with the help of SPSite object:
                SPWeb oWebsiteRoot = oSiteCollection.OpenWeb();
                oWebsiteRoot.AllowUnsafeUpdates = true;

                //create the connection to the custom List and create object of SPListItemCollection
                SPList oList = oWebsiteRoot.Lists[“MyListName”];
                SPListItemCollection listItemCOll;

                //add the item in the User List
                SPListItem oListItem = oList.Items.Add();
                oListItem[“Name”] = txtName.Text;
                oListItem[“Email”] = txtEmail.Text;
                oListItem[“Color”] = ddldropdown.SelectedItem;
                oListItem[“AssignTo”] = txtAssignto.Text;

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



Posted by on April 21, 2010 in ASP.NET, Code Snippets


Tags: ,

16 responses to “Programmatically insert an item in sharepoint list using ASP.NET user control

  1. Dimitris Porikos

    May 12, 2010 at 2:26 PM

    Thank you Jim.
    There is a bug in your code: you should always dispose SPSite and SPWeb objects or you can use the using statement for automatic disposing.

  2. Bruce Wexler

    May 30, 2010 at 12:10 PM


    I have a simple user control setup to work with smartpart to save a list item to a custom list. I’ve successfully built the assembly, installed it into the GAC with a strong name and placed the .dll and .ascx files in their proper places in the site’s virtual directory. I can edit the page, place the web part with the user control on the page fine and the submission form displays. I’ve checked the internal field names in the form and made sure the fields in my code are aligned with the list fields. I have the error handling in the page itself set to return an error as shown below, and as well I have the following in the web.config to catch errors:

    I am using the SPListItem in the Sharepoint object model as follows:

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;
    using Microsoft.SharePoint.WebPartPages;

    namespace ServiceRequests
    public partial class Service_Request : System.Web.UI.UserControl
    protected void Page_Load(object sender, EventArgs e)

    protected void Button1_Click(object sender, EventArgs e)
    //create the object of SPSite
    //SPSite oSiteCollection = SPContext.Current.Site;

    SPSite oSiteCollection = new SPSite(“http://testvpc:1111/sandbox”);
    //entire site collection at top level or top level site?

    //create the object of SPWeb with the help of SPSite object:
    SPWeb oWebsiteRoot = oSiteCollection.OpenWeb();
    oWebsiteRoot.AllowUnsafeUpdates = true;

    //create the connection to the custom List and create object of SPListItemCollection
    SPList oList = oWebsiteRoot.Lists[“SubmissionTest”];

    //SPListItemCollection listItemColl;

    //add the item in the Task List
    SPListItem oListItem = oList.Items.Add();
    oListItem[“Title”] = field5.Text;
    oListItem[“Field1”] = field1.Text;
    oListItem[“Field2”] = field2.Text;
    oListItem[“Field3”] = DropDownList1.SelectedItem.Value;
    oListItem[“Field4”] = field4.Text;
    // Sharepoint list is case sensitive


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

    When I submit to add a new list item I am coming up against this error:

    Value does not fall within the expected range.

    Can someone please explain what I might be missing. I am trying to get this simple test to work so I can build upon this, but I am definitely stuck at this point. I even reduced my test form to two fields, “Title” and “field1” with the two columns named the same on the custom list. Any help would be greatly appreciated, I think I’m pretty close.



    • Manoj

      June 28, 2011 at 1:46 PM

      Value does not fall within the expected range.

      Thia error generally shows when the Field name is different as in List.

      ex-In List the Field is- Name
      and you r inserting as -oListItem[“name”] = field2.Text.


  3. Ulf

    August 2, 2011 at 10:41 AM

    Depending on your needs and complicity of the application, I would recommend you to have a look at the Camelot .NET Connector, which lets you insert, update and delete items as if sharepoint was a typical sql server. So in your first example that would be:

    var connection = new SharePointConnection(@”


    var command = new SharePointCommand(@”UPDATE `MyListName` SET Name = ‘yourname’, Email = ‘your email’, Color = ‘yourcolor’, AssignTo = ‘your assigned to'”, connection);


    • Jim Mathew

      August 2, 2011 at 11:48 AM

      Thanks for your suggestion…i’ll try it

      • vikalp

        January 20, 2012 at 4:38 PM

        Nice One Its very Helpful

  4. Mir Hassan Shahani

    September 18, 2012 at 9:37 PM

    Pragmatically Add Items into Sharepoint List

    1) Add Visual Webpart from Visual Studio 2010
    2) Insert the Code into button Click Event
    3) Deploy

    public partial class InsertEnquiryToSharepoint: UserControl
    protected void Page_Load(object sender, EventArgs e)


    protected void InsertEnquiryToSharepoint()
    using (SPSite oSite = new SPSite(“http://localhost”))
    using (SPWeb oWeb = oSite.RootWeb)
    SPList oList = oWeb.Lists[“EnquiryForm”];
    SPListItem oSPListItem = oList.Items.Add();
    oSPListItem[“Title”] = txtcomanyname.Text;
    oSPListItem[“Description”] = TextBox1.Text;
    oSPListItem[“DateReceived”] = System.DateTime.Now;




    protected void btn_Click(object sender, EventArgs e)


    October 22, 2012 at 8:32 PM

    I will share this article

  6. Jamey

    October 29, 2012 at 3:26 AM

    Hurrah! Finally I got a webpage from where I be able to
    actually take valuable information concerning my study and knowledge.

  7. raspberry ketone

    October 31, 2012 at 7:21 AM

    Whoa! This blog looks just like my old one! It’s on a entirely different topic but it has pretty much the same layout and design. Great choice of colors!

  8. natural liver cleanse

    November 1, 2012 at 4:28 PM

    I am sure this article has touched all the internet users,
    its really really good piece of writing on building up new webpage.

  9. Famous Jazz Singers

    November 8, 2012 at 4:29 AM

    Hello there I am so thrilled I found your site, I really found you by mistake, while I was researching
    on Digg for something else, Anyways I am here now and would just
    like to say thank you for a marvelous post and a all round enjoyable blog (I also love the theme/design), I don’t have time to browse it all at the moment but I have bookmarked it and also added in your RSS feeds, so when I have time I will be back to read more, Please do keep up the excellent job.

  10. cold call pressure

    November 23, 2012 at 5:48 PM

    It’s great that you are getting thoughts from this article as well as from our discussion made at this time.

  11. a replacement

    November 28, 2012 at 10:10 AM

    What’s up to every body, it’s my first visit of this weblog;
    this web site carries remarkable and truly fine data for visitors.

  12. Priyadarshini Soman

    February 15, 2013 at 1:16 PM

    This is absolutely great!!! Thankyou for putting this online.

  13. Gearxs Discount Code

    July 21, 2013 at 9:31 PM

    I used to be suggested this web site by way of my cousin.
    I’m not positive whether or not this put up is written through him as no one else understand such specified about my trouble. You are wonderful! Thanks!


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: