In this article I am explaining about how to Add Items to the Sharepoint List Programmatically.
Adding or Manipulating the Items for a Sharepoint List is very easy. Sharepoint allows you to access Sharepoint List Items by default.
Sharepoint List may contain various fileds (Columns). It allows to access and manipulate their Items. By using Items indexer property you can access and manipulate sharepoint List Items.
To Add Item to the Sharepoint List Programmatically
Open Visual studio - File - Create New project
then select C# as the language and Console Application from the panel.
Give AddListItem as name for project, then click ok.
After Creating the project, you need to add the Sharepoint DLL for your project.
For this right click on solution Explorer and select Add Reference . Browse for Microsoft.Sharepoint.dll file and click ok.
With default installation of Sharepoint, you can find this dll file at below location.
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI\Microsoft.SharePoint.dll
So, you successfully establish the environment to Add Item to the Sharepoint List.
The code to Add Item to the Sharepoint List is shown below.
using System; using System.Collections.Generic; using System.Text; using Microsoft.SharePoint; namespace AddListItem { class Program { static void Main(string[] args) { SPSite site = new SPSite("http://home-pt72rbbzr0"); SPWeb web = site.AllWebs[0]; SPList list = web.Lists["Tasks"]; SPListItem newItem = list.Items.Add(); newItem["Title"] = "Creating New Task"; newItem["Status"] = "Not Started"; newItem["Priority"] = "Normal"; newItem.Update(); Console.WriteLine("New List Item Added Successfully"); Console.ReadLine(); } } }
In the above code, first we create the object for the SPSite by giving it's Sharepoint site URL. In my System site URL is http://home-pt72rbbzr0 by default installation. It may differ in your System.
After that I refer the first web of the site, from that I am useing the "Tasks" List to add Items. Here we are using Add method for the Items property of the list. By using Add method we create the new Item and by refereing the index property of this new item we assign the values for the various columns of the list(here Tasks list).
After assigning the values we have to update list by using Update method for the new List Item.
In this way we develop the code to add new List Item to the List.Then we need to deploy it on server.
If the production server is same as develop server, then just press F5, if not you need to copy the entire project to production server and install or deploying your code using various deployment methods in .Net Framework and install on server.
After completing the installation, you will get the following window.
open new Browser window with the new site URL(i.e. http://home-pt72rbbzr0), and go to the Tasks List of first web. Then you observe Item just we added programmaticall for the Tasks List as shown below.
In this way you can add Items to any Sharepoint List Programmatically.