建立一個 **ItemDeleting** 事件接收器,攔截並取消特定列表(公告列表)的項目刪除操作,以 Feature 形式部署啟用。
Visual C# → Class Library,名稱 DeletingEventHandler。Microsoft.SharePoint (.NET 標籤)。using Microsoft.SharePoint;SPItemEventReceiver,覆寫 ItemDeleting:
public class DeletingAction : SPItemEventReceiver
{
public override void ItemDeleting(SPItemEventProperties properties)
{
properties.Cancel = true; // 取消刪除
properties.ErrorMessage = "Deleting items from " + properties.RelativeWebUrl + " is not supported.";
}
}
DeletingEventHandler.snk。Ctrl+Shift+B 編譯產生 DLL。DeletingEventHandler.dll 從 bin\Debug 拖曳至 C:\Windows\Assembly 部署 GAC。.../12/TEMPLATE/FEATURES/DeletingEventHandlerFeature.xml(範圍 Web):
<Feature Scope="Web" Title="Deleting Event Handler" Id="{GUID}"
xmlns="https://schemas.microsoft.com/sharepoint/">
<ElementManifests>
<ElementManifest Location="Elements.xml" />
</ElementManifests>
</Feature>
guidgen.exe 產生 GUID。Elements.xml 註冊事件接收器:
<Elements xmlns="https://schemas.microsoft.com/sharepoint/">
<Receivers ListTemplateId="104"> <!-- 僅套用至公告列表 (Template 104) -->
<Receiver>
<Name>DeletingEventHandler</Name>
<Type>ItemDeleting</Type> <!-- 同步前置事件 -->
<SequenceNumber>10000</SequenceNumber>
<Assembly>
DeletingEventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a26b5449ac4a4cf3
</Assembly>
<Class>DeletingEventHandler.DeletingAction</Class>
<Data></Data>
<Filter></Filter>
</Receiver>
</Receivers>
</Elements>
PublicKeyToken 取得方式:總管瀏覽 C:\Windows\Assembly,右鍵 DLL → 內容 → 一般頁籤複製 Token。stsadm -o installfeature -filename DeletingEventHandler\feature.xml
stsadm -o activatefeature -filename DeletingEventHandler\feature.xml -url https://Server/Site/Subsite
iisreset
Deleting items from /sites/subsite is not supported.ListTemplateId="104" 限定範圍。| 項目 | 說明 |
|---|---|
| 基類 | SPItemEventReceiver(列表項事件)、SPListEventReceiver(列表事件)、SPWebEventReceiver(站台事件)、SPFeatureReceiver(Feature 事件) |
| 同步 vs 非同步 | ItemDeleting(-ing 尾綴)= **同步前置事件**,可取消;ItemDeleted(-ed 尾綴)= **非同步後置事件**,不可取消。 |
| 範圍限定 | ListTemplateId(列表範本)、ListUrl(特定列表 URL)、ListId(特定列表 GUID)三選一。 |
| SequenceNumber | 同類型多接收器執行順序,數值小先執行。 |
| Assembly 強名稱 | 四部曲:名稱、版本、文化特性、PublicKeyToken,**必須精確**。 |
| 部署位置 | 事件接收器組件**必須在 GAC**,WSS 才能載入。 |