Export (0) Print
Expand All

SPListItem.ID property

Gets the integer that identifies the item.

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

public override int ID { get; }

Property value

Type: System.Int32
An integer that identifies the list item.

The value of the ID property is not the same as the index of the item in the collection of list items. This property contains the item's 1-based integer ID, which is one greater than the ID of the item that was previously added. If the item is deleted, its ID is not reused.

The ID property is invalid for an item that is created through the SPListItemCollection.Add method until it has been persisted to the database by calling the SPListItem.Update method.

The following example is a console application that iterates through a collection of list items and prints each item’s index in the collection and also the value of its ID property. Output from the application might look something like the following:

Index = 0 ID = 1
Index = 1 ID = 4
Index = 2 ID = 5
Index = 3 ID = 6
Index = 4 ID = 7

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;

                    for (int i = 0; i < items.Count; i++)
                    {
                        SPListItem item = items[i];
                        Console.WriteLine("Index = {0} ID = {1}", i, item.ID);
                    }
                }
            }
            Console.ReadLine();
        }
    }
}

Show:
© 2014 Microsoft