SPListItemCollection.Add Method

Creates an item but requires the SPListItem.Update method to actually add the item to the list.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

public SPListItem Add()

Return Value

Type: Microsoft.SharePoint.SPListItem
The new item.

To add an item to a list, call the Add method to create a nonfolder list item in the root of the list with an automatically generated name, and assign the return value to an SPListItem object. Use indexers on this object for each field to assign specific values, and then call the Update method on the item to effect changes in the database.

Use list.RootFolder.Files.Add to add files to a document library, where list is an SPList object.

This method returns a System.Exception exception if applied to a document library.

The following example adds a new item to a specified list.

using System;
using Microsoft.SharePoint;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://localhost"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.GetList("/lists/announcements");
                    SPListItemCollection items = list.Items;

                    SPListItem item = items.Add();
                    item[SPBuiltInFieldId.Title] = "A new item!";
                    item[SPBuiltInFieldId.Body] = "This item was added by a call to the Add method.";
                    item[SPBuiltInFieldId.Expires] = DateTime.Now.AddHours(1);
                    item.Update();

                    Guid id = item.UniqueId;
                    Console.WriteLine(items[id].Title);
                }
            }
            Console.ReadLine();
        }
    }
}
Show: