---
title: "List Manager API"
slug: "the-list-manager-api"
abstract: ""
---
# List Manager API

Sitecoreはマーケティングの定義として連絡先リストを保存しています。コンテンツマネージャー(CM)インスタンスでは、List Manager APIを使ってプログラム的に連絡先リストにアクセスできます。

> !注Content Delivery(CD)インスタンスではList Manager APIを使うことはできません。なぜなら、この場合はList Managerアプリケーションが無効化されているからです。

## セグメントの追加、更新、削除

この例は、List Manager APIを使ってセグメントを追加する方法を示しています。

> !注セグメントルールの作成および変更はList Managerインターフェースを通じてのみ行えます。

var segmentRepository = ServiceLocator.ServiceProvider.GetService< SegmentRepository>();

var newSegment = new SegmentModel
      {
         Id = Guid.NewGuid().ToString("B"),
         Name = "New Segment",
         Description = "New Description",
         UseAsAnalyticsFilter = true
      };

segmentRepository.Add(newSegment);

この例はセグメントの更新方法を示しています。

var segmentRepository = ServiceLocator.ServiceProvider.GetService< SegmentRepository>();

//Find existing segment by ID. ID must be vali
var segmentToUpdate = segmentRepository.FindById("{00000000-0000-0000-0000-000000000000}");
segmentToUpdate.Description = "New Description";
segmentRepository.Update(segmentToUpdate);

この例はセグメントを削除する方法を示しています。

var segmentRepository = ServiceLocator.ServiceProvider.GetService<IFetchRepository<SegmentModel>>();

//Find existing segment by ID. ID must be valid.

var segmentToDelete =  segmentRepository.FindById("{00000000-0000-0000-0000-000000000000}");
segmentRepository.Delete(segmentToDelete);
## セグメント化されたリストの追加、更新、削除

この例は、セグメント化されたリストを追加する方法を示しています。

var segmentedListRepository = ServiceLocator.ServiceProvider.GetService< SegmentedListRepository>();

var newSegmentedList = new SegmentedListModel()
            {
                Id = Guid.NewGuid().ToString("B"),
                Name = "New Contact List",
                Description = "Description of the contact list",
                Owner = "Administrator"
            };

作成したいリストにセグメントを追加するには、このコードスニペットを使ってください。

newSegmentedList .SegmentIds = new {“<Guid of the Segment>”};

新しい分割リストを保存するには、このコードスニペットを使ってください。

segmentedListRepository.Add(newSegmentedList);

この例は、分割されたリストを更新する方法を示しています。

var segmentedListRepository = ServiceLocator.ServiceProvider.GetService< SegmentedListRepository>();

//Find existing list by ID. ID must be valid.

var listToUpdate =  segmentedListRepository.FindById("{00000000-0000-0000-0000-000000000000}");
listToUpdate.Description = "New Description";
segmentedListRepository.Update(listToUpdate);

この例は、分割されたリストを削除する方法を示しています。

var segmentedListRepository = ServiceLocator.ServiceProvider.GetService<IFetchRepository<SegmentedListModel>>();

//Find existing list by ID. ID must be valid.

var listToDelete =  segmentedListRepository.FindById("{00000000-0000-0000-0000-000000000000}");
segmentedListRepository.Delete(listToDelete);
## 連絡先リストの追加、更新、削除

この例は連絡先リストの追加方法を示しています。

var contactListRepository = ServiceLocator.ServiceProvider.GetService<IFetchRepository< ContactListModel>>();
var newContactList = new ContactListModel()
       {
           Id = Guid.NewGuid().ToString("B"),
           Name = "New Contact List",
           Description = "Description of the contact list",
           Owner = "Administrator"
        };

作成したいリストに含まれているリストを追加するには、このコードスニペットを使ってください。

newContactList.Subscriptions.Add(new SubscriptionViewModel
            {
              Id = Guid.NewGuid(),
               ListId = <Guid of the List>,
 Name= "Name of the included list"
            });

新しい連絡先リストを保存するには、このコードスニペットを使ってください。

contactListRepository.Add(newContactList);

この例は連絡先リストの更新方法を示しています。

var contactListRepository = ServiceLocator.ServiceProvider.GetService<IFetchRepository< ContactListModel>>();

//Find existing list by ID. ID must be valid.
var listToUpdate =  contactListRepository.FindById("{00000000-0000-0000-0000-000000000000}");
listToUpdate.Description = "New Description";
contactListRepository.Update(listToUpdate);

この例は連絡先リストの削除方法を示しています。

var contactListRepository = ServiceLocator.ServiceProvider.GetService<IFetchRepository< ContactListModel>>();

//Find existing list by ID. ID must be valid.

var listToDelete =  contactListRepository.FindById("{00000000-0000-0000-0000-000000000000}");
contactListRepository.Delete(listToDelete);
## コンタクトの追加・削除

この例は、連絡先リストに1つの連絡先を追加する方法を示しています:

var subscriptionService = ServiceLocator.ServiceProvider.GetService<ISubscriptionService>();
subscriptionService.Subscribe(contactListId, contactId);

この例は、連絡先リストに複数の連絡先を追加する方法を示しています:

var contacts = new List<Contact>(); // List of contacts
var subscriptionService = ServiceLocator.ServiceProvider.GetService<ISubscriptionService>();
subscriptionService.Subscribe(contactListId, contacts);

接触関連を解除し、対応するメソッドを*ISubscriptionService*から呼び出すには:

var subscriptionService = ServiceLocator.ServiceProvider.GetService<ISubscriptionService>();
subscriptionService.Unsubscribe(contactListId, contactId);
subscriptionService.Unsubscribe(contactListId, contacts);

## リストに関連付けられた連絡先の取得

この例は、連絡先リストから連絡先を取得する方法を示しています:

int batchSize = 200; // Size of the batch
string facets =
{
 CollectionModel.FacetKeys.PersonalInformation,
 CollectionModel.FacetKeys.ListSubscriptions
}; // Contact facets to retrieve
var contactListProvider = ServiceLocator.ServiceProvider.GetService<IContactListProvider>();
var contactProvider = ServiceLocator.ServiceProvider.GetService<IContactProvider>();
var contactList = contactListProvider.Get(contactListId, cultureInfo);
var contactBatchEnumerator = contactProvider.GetContactBatchEnumerator(
 contactList,
 batchSize,
 facets);
while (contactBatchEnumerator.MoveNext())
{
var contacts = contactBatchEnumerator.Current;
 // write your logic here
 contacts.ToList();
}

この例では、バッチ列挙子を接触を反復する最も効率的かつ迅速な方法として使っています。詳細は[xConnectのドキュメント](https://doc.sitecore.com/xp/en/developers/104/sitecore-experience-platform/xconnect-and-the-xdb.html)をご覧ください。