首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

WSS 3.0 SDK 實戰演練:建立事件接收器 Feature——攔截列表項刪除操作的完整實作

     編輯:關於.NET

演練目標

建立一個 **ItemDeleting** 事件接收器,攔截並取消特定列表(公告列表)的項目刪除操作,以 Feature 形式部署啟用。

兩階段實作流程

  1. Visual Studio 建立事件接收器組件(Class Library)。
  2. 封裝為 Feature 部署至 WSS。

階段一:Visual Studio 建立事件處理程序

  1. 新專案:Visual C# → Class Library,名稱 DeletingEventHandler。
  2. 加入參考:Microsoft.SharePoint (.NET 標籤)。
  3. 引用命名空間:using Microsoft.SharePoint;
  4. 類別繼承 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.";
        }
    }
  5. 專案屬性 → 簽署 → 勾選「簽署組件」→ 新建強名稱金鑰 DeletingEventHandler.snk。
  6. Ctrl+Shift+B 編譯產生 DLL。
  7. 將 DeletingEventHandler.dll 從 bin\Debug 拖曳至 C:\Windows\Assembly 部署 GAC。

階段二:封裝為 Feature 部署

  1. 建立 Feature 目錄:.../12/TEMPLATE/FEATURES/DeletingEventHandler
  2. 建立 Feature.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。
  3. 建立 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。
  4. 命令列安裝啟用:
    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" 限定範圍。

關鍵技術點總結

此演練展示了從程式碼到 Feature 封裝的事件接收器開發完整鏈路,為 SharePoint 伺服器端邏輯擴充提供標準範本。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved
項目說明
基類SPItemEventReceiver(列表項事件)、SPListEventReceiver(列表事件)、SPWebEventReceiver(站台事件)、SPFeatureReceiver(Feature 事件)
同步 vs 非同步ItemDeleting(-ing 尾綴)= **同步前置事件**,可取消;ItemDeleted(-ed 尾綴)= **非同步後置事件**,不可取消。
範圍限定ListTemplateId(列表範本)、ListUrl(特定列表 URL)、ListId(特定列表 GUID)三選一。
SequenceNumber同類型多接收器執行順序,數值小先執行。
Assembly 強名稱四部曲:名稱、版本、文化特性、PublicKeyToken,**必須精確**。
部署位置事件接收器組件**必須在 GAC**,WSS 才能載入。