Wiki内検索
最近更新したページ
最新コメント
メニューバー
タグ

イベントレシーバの作成方法

■[WSS3.0(MOSS2007)] イベントレシーバの作成方法

イベントレシーバの作成方法についてまとめました。以下の手順は、新しくドキュメントライブラリのフィーチャーを作成し、そのドキュメントライブラリに、新しく作成したイベントレシーバを関連付けるための手順です。
なお、既存のドキュメントライブラリやリストにイベントレシーバを関連付けることも可能です。


■イベントレシーバの作成方法
以下は、ドキュメントライブラリのアイテムが新規追加された直後(ItemAdded)に稼働するイベントレシーバを作成する手順です。

●必要環境
  • Windows Server 2003 (SharePoint稼働サーバ)
  • Visual Studio 2005 (推奨: SP1以上)
  • VSeWSS (Visual Studio 2005 Extensions for Windows SharePoint Services)


【1】準備
(1) Windows Server 2003がインストールされているサーバに、WSS3.0(MOSS2007)をインストールする。
(2) 「Visual Studio 2005」をインストールする。
(3) 「VSeWSS」をインストールする。


【2】イベントレシーバのコーディング
(1) 「Visual Studio 2005」を起動し、新規にプロジェクトを作成する。このとき、プロジェクトテンプレートに「List Definition」を選択する。
(2) 表示されるダイアログで、以下のように選択する。
「Base List Definition」 → 「Document Library」を選択する
「Create an instance of this list」 → 選択しない
「Add with event receiver」 → 選択する
(3) イベントレシーバ用のプロジェクトテンプレートが自動展開されるので、「ItemEventReceiver.cs」ファイルの「ItemAdded」メソッドに、イベントレシーバ用のコーディングを行う。
(例)
namespace ListDefinition1
{
    [CLSCompliant(false)]
    [TargetList("5c990999-d7b5-4d28-af38-cacb3c975d14")]
    [Guid("04b0a977-fb23-4cc9-a097-ceb70ac57f9e")]
    public class ListDefinition1ItemEventReceiver : SPItemEventReceiver
    {
        /// <summary>
        /// Asynchronous after event that occurs after a new item has been added to its containing object.
        /// </summary>
        /// <param name="properties">
        /// An Microsoft.SharePoint.SPItemEventProperties object that represents properties of the event handler.
        /// </param>
        [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
        public override void ItemAdded(SPItemEventProperties properties)
        {
            System.Diagnostics.EventLog.WriteEntry(properties.UserDisplayName, properties.ListItem.File.Url);
        }
    }
}

【3】WSS(SharePoint)サーバへの反映

●Windows Server 2003で開発する場合
(1) 「Visual Studio 2005」で、イベントレシーバ用のプロジェクトを起動している状態で、F5キーを押下する。F5キーを押下することによって、アセンブリのビルドからWSSサーバへの配置まで自動的に行われる。

●その他のOSで開発する場合
(1) 「Visual Studio 2005」で、プロジェクトをビルドし、DLLファイルを生成する。
(2) 作成したDLLファイルを、WSSサーバの「C:\WINDOWS\assembly」にDrag&Dropする。
(3) WSS(SharePoint)稼働サーバの「C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES」フォルダの下に、「ListDefinition1」という名前で新しいフォルダを作成する。
(4) (3)で作成したフォルダの下に、以下のファイルを配置する。
feature.xml
<Feature Scope="Web"
    Title="ListDefinition1"
    Description=""
    Id="04b0a977-fb23-4cc9-a097-ceb70ac57f9e"
    DefaultResourceFile="core"
    xmlns="http://schemas.microsoft.com/sharepoint/">
    <ElementManifests>
        <ElementManifest Location="elements.xml"/>
        <ElementManifest Location="instance.xml"/>
    </ElementManifests>
</Feature>

elements.xml
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <Receivers ListTemplateId="211">
        <Receiver>
            <Name>ListDefinition1</Name>
            <Type>ItemAdded</Type>
            <SequenceNumber>10000</SequenceNumber>
            <Assembly>
                ListDefinition1, Version=1.0.0.0,
                Culture=neutral, PublicKeyToken=9f4da00116c38ec5
            </Assembly>
            <Class>ListDefinition1.ListDefinition1ItemEventReceiver</Class>
            <Data></Data>
            <Filter></Filter>
        </Receiver>
    </Receivers>
</Elements>

instance.xml
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <ListTemplate Name="ListDefinition1" DisplayName="ListDefinition1"
        Type="211" Description="ListDefinition1の説明"
        BaseType="1" OnQuickLaunch="TRUE"
        DocumentTemplate="102"
        SecurityBits="11" />
</Elements>
(5) (3)で作成したフォルダの下に、「ListDefinition1」という名前で新しいフォルダを作成する。
(6) (5)で作成したフォルダの下に、以下のファイルを配置する。なお、配置元のファイルは、「Visual Studio 2005」でイベントレシーバ用のプロジェクトを作成したときに、プロジェクトの「List Definition1」フォルダの下に自動生成されている。
AllItems.aspx
Combine.aspx
DispForm.aspx
EditDlg.htm
EditForm.aspx
FileDlg.htm
Repair.aspx
schema.xml
Upload.aspx
WebFldr.aspx
(7) コマンドプロンプトより、以下のコマンドを実行する。このコマンドプロンプトを実行することにより、WSSサーバにフィーチャーをインストールする。
chdir C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN
stsadm -o installfeature -filename ListDefinition1\feature.xml -force
"%SystemRoot%\System32\iisreset.exe" /restart

【4】WSSサイトへのイベントレシーバの配置方法
(1) 各サイトページより、「サイトの操作」->「サイトの設定」->「サイトの機能」を選択する。
(2) 上記で追加したフィーチャー(例:ListDefinition1)が「アクティブ」と表示されていることを確認する。「アクティブ」と表示されていない場合は、「アクティブ化」ボタンを押してアクティブにする。
(3) 各サイトページより、「サイトの操作」->「作成」を選択する。
(4) 上記で追加したテンプレート(例:ListDefinition1)が表示されているので、それを選択し、新しいドキュメントラブラリを作成する。


【5】イベントレシーバの稼働確認
(1) 作成したドキュメントライブラリに、新しいファイルをアップロードして、イベントレシーバのプログラムが正しく動作していればOK。
2007年05月08日(火) 00:33:42 Modified by sharepoint




スマートフォン版で見る