Compartilhar via


SPListItem.ID Property

Gets the integer that identifies the item.

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

Syntax

'Declaration
<ClientCallableAttribute(Name := "Id")> _
Public Overrides ReadOnly Property ID As Integer
    Get
'Usage
Dim instance As SPListItem
Dim value As Integer

value = instance.ID
[ClientCallableAttribute(Name = "Id")]
public override int ID { get; }

Property Value

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

Remarks

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.

Examples

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
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 i As Integer
                For i = 0 To items.Count - 1 Step i + 1
                    Dim item As SPListItem = items(i)
                    Console.WriteLine("Index = {0} ID = {1}", i, item.ID)
                Next

            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;

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

See Also

Reference

SPListItem Class

SPListItem Members

Microsoft.SharePoint Namespace