SPItemEventReceiver Class (Microsoft.SharePoint)
SPItemEventReceiver Class (Microsoft.SharePoint)
Provides methods for trapping events that occur to items. This class is never instantiated.

Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
Syntax

'Usage

Dim instance As SPItemEventReceiver


'Declaration

<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel:=True)> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel:=True)> _
Public Class SPItemEventReceiver
    Inherits SPEventReceiverBase
Remarks

The SPItemEventReceiver class is not instantiated but the item event receiver class of a custom event handler must derive from this class and override its methods for the event types that are handled.

If an item is moved, it is treated in the same way as an item that has been removed and then added.

Before and After properties are guaranteed for post events on documents, but Before properties are not available for post events on list items.

Example

The following code example uses the SPItemEventReceiver class to add an item to a specified Announcements list whenever an attachment is added to a list.

using System;
using Microsoft.SharePoint;

namespace Example_Namespace
{
    public class Class_Name : SPItemEventReceiver
    {
        public override void ItemAttachmentAdded(SPItemEventProperties properties)
        {
            using(SPSite oSiteCollectionEvent = new SPSite(properties.SiteId))
            {
                SPWeb oSiteEvent = oSiteCollectionEvent.OpenWeb(properties.RelativeWebUrl);
                SPListItemCollection oItemsEvent = oSiteEvent.Lists[properties.ListTitle].Items;
            }
            using(SPSite oSiteCollection = new SPSite("http://Top_Site"))
            {
                SPWeb oWebsite = oSiteCollection.OpenWeb("Website_Name");
                SPList oList = oWebsite.Lists["Announcements"];
                SPListItemCollection collListItems = oList.Items;

                SPListItem oItem = collListItems.Add();
                oItem["Title"] = properties.UserDisplayName + " added an attachment to " + oItemsEvent[properties.ListItemId].Title + " in list " + properties.ListTitle + " at " + properties.WebUrl;
                oItem.Update();
            }
        }
    }
}

NoteNote:

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Best Practices: Using Disposable Windows SharePoint Services Objects.

Inheritance Hierarchy

System.Object
   Microsoft.SharePoint.SPEventReceiverBase
    Microsoft.SharePoint.SPItemEventReceiver
       Microsoft.SharePoint.Workflow.SPWorkflowLibraryEventReceiver
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
See Also

Community Content

Following "Best Practices: Using Disposable Windows SharePoint Service Objects."
Added by:Keith Dahlby

This example actually only half follows the Best Practices; the lines which call the oSiteCollectionEvent.OpenWeb() method should also be inside a using statement:

using(SPSite oSiteCollectionEvent = new SPSite(properties.SiteId)) {
using(SPWeb oSiteEvent = oSiteCollectionEvent.OpenWeb(properties.RelativeWebUrl)) {
SPListItemCollection oItemsEvent = oSiteEvent.Lists[properties.ListTitle].Items;
}
}

[dahlbyk] When an SPSite is disposed, all remaining open SPWeb objects are disposed as well, so a nested using here is redundant and unnecessary.

© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View