---
title: "イベント"
slug: "events-841788"
abstract: ""
---
# イベント

イベント定義はSitecore.Marketing.Definitions.Events.EventDefinitionManagerクラスによって管理されます。

## EventDefinitionManagerへのアクセス

EventDefinitionManagerはSitecore DIコンテナから利用可能です。クラスのコンストラクタに型DefinitionManagerBase<IEventDefinition, EventDefinitionRecord>のパラメータを含め、コンテナからクラスを引き出すことで、コンテナがインスタンスを解決するのが望ましいです。

public MyClass(DefinitionManagerBase<IEventDefinition, EventDefinitionRecord> eventDefinitionManager)
{
 ...
}

コンテナを使ってクラスを構築できない場合は、サービスロケーターを使うことができます。このクラスはSitecore DIコンテナでも利用可能です:

using Sitecore.DependencyInjection;
using Sitecore.Marketing.Definitions;

ServiceLocator.ServiceProvider.GetDefinitionManagerFactory().GetDefinitionManager<IEventDefinition>();

## イベントの定義

イベントはSitecore.Marketing.Definitions.Events.Model名前空間の型を用いて定義されます。

Guid eventId = Guid.NewGuid(); // Event ID
CultureInfo eventCulture = new CultureInfo("es"); // Event culture
string eventName = "Newly created event"; // Event name
DateTime creationDate = DateTime.UtcNow; // Event creation date
string createdBy = "sitecore\\admin"; // Event creator

EventDefinition event = new EventDefinition(eventId, "Event item name", eventCulture, eventName, creationDate, createdBy);

event.ShowInXfileAsLatestEvent = true;
event.ShowInXfileEventsList = true;
event.EngagementValuePoints = 20;

 event.IsLiveEvent = false;

### ライブイベント

IsLiveEventプロパティは基本EventDefinitionクラスの一部であり、マーケティングオートメーションがイベントをセッション終了時ではなく即時処理するかどうかを判断するために使用されます。[ライブイベント処理](https://doc.sitecore.com/xp/ja/developers/103/sitecore-experience-platform/marketing-automation-plan-and-activity-enrollment.html "Marketing Automation plan and activity enrollment")についてさらに詳しくはこちらをご覧ください。

## イベントのセーブ

イベントを定義したら、定義マネージャーのSaveAsync() メソッドを呼び出すことで保存できます。

Guid eventId = Guid.NewGuid(); // Event ID
CultureInfo eventCulture = new CultureInfo("es"); // Event culture
string eventName = "Download Brochure"; // Event name
DateTime creationDate = DateTime.UtcNow; // Event creation date
string createdBy = "sitecore\\admin"; // Event creator

EventDefinition event = new EventDefinition(eventId, "Event item name", eventCulture, eventName, creationDate, createdBy);

 EventDefinitionManager manager = ServiceLocator.ServiceProvider.GetDefinitionManagerFactory().GetDefinitionManager<IEventDefinition>();
 manager.SaveAsync(event);

また、セーブ中にtrueSaveAsync()メソッドの2つ目のパラメータに渡すことでイベントを有効化することも可能です。

manager.SaveAsync(event, true);

## イベントの起動

イベントは管理外で利用可能になる前に有効化しなければなりません。イベントは保存時にtrueSaveAsync()メソッドのactivate(2つ目の)パラメータに渡すことで有効化できます。

manager.SaveAsync(event, true);

イベントは、ActivateAsync() メソッドを用いてセーブを呼び出さずに有効化することも可能です。

manager.ActivateAsync(eventId);

ActivateAsync()法はイベントのIDを取り、イベント定義モデルを必要としません。

## イベントの削除

イベントを削除するには、マネージャーのDelete() メソッドを使用します。個々のカルチャーは定義から削除できず、定義全体のみ削除できます。メソッド呼び出しに提供されるカルチャーはnull(デフォルト値)またはCultureInfo.InvariantCultureのいずれかであるべきです。

manager.Delete(eventId);

## イベントを取得する

単一のイベントは、マネージャーのGet() メソッドのいずれかを使ってIDで取得できます。

Guid eventId = Guid.NewGuid(); // Ensure this is the ID of an existing event

// Get by ID and culture. Will get the latest active version
IEventDefinition event = manager.Get(eventId, new CultureInfo("da"));

// Get by ID and culture. Will get the latest version, including if the version is inactive
IEventDefinition event = manager.Get(eventId, new CultureInfo("da"), true);

### イベントを別名で取得する

また、[イベントの別名](https://doc.sitecore.com/xp/ja/developers/103/sitecore-experience-platform/definition-aliases.html "Definition aliases")からも取得できます:

CultureInfo eventCulture = new CultureInfo("fr-fr");
var eventDefinitionByAlias = definitionManager.GetByAlias("My alias", eventCulture);

## 既存のイベントの更新

既存のイベント定義を更新するには、IDでイベントを取得し、イベントを編集します

Guid eventId = Guid.NewGuid(); // Ensure this is the ID of an existing event

// Get by ID and culture. Will get the latest active version
IEventDefinition event = manager.Get(eventId, new CultureInfo("da"));

// Make updates to event
event.Name = "Updated name";

// Save to update
manager.SaveAsync(event);

## すべてのイベントの取得

GetAll()メソッドはマネージャーからすべてのイベントを取得するために使用できます。定義が多岐にわたるため、このメソッドはページングをサポートしています。返却値は結果の1ページ分です。

// Get All with defaults which will be first page, page size 20, latest active versions only
 ResultSet<DefinitionResult<IEventDefinition>> events = manager.GetAll(new CultureInfo("da"), new RetrievalParameters<IEventDefinition, string>());

 // Get page 2
 ResultSet<DefinitionResult<IEventDefinition>> page2Events = manager.GetAll(new CultureInfo("da"), new RetrievalParameters<IEventDefinition, string>(pageNumber: 2));

 // Include inactive versions
ResultSet<DefinitionResult<IEventDefinition>> page1Events = manager.GetAll(new CultureInfo("da"), new RetrievalParameters<IEventDefinition, string>(), true);

結果にアクセスするには、結果のDataPageプロパティを使いましょう。

IEventDefinition event = page1Events.DataPage.ElementAt(0);

その結果には、定義の総数や現在のページインデックス、ページサイズを公開するプロパティも含まれています。

long totalDefinitionCount = page1Events.Total;
int pageNumber = page1Events.PageNumber;
int pageSize = page1Events.Count;