Thursday, January 28, 2010

Get GUID of SPListItem

Category: Working with SPListItem

Scenario: I was trying to check a column value in splistitem in an Asynchronous event handler.
Since it is asynchronous, the values had already changed & the before state is unaccessible through properties object.

Problem: How to get GUID of a SPListItem so that you can access list item values before the event handler updates it

Solution: use SPListItem.UniqueId

Example:

Guid SiteGuid = (Guid)properties.SiteId;
string weburl = (string)properties.WebUrl;
using (SPSite tempsite = new SPSite(SiteGuid))
{
using (SPWeb tempweb = tempsite.OpenWeb(()))
{
SPList templist = tempweb.Lists[];
Guid templistitemID = (Guid)properties.ListItem.UniqueId;  //get GUID of splistitem
SPListItem templistitem = templist.Items[templistitemID]; //for accessing splistitem values
}
}

No comments: