Click to Rate and Give Feedback

  Switch on low bandwidth view
Community Content
In this section
Statistics Annotations (5)
SPListItemCollection Class (Microsoft.SharePoint)
Represents a collection of SPListItem objects.

Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
Visual Basic (Declaration)
<DefaultMemberAttribute("Item")> _
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel:=True)> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel:=True)> _
Public Class SPListItemCollection
    Inherits SPBaseCollection
Visual Basic (Usage)
Dim instance As SPListItemCollection
C#
[DefaultMemberAttribute("Item")] 
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel=true)] 
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel=true)] 
public class SPListItemCollection : SPBaseCollection

To return the collection of items for a list or document library, use the Items property or one of the GetItems methods of the SPList class, or use the Items property or GetItemsInFolder method of the SPDocumentLibrary class. To create an item, use one of the Add methods.

Use an indexer to return a single list 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 2005, where index is either the index number of the item in the collection or the display name of a list field.

Always use a Get* method on a higher-level SharePoint object to return specific items or files. If you use one of these methods, you are not required to enumerate all the items in the list or library. For example, instead of using the SPListItemCollection object, use the GetFile method of the SPWeb class to return a specific file from a library in a Web site, or use the GetItems method of the SPList class, and pass an SPQuery object to return specific items from a list.

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

Visual Basic
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 
C#
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();
NoteNote:

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 Best Practices: Using Disposable Windows SharePoint Services Objects.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
inaccurate description of SPListItemCollection class example code      Adamx97   |   Edit   |   Show History
The article states:
The following code example uses the GetView method to return specific items from a source list in one site and then add these items to a destination list in another site.

In fact:
The example does not use the GetView method. The example uses the views indexer.

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker