共用方式為


SPListItemCollection Class

Represents a collection of SPListItem objects.

Inheritance Hierarchy

System.Object
  Microsoft.SharePoint.Administration.SPAutoSerializingObject
    Microsoft.SharePoint.SPBaseCollection
      Microsoft.SharePoint.SPListItemCollection

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

Syntax

'Declaration
<ClientCallableTypeAttribute(Name := "ListItemCollection", CollectionChildItemType := GetType(SPListItem),  _
    ServerTypeId := "{1722DF25-A4D3-44bb-A1C6-04DBB90E9D91}")> _
<SubsetCallableTypeAttribute> _
Public Class SPListItemCollection _
    Inherits SPBaseCollection _
    Implements IEnumerable
'Usage
Dim instance As SPListItemCollection
[ClientCallableTypeAttribute(Name = "ListItemCollection", CollectionChildItemType = typeof(SPListItem), 
    ServerTypeId = "{1722DF25-A4D3-44bb-A1C6-04DBB90E9D91}")]
[SubsetCallableTypeAttribute]
public class SPListItemCollection : SPBaseCollection, 
    IEnumerable

Remarks

To return a collection of items from a list, it is best practice to use one of the GetItems* methods of the SPList class, instead of the Items property. For example, you can use the GetItems method of the SPList class and pass an SPQuery object to return specific items from a list. For items in document libraries, use the GetItemsInFolder method of the SPDocumentLibrary class.

Once you return a list item collection and assign it to an SPListItemCollection object, you can use an indexer to return a single item from the collection. For example, assuming the collection is assigned to a variable named collListItems, use collListItems[index] in C#, or collListItems(index) in Visual Basic, where index is either the index number of the item in the collection or the display name of a list field. Alternatively, to return a single item from the item collection in a list, use one of the Get* methods of SPList or another SharePoint object whenever possible to return specific items or files. If you use one of these methods, you do not enumerate all the items in the list or library to return the item. For example, use the GetFile method of the SPWeb class to return a specific file from a library in a Web site, or use the GetItemByUniqueId(Guid) method of the SPList class to retrieve a specific item when you know its GUID.

To create an item, you can use one of the Add() methods of the list item collection, although it’s more efficient to use the AddItem() method directly from the parent list object.

Examples

The following code example uses the GetItems method to return specific items from a source list in one site and then add these items to a destination list in another site.

Dim siteCollection As SPSite = SPControl.GetContextSite(Context)
Dim destSite As SPWeb = siteCollection.AllWebs("Destination_Site")
Dim srcSite As SPWeb = siteCollection.AllWebs("Source_Site")
Dim srcList As SPList = srcSite.Lists("Source_List")
Dim srcView As SPView = srcList.Views("Source_Niew_Name")

Dim srcItems As SPListItemCollection = srcList.GetItems(srcView)
Dim destItems As SPListItemCollection = 
    destSite.Lists("Destination_List").Items
Dim srcItem As SPListItem

For Each srcItem In  srcItems

    Dim newItem As SPListItem = destItems.Add()

    newItem("destField1_Name") = srcItem("srcField1_Name")
    newItem("destField2_Name") = srcItem("srcField2_Name")
    newItem("destField3_Name") = srcItem("srcField2_Name")

    newItem.Update()

Next srcItem 
SPSite oSiteCollection = SPContext.Current.Site;
SPWeb oWebsiteDest = oSiteCollection.AllWebs["Destination_Site"];
SPWeb oWebsiteSrc = oSiteCollection.AllWebs["Source_Site"];
SPList oList = oWebsiteSrc.Lists["Source_List"];
SPView oView = oList.Views["Source_View_Name"];

SPListItemCollection collListItemsSrc = oList.GetItems(oView);
SPListItemCollection collListItemsDest =
oWebsiteDest.Lists["Destination_List"].Items;

foreach (SPListItem oListItemSrc in collListItemsSrc)
{
    SPListItem oListItemDest = collListItemsDest.Add();

    oListItemDest["destField1_Name"] = oListItemSrc["srcField1_Name"];
    oListItemDest["destField2_Name"] = oListItemSrc["srcField2_Name"];
    oListItemDest["destField3_Name"] = oListItemSrc["srcField2_Name"];

    oListItemDest.Update();
}
oWebsiteDest.Dispose();
oWebsiteSrc.Dispose();

Note

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objects.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

SPListItemCollection Members

Microsoft.SharePoint Namespace