다음을 통해 공유


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

Syntax

'Declaration
Public Function Add As SPListItem
'Usage
Dim instance As SPListItemCollection
Dim returnValue As SPListItem

returnValue = instance.Add()
public SPListItem Add()

Return Value

Type: Microsoft.SharePoint.SPListItem
The new item.

Remarks

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.

Examples

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

Imports System
Imports Microsoft.SharePoint

Module Test

    Sub Main()
        Using site As SPSite = New SPSite("https://localhost")
            Using web As SPWeb = site.OpenWeb()

                Dim list As SPList = web.GetList("/lists/announcements")
                Dim items As SPListItemCollection = list.Items

                Dim item As SPListItem = 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()

                Dim id As Guid = item.UniqueId
                Console.WriteLine(items(id).Title)

            End Using
        End Using
        Console.ReadLine()
    End Sub

End Module
using System;
using Microsoft.SharePoint;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("https://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();
        }
    }
}

See Also

Reference

SPListItemCollection Class

SPListItemCollection Members

Add Overload

Microsoft.SharePoint Namespace