Monday, January 12, 2009

Get the Site ID in an event handler (SPItemEventReceiver)

While creating an event receiver for a list that is used to replicate project status I discovered that the SPItemEventProperties did not return what I expected.

I use the SiteID to recognize the list item when doing a update. The receiving list should only have one list item (status) for each reporting project site. To identify the site I was planning to use the property SiteId

SiteID

After reporting the status from two different sites I discovered that the receiving list only contained one list item. It should have contained two, one for each site.

Some more research discovered that the siteId is the ID of the root site, not the site where the event occurred.

Needing the SiteID of the site where the event occurred I tried using the OpenWeb() function instead

WebSiteID

The ID returned what I needed, and the event receiver reported successfully status for each subsite. I guess this just adds one more situation where you can get a little confused in the use of the terms site and web in SharePoint