Returns the list item with the specified integer ID.
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
Visual Basic (Declaration)
Public Function GetItemById ( _
id As Integer _
) As SPListItem
Dim instance As SPList
Dim id As Integer
Dim returnValue As SPListItem
returnValue = instance.GetItemById(id)
public SPListItem GetItemById (
int id
)
Parameters
- id
A 32-bit integer that identifies the item. The value of this parameter does not correspond to the index of the item within the collection of items for the list, but to the value of the ID property of the >SPListItem class.
Return Value
An SPListItem object that represents the item.
The following code example uses the GetItemById method to return the fourth item from a list and display specified field values of the item.
This example requires using directives (Imports in Visual Basic) for the Microsoft.SharePoint and Microsoft.SharePoint.Utilities namespaces.
The example assumes the existence of an .aspx page that contains a label control.
Dim siteCollection As SPSite = SPContext.Current.Site
Try
Dim list As SPList = siteCollection.AllWebs("Site_Name").Lists("List_Name")
Dim listItem As SPListItem = list.GetItemById(4)
Label1.Text += SPEncode.HtmlEncode(listItem("Field1_Name").ToString()) +
" -- " + SPEncode.HtmlEncode(listItem("Field2_Name").ToString()) + " -- " +
SPEncode.HtmlEncode(listItem("Field3_Name").ToString())
Finally
siteCollection.Dispose()
End Try
SPSite oSiteCollection = SPContext.Current.Site;
SPList oList = oSiteCollection.AllWebs["Site_Name"].Lists["List_Name"];
SPListItem oListItem = oList.GetItemById(4);
Label1.Text += SPEncode.HtmlEncode(oListItem["Field1_Name"].ToString())
+ " -- " + SPEncode.HtmlEncode(oListItem["Field2_Name"].ToString())
+ " -- " +
SPEncode.HtmlEncode(oListItem["Field3_Name"].ToString());