イベント登録

最終更新日: 2010年3月24日

適用対象: SharePoint Foundation 2010

イベントの応答用フィルターを実装するために使用するイベント ハンドラーを、サイト レベルまたはサイト コレクション レベルで登録します。

Receivers 要素は、リスト アイテム イベントのイベント ハンドラを指定します。

Elements

  Receivers

    Receiver

      Assembly

      Class

      Data

      Filter 要素 (イベント)

      Name

      SequenceNumber

      SolutionId 要素 (イベント)

      Synchronization 要素 (イベント)

      Type

リスト イベントのイベント ハンドラを登録するには、フィーチャーの範囲と ID が指定された Feature.xml ファイルとこのファイルから参照される要素マニフェスト ファイルを格納するフォルダを \Template\Features に作成します。

イベント ハンドラを登録する Feature.xml ファイルの内容は、次のように記述します。

<Feature 
  Scope="Web" 
  Title="Simple Updating Item Event Handler Registration" 
  Id="A6B8687A-3200-4b01-AD76-09E8D163FB9A" 
  xmlns="https://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest Location="elements.xml"/>
  </ElementManifests>
</Feature>

要素マニフェスト ファイルは、イベント ハンドラ アセンブリを登録して、リストの種類に関連付けます。次の例では、お知らせリスト (104) を指定しています。

<Elements xmlns="https://schemas.microsoft.com/sharepoint/">
  <Receivers 
    ListTemplateId="104">
    <Receiver>
      <Name>SimpleUpdateEvent</Name>
      <Type>ItemUpdating</Type>
      <SequenceNumber>10000</SequenceNumber>
      <Assembly>SimpleUpdateEventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=10b23036c9b36d6d</Assembly>
      <Class>MS.Samples.SimpleItemUpdateHandler</Class>
      <Data></Data>
    </Receiver>
  </Receivers>
</Elements>

イベント ハンドラの .cs ファイルでは、SharePoint Foundation オブジェクト モデルを使用してイベントに応答できます。オブジェクト モデルを使用してカスタム イベント ハンドラを作成する方法の詳細については、「SharePoint Foundation 2010 のイベント」を参照してください。

次の例では、ユーザーがリスト内のアイテムを変更しようとしたときに表示されるエラー メッセージの内容を定義しています。

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace MS.Samples
{
    public class SimpleItemUpdateHandler : SPItemEventReceiver
    {
        public override void ItemUpdating(SPItemEventProperties properties)
        {
            properties.Cancel = true;
            properties.ErrorMessage = "Updating data is not supported.";
        }
    }
}
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Microsoft.SharePoint

Namespace MS.Samples
    Public Class SimpleItemUpdateHandler
        Inherits SPItemEventReceiver
        Public Overrides Sub ItemUpdating(ByVal properties As SPItemEventProperties)
            properties.Status = SPEventReceiverStatus.CancelWithError
            properties.ErrorMessage = "Updating data is not supported."
        End Sub
    End Class
End Namespace

関連項目

タスク

[方法] イベント ハンドラ フィーチャーを作成する

その他の技術情報

SharePoint Foundation 2010 のイベント