レコード管理の拡張 SharePoint サンプル アドイン

重要

SharePoint アドインは、2026 年 4 月 2 日に SharePoint Online のサポートが終了しました。 SharePoint でホストされているアドインとプロバイダーでホストされているアドインはどちらも廃止され、政府機関向けクラウドを含む SharePoint Online では機能しなくなります。 アドインの開発、展開、使用に関するガイダンスは、新しい SharePoint Online ソリューション用ではなく、履歴参照と該当する SharePoint Server シナリオ用に保持されます。

オンプレミスのアプリ カタログを通じて展開された SharePoint Server アドインは、この変更によって廃止されません。 パブリック マーケットプレースからの SharePoint アドインの取得は、SharePoint Server の 2026 年 4 月 2 日に終了しました。

この廃止により、SharePoint REST API、CSOM、JSOM、または SharePoint Framework (SPFx) は廃止されません。 SharePoint Online の場合は、ユーザー インターフェイス拡張機能に SPFx を使用し、外部アプリケーションへの認証に Microsoft Entra ID を使用します。 廃止のお知らせとアドインの移行ガイダンスを参照してください。

ECM.RecordsManagement サンプルは、サイトまたはリストのインプレース レコード管理の設定を制御する、プロバイダー ホスト型アドインの使用方法を示しています。

カスタム サイト プロビジョニング プロセス中にインプレース レコード管理の設定を構成するには、このソリューションを使用してください。

はじめに

まず、ECM.RecordsManagement サンプル アドインを GitHub 上の Office 365 Developer Patterns and Practices プロジェクトからダウンロードします。

注:

この記事で提供されるコードは、明示または黙示のいかなる種類の保証なしに現状のまま提供されるものであり、特定目的への適合性、商品性、権利侵害の不存在についての暗黙的な保証は一切ありません。

このアドインを実行する前に

  1. サイト コレクションでインプレース レコード管理機能をアクティブ化します。

    アクティブ化されたインプレース レコード管理機能が強調表示されている [サイト コレクションの機能] ページのスクリーンショット。

  2. サイトの設定で、[サイト コレクション管理] に [レコード宣言の設定] が表示されていることを確認します。

    レコード宣言の設定が強調表示されている [サイトの設定] ページのスクリーンショット。

ECM.RecordsManagement サンプル アドインを使用する

ECM.RecordsManagement アドインを起動すると、開始ページに使用可能な 2 つのシナリオが表示されます。

  • サイト用のインプレース レコード管理を有効にする (シナリオ 1)
  • リスト用のインプレース レコード管理を有効にする (シナリオ 2)

2 つのシナリオを示すアドイン開始ページのスクリーンショット。

シナリオ 1 を使用すると、サイト コレクションのレコード管理設定を制御する UI を作成できます。 このアドインの UI は、[サイト設定] の [レコード宣言の設定] にある UI と似ています。 サイト コレクションでインプレース レコード管理機能をアクティブ化または非アクティブ化することができます。

シナリオ 2 を使用すると、リストのレコード管理設定を制御する UI を作成できます。 このアドインの UI は、リストのライブラリ設定にある [レコード宣言の設定] の UI と似ています。

[ライブラリのレコード宣言設定] ページのスクリーンショット。

シナリオ 1

シナリオ 1 は、インプレース レコード管理機能とサイトの設定に対応しています。 次の図に示すように、アドイン UI には非 アクティブ化 (または アクティブ化) ボタンが含まれています。 このボタンを選択すると、サイト コレクションでのインプレース レコード管理機能が非アクティブ化 (またはアクティブ化) されます。

インプレース レコード管理用の非アクティブ化またはアクティブ化ボタンを示すスクリーン ショット。

次のコードは、サイト コレクションでのインプレース レコード管理機能のアクティブ化または非アクティブ化を行います。 DisableInPlaceRecordsManagementFeature メソッドと EnableSiteForInPlaceRecordsManagement メソッドは、OfficeDevPnP.Core の AppModelExtensions\RecordsManagementExtensions.cs ファイルに含まれています。

protected void btnToggleIPRStatus_Click(object sender, EventArgs e)
{
  if (cc.Site.IsInPlaceRecordsManagementActive())
  {
    cc.Site.DisableInPlaceRecordsManagementFeature();
    IPRStatusUpdate(false);
  }
  else
  {
    cc.Site.EnableSiteForInPlaceRecordsManagement();
    IPRStatusUpdate(true);
  }
}

OfficeDevPnP.Core には、サイトを対象範囲とするインプレース レコード管理設定すべての取得と設定を行う拡張メソッドがあります。 EnableSiteForInPlaceRecordsManagement メソッドの次のコードは、これらの拡張メソッドを使用して制約を設定する方法、およびサイトのレコード宣言またはレコード宣言の解除を行えるユーザーを指定する方法を示しています。

public static void EnableSiteForInPlaceRecordsManagement(this Site site)
{
  // Activate the In-Place Records Management feature if not yet enabled.
  if (!site.IsFeatureActive(new Guid(INPLACE_RECORDS_MANAGEMENT_FEATURE_ID)))
  {
    // Note: this also sets the ECM_SITE_RECORD_RESTRICTIONS value to "BlockDelete, BlockEdit".
    site.ActivateInPlaceRecordsManagementFeature();
  }

  // Enable in-place records management in all locations.
  site.SetManualRecordDeclarationInAllLocations(true);

  // Set restrictions to default values after enablement (this is also done at feature activation).
  EcmSiteRecordRestrictions restrictions = EcmSiteRecordRestrictions.BlockDelete | EcmSiteRecordRestrictions.BlockEdit;
  site.SetRecordRestrictions(restrictions);

  // Set record declaration to default value.
  site.SetRecordDeclarationBy(EcmRecordDeclarationBy.AllListContributors);

  // Set record undeclaration to default value.
  site.SetRecordUnDeclarationBy(EcmRecordDeclarationBy.OnlyAdmins);
}

ユーザーがインプレース レコード管理の設定を変更し、[変更の保存] ボタンを選択すると、btnSaveSiteScopedIPRSettings_Click メソッドの次のコードが実行されます。

protected void btnSaveSiteScopedIPRSettings_Click(object sender, EventArgs e)
{
  EcmSiteRecordRestrictions restrictions = (EcmSiteRecordRestrictions)Convert.ToInt32(rdRestrictions.SelectedValue);
  cc.Site.SetRecordRestrictions(restrictions);
  cc.Site.SetManualRecordDeclarationInAllLocations(Convert.ToBoolean(rdAvailability.SelectedValue));
  EcmRecordDeclarationBy declareBy = (EcmRecordDeclarationBy)Convert.ToInt32(rdDeclarationBy.SelectedValue);
  cc.Site.SetRecordDeclarationBy(declareBy);
  EcmRecordDeclarationBy unDeclareBy = (EcmRecordDeclarationBy)Convert.ToInt32(rdUndeclarationBy.SelectedValue);
  cc.Site.SetRecordUnDeclarationBy(unDeclareBy);
}

前のコードでは、RecordsManagementExtensions.cs の SetRecordRestrictions メソッドが呼び出されます。 次の例での SetRecordRestrictions メソッドは、レコードに制限を設定する方法を示しています。

public static void SetRecordRestrictions(this Site site, EcmSiteRecordRestrictions restrictions)
{
  string restrictionsProperty = "";

  if (restrictions.Has(EcmSiteRecordRestrictions.None))
  {
    restrictionsProperty = EcmSiteRecordRestrictions.None.ToString();
  }
  else if (restrictions.Has(EcmSiteRecordRestrictions.BlockEdit))
  {
    // BlockEdit is always used in conjunction with BlockDelete.
    restrictionsProperty = EcmSiteRecordRestrictions.BlockDelete.ToString() + ", " + EcmSiteRecordRestrictions.BlockEdit.ToString();
  }
  else if (restrictions.Has(EcmSiteRecordRestrictions.BlockDelete))
  {
    restrictionsProperty = EcmSiteRecordRestrictions.BlockDelete.ToString();
  }

  // Set property bag entry.
  site.RootWeb.SetPropertyBagValue(ECM_SITE_RECORD_RESTRICTIONS, restrictionsProperty);
}

シナリオ 2

シナリオ 2 は、リスト用のインプレース レコード管理の設定と対話する方法を示しています。 アドインがインストールされると、IPRTest というドキュメント ライブラリが作成されます。 このアドインを使ってインプレース レコード管理の設定を変更して保存すると、変更内容が IPRTest に適用されます。

注:

リストでインプレース レコード管理設定を使用するには、サイト コレクションでインプレース レコード管理機能をアクティブ化する必要があります。

ユーザーが [変更の保存] ボタンをクリックすると、次に示す Default.aspx.cs 内のコードが実行されます。

protected void btnSaveListScopedIPRSettings_Click(object sender, EventArgs e)
{
  List ipr = cc.Web.GetListByTitle(IPR_LIBRARY);
  EcmListManualRecordDeclaration listManual = (EcmListManualRecordDeclaration)Convert.ToInt32(rdListAvailability.SelectedValue);
  ipr.SetListManualRecordDeclaration(listManual);
  ipr.SetListAutoRecordDeclaration(chbAutoDeclare.Checked);

  // Refresh the settings as AutoDeclare changes the manual settings.
  if (ipr.IsListRecordSettingDefined())
  {
    rdListAvailability.SelectedValue = Convert.ToString((int)ipr.GetListManualRecordDeclaration());
    chbAutoDeclare.Checked = ipr.GetListAutoRecordDeclaration();
    rdListAvailability.Enabled = !chbAutoDeclare.Checked;
  }
}

このコードは、OfficeDevPnP.Core の RecordsManagementExtensions.cs ファイルにある次の 2 つのメソッドを呼び出します。

  • SetListManualRecordDeclaration - このリストの手動レコード宣言設定を定義します。
  • SetListAutoRecordDeclaration - レコードとしてこのリストに追加される項目を、自動的に宣言します。 このリストでのレコード宣言が自動に設定されている場合、そのリストでの手動のレコード宣言設定は適用されなくなります。 イベント レシーバーがリストに追加され、イベントが発生したときに特定のレコード管理操作が開始されます。
public static void SetListManualRecordDeclaration(this List list, EcmListManualRecordDeclaration settings)
{
  if (settings == EcmListManualRecordDeclaration.UseSiteCollectionDefaults)
  {
    // If you set list record declaration back to the default values, you also need to
    // turn off auto record declaration. Other property bag values are left as is; when
    // settings are changed again these properties are also again usable.
    if (list.PropertyBagContainsKey(ECM_AUTO_DECLARE_RECORDS))
    {
      list.SetListAutoRecordDeclaration(false);
    }
    // Set the property that dictates custom list record settings to false.
    list.SetPropertyBagValue(ECM_IPR_LIST_USE_LIST_SPECIFIC, false.ToString());
  }
  else if (settings == EcmListManualRecordDeclaration.AlwaysAllowManualDeclaration)
  {
    list.SetPropertyBagValue(ECM_ALLOW_MANUAL_DECLARATION, true.ToString());
    // Set the property that dictates custom list record settings to true.
    list.SetPropertyBagValue(ECM_IPR_LIST_USE_LIST_SPECIFIC, true.ToString());
  }
  else if (settings == EcmListManualRecordDeclaration.NeverAllowManualDeclaration)
  {
    list.SetPropertyBagValue(ECM_ALLOW_MANUAL_DECLARATION, false.ToString());
    // Set the property that dictates custom list record settings to true.
    list.SetPropertyBagValue(ECM_IPR_LIST_USE_LIST_SPECIFIC, true.ToString());
  }
  else
  {
    throw new ArgumentOutOfRangeException("settings");
  }
}

public static void SetListAutoRecordDeclaration(this List list, bool autoDeclareRecords)
{
  // Determine the SharePoint version based on the loaded CSOM library.
  Assembly asm = Assembly.GetAssembly(typeof(Microsoft.SharePoint.Client.Site));
  int sharePointVersion = asm.GetName().Version.Major;

  if (autoDeclareRecords)
  {
    // Set the property that dictates custom list record settings to true.
    list.SetPropertyBagValue(ECM_IPR_LIST_USE_LIST_SPECIFIC, true.ToString());
    // Prevent manual declaration.
    list.SetPropertyBagValue(ECM_ALLOW_MANUAL_DECLARATION, false.ToString());

    // Hook up the needed event handlers.
    list.Context.Load(list.EventReceivers);
    list.Context.ExecuteQuery();

    List<EventReceiverDefinition> currentEventReceivers = new List<EventReceiverDefinition>(list.EventReceivers.Count);
    currentEventReceivers.AddRange(list.EventReceivers);

    // Track changes to see if a list.Update is needed.
    bool eventReceiverAdded = false;

    // ItemUpdating receiver.
    EventReceiverDefinitionCreationInformation newEventReceiver = CreateECMRecordEventReceiverDefinition(EventReceiverType.ItemUpdating, 1000, sharePointVersion);
    if (!ContainsECMRecordEventReceiver(newEventReceiver, currentEventReceivers))
    {
      list.EventReceivers.Add(newEventReceiver);
      eventReceiverAdded = true;
    }
    // ItemDeleting receiver.
    newEventReceiver = CreateECMRecordEventReceiverDefinition(EventReceiverType.ItemDeleting, 1000, sharePointVersion);
    if (!ContainsECMRecordEventReceiver(newEventReceiver, currentEventReceivers))
    {
      list.EventReceivers.Add(newEventReceiver);
      eventReceiverAdded = true;
    }
    // ItemFileMoving receiver.
    newEventReceiver = CreateECMRecordEventReceiverDefinition(EventReceiverType.ItemFileMoving, 1000, sharePointVersion);
    if (!ContainsECMRecordEventReceiver(newEventReceiver, currentEventReceivers))
    {
      list.EventReceivers.Add(newEventReceiver);
      eventReceiverAdded = true;
    }
    // ItemAdded receiver.
    newEventReceiver = CreateECMRecordEventReceiverDefinition(EventReceiverType.ItemAdded, 1005, sharePointVersion);
    if (!ContainsECMRecordEventReceiver(newEventReceiver, currentEventReceivers))
    {
      list.EventReceivers.Add(newEventReceiver);
      eventReceiverAdded = true;
    }
    // ItemUpdated receiver.
    newEventReceiver = CreateECMRecordEventReceiverDefinition(EventReceiverType.ItemUpdated, 1007, sharePointVersion);
    if (!ContainsECMRecordEventReceiver(newEventReceiver, currentEventReceivers))
    {
      list.EventReceivers.Add(newEventReceiver);
      eventReceiverAdded = true;
    }
    // ItemCheckedIn receiver.
    newEventReceiver = CreateECMRecordEventReceiverDefinition(EventReceiverType.ItemCheckedIn, 1006, sharePointVersion);
    if (!ContainsECMRecordEventReceiver(newEventReceiver, currentEventReceivers))
    {
      list.EventReceivers.Add(newEventReceiver);
      eventReceiverAdded = true;
    }

    if (eventReceiverAdded)
    {
      list.Update();
      list.Context.ExecuteQuery();
    }

    // Set the property that dictates the autodeclaration.
    list.SetPropertyBagValue(ECM_AUTO_DECLARE_RECORDS, autoDeclareRecords.ToString());
  }
  else
  {
    // Set the property that dictates the autodeclaration.
    list.SetPropertyBagValue(ECM_AUTO_DECLARE_RECORDS, autoDeclareRecords.ToString());
    //Note: Existing list event handlers will just stay as they are, no need to remove them.
  }
}

関連項目