다음을 통해 공유


SPItemEventReceiver Class

Provides methods for trapping events that occur to items. This class is never instantiated.

Inheritance Hierarchy

System.Object
  Microsoft.SharePoint.SPEventReceiverBase
    Microsoft.SharePoint.SPItemEventReceiver

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

Syntax

'Declaration
<SubsetCallableTypeAttribute> _
Public Class SPItemEventReceiver _
    Inherits SPEventReceiverBase
'Usage
Dim instance As SPItemEventReceiver
[SubsetCallableTypeAttribute]
public class SPItemEventReceiver : SPEventReceiverBase

Remarks

This 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.

Examples

The following code example uses this 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();
            }
        }
    }
}
Imports System
Imports Microsoft.SharePoint

Namespace Example_Namespace
    Public Class Class_Name
        Inherits SPItemEventReceiver
        Public Overrides Sub ItemAttachmentAdded(ByVal properties As SPItemEventProperties)
            Using oSiteCollectionEvent As New SPSite(properties.SiteId)
                Dim oSiteEvent As SPWeb = oSiteCollectionEvent.OpenWeb(properties.RelativeWebUrl)
                Dim oItemsEvent As SPListItemCollection = oSiteEvent.Lists(properties.ListTitle).Items
            End Using
            Using oSiteCollection As New SPSite("http://Top_Site")
                Dim oWebsite As SPWeb = oSiteCollection.OpenWeb("Website_Name")
                Dim oList As SPList = oWebsite.Lists("Announcements")
                Dim collListItems As SPListItemCollection = oList.Items

                Dim oItem As SPListItem = collListItems.Add()
                oItem("Title") = properties.UserDisplayName & " added an attachment to " & oItemsEvent(properties.ListItemId).Title & " in list " & properties.ListTitle & " at " & properties.WebUrl
                oItem.Update()
            End Using
        End Sub
    End Class
End Namespace

Note

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 Disposing Objects.

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

Reference

SPItemEventReceiver Members

Microsoft.SharePoint Namespace