共用方式為


SPItemEventReceiver class

提供方法讓補漏白發生事件的項目。這個類別無法執行個體化。

Inheritance hierarchy

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

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'宣告
Public Class SPItemEventReceiver _
    Inherits SPEventReceiverBase
'用途
Dim instance As SPItemEventReceiver
public class SPItemEventReceiver : SPEventReceiverBase

備註

這個類別不具現化但自訂的事件處理常式的項目事件接收器類別必須衍生自這個類別並覆寫處理的事件型別及其方法。

如果已移動的項目,它會被視為已移除然後再加入的項目為相同的方式。

Before和After屬性保證張貼事件文件,但Before屬性沒有可用的清單項目上張貼事件。

Examples

下列程式碼範例會使用這個類別來指定 [宣布] 清單中加入項目附件加入至清單時。

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
注意事項注意事項

某些物件實作IDisposable介面,並且您必須避免之後不再需要保留這些物件在記憶體中。良好的程式碼撰寫方式的相關資訊,請參閱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.

請參閱

參照

SPItemEventReceiver members

Microsoft.SharePoint namespace