Sharepoint-feature-event-receiver

提供:Dev Guides
移動先:案内検索

SharePoint-Feature \ Event Receiver

この章では、コードハンドル*を追加する方法を学びます。 コードハンドルは、機能がアクティブ化または非アクティブ化されたときに発生するイベントです。 つまり、 *Feature Receivers を調べます。

前の章で作成したVisual Studioプロジェクトには1つの機能があり、アクティブ化されると、連絡先リスト、SitePage、およびSitePageへのリンクがプロビジョニングされました。

ただし、機能が非アクティブ化されると、SharePointはリンクのみを削除し、SitePageと連絡先リストは残ります。

必要に応じて、リストとページを削除するために機能が非アクティブ化されたときにコードを記述できます。 この章では、フィーチャーが非アクティブ化されたときにコンテンツと要素を削除する方法を学習します。

Featureのイベントを処理するには、 Feature Receiver が必要です。

ステップ1 *-フィーチャーレシーバーを取得するには、ソリューションエクスプローラーでフィーチャーを右クリックし、[イベントレシーバーの追加]を選択します。

using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;

namespace FeaturesAndElements.Features.Sample {
  ///<summary>
     ///This class handles events raised during feature activation, deactivation,
         installation, uninstallation, and upgrade.
  ///</summary>
  ///<remarks>
     ///The GUID attached to this class may be used during packaging and should not be modified.
  ///</remarks>
   [Guid("e873932c-d514-46f9-9d17-320bd3fbcb86")]

   public class SampleEventReceiver : SPFeatureReceiver {
     //Uncomment the method below to handle the event raised after a feature has been activated.
     //public override void FeatureActivated(SPFeatureReceiverProperties properties)//{
        //
      }
     //Uncomment the method below to handle the event raised before a feature is deactivated.
     //public override void FeatureDeactivating(SPFeatureReceiverProperties properties)//{
        //
      }
     //Uncomment the method below to handle the event raised after a feature has been installed.
     //public override void FeatureInstalled(SPFeatureReceiverProperties properties)//{
        //
      }
     //Uncomment the method below to handle the event raised before a feature is uninstalled.
     //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)//{
        //
      }
     //Uncomment the method below to handle the event raised when a feature is upgrading.
     //public override void FeatureUpgrading(SPFeatureReceiverProperties
         properties, string upgradeActionName,
         System.Collections.Generic.IDictionary<string, string> parameters)//{
        //
      }
   }
}

取得するものは、 SPFeatureReceiver を継承するクラスです。

SharePointには、処理できるイベントの種類ごとに異なるクラスがあります。 たとえば、リスト上のイベント、リストアイテム上のイベント、サイト上のイベント。 特定のイベントレシーバーから派生したクラスを作成し、そのクラス内のメソッドをオーバーライドしてイベントを処理できます。

それがされているときに機能のイベントが使用されます-

  • 有効化
  • 無効化
  • インストール済み
  • アンインストール済み
  • アップグレード

次に、特定のアイテムのイベントハンドラーとしてそのクラスをアタッチする必要があります。 たとえば、リストイベントを処理するイベントハンドラーがある場合、そのクラスをリストにアタッチする必要があります。

したがって、我々は2つの機能を処理します-

  • 機能がアクティブになったとき
  • 非アクティブ化されているとき。

ステップ2 *-以下に示すように、 *FeatureActivated およびFeatureDeactivatedメソッドを実装します-

using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;

namespace FeaturesAndElements.Features.Sample {
  ///<summary>
     ///This class handles events raised during feature activation, deactivation,
         installation, uninstallation, and upgrade.
  ///</summary>
  ///<remarks>
     ///The GUID attached to this class may be used during packaging and should
         not be modified.
  ///</remarks>

   [Guid("e873932c-d514-46f9-9d17-320bd3fbcb86")]
   public class SampleEventReceiver : SPFeatureReceiver {
      private const string listName = "Announcements";

      public override void FeatureActivated(SPFeatureReceiverProperties properties) {
         var web = properties.Feature.Parent as SPWeb;

         if (web == null) return;
         var list = web.Lists.TryGetList(listName);

         if (list != null) return;
         var listId = web.Lists.Add(listName, string.Empty,
         SPListTemplateType.Announcements);
         list = web.Lists[listId];
         list.OnQuickLaunch = true;
         list.Update();
      }
      public override void FeatureDeactivating(SPFeatureReceiverProperties properties) {
         var web = properties.Feature.Parent as SPWeb;

         if (web == null) return;
         var list = web.Lists.TryGetList(listName);

         if (list == null) return;
         if (list.ItemCount == 0) {
            list.Delete();
         }
      }
   }
}

-

  • 機能がアクティブになると、お知らせリストを作成します。
  • 機能が非アクティブ化されると、お知らせリストが空かどうかを確認し、空の場合は削除します。
  • ステップ3 *-プロジェクトを右クリックして、デプロイを選択します。 次のデプロイメント競合警告が表示されます。

展開の競合

Visual Studioは、contactsというリストを作成しようとしていると言っていますが、Contactsというリストがすでにサイトにあります。 既存のリストを上書きするかどうかを尋ねられます。この場合、*解決*をクリックします。

  • ステップ4 *-SharePointに戻り、サイトを更新して、*サイトアクション→サイト設定→サイト機能の管理→サンプル機能*に移動します。

サンプル機能

左ペインにお知らせリストがないことがわかります。

  • ステップ5 *-サンプル機能を有効にすると、お知らせリストが表示されますが、現在は空です。

お知らせリスト

-サンプル機能を無効にすると、お知らせリストが消えます。

  • ステップ6 *-機能を再度有効にします。 お知らせに移動してから、新しいお知らせを追加します。 このテストを呼び出して、[保存]をクリックします。

新しいお知らせ

アナウンスの下にテストファイルが表示されます。

発表中のテストファイル

これで、アナウンスを非アクティブ化すると、アナウンスリストが空ではなかったために残ることがわかります。

アナウンスメントの非アクティブ化