共用方式為


SPListItemCollection.Add method

建立項目,但需要實際將項目加到清單的SPListItem.Update方法。

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'宣告
Public Function Add As SPListItem
'用途
Dim instance As SPListItemCollection
Dim returnValue As SPListItem

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

傳回值

Type: Microsoft.SharePoint.SPListItem
新的項目。

備註

若要加入清單中的項目,呼叫Add方法,以在清單中,使用自動產生的名稱、 根目錄中建立 nonfolder 清單項目,將傳回的值指派給SPListItem物件。指定特定值,此物件的每個欄位上使用索引子,然後在資料庫中的效果變更的項目上呼叫Update方法。

您可以使用list.RootFolder.Files.Add ,將檔案加入至文件庫, list的SPList物件的位置。

如果套用至文件庫,這個方法會傳回System.Exception例外狀況。

Examples

下列範例會將新的項目加入指定的清單。

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();
        }
    }
}

請參閱

參照

SPListItemCollection class

SPListItemCollection members

Add overload

Microsoft.SharePoint namespace