ユーザー様が連絡先のファセットを追加で表示させるようList Managerを拡張する際に、困難に直面する場合があります。
本記事では、Contactsテーブルにカラムを追加するためのアルゴリズムについて説明します。
重要:変更を適用する前に、ソリューションのバックアップを実施してください。
public class ContactDataModel : Sitecore.ListManagement.Services.Model.ContactDataModel2. データは、Sitecore.ListManagement.Services.Repositories.ListSubscriptionsStoreクラスでマップされます(IL Spy、.NET Reflectorまたは他のリバース エンジニアリング ツールでコードを取得します)。次のように、カスタム モデルのIEnumberableを返すGetSubscribersメソッドを持つ独自のクラスを作成します。
{
public string FavouriteBrand { get; set; }
}
public IEnumerable<CustomNamespace.ContactDataModel> GetSubscribers(Guid listId, string searchFilter, int pageIndex, int pageSize)備考:データは_contactProviderから取得し、MapEntityメソッドで特定のモデルに変換します。
{
ContactList contactList = this.EnsureList(listId, null);
ContactSearchResult result = this._contactProvider.GetFilteredContacts(contactList, searchFilter, pageIndex, pageSize);
List<CustomNamespace.ContactDataModel> source = result.Contacts.Select<Contact, CustomNamespace.ContactDataModel>(new Func<Contact, CustomNamespace.ContactDataModel> (this.MapEntity)).ToList<CustomNamespace.ContactDataModel>();
if (source.Any<ContactDataModel>())
{
source[0].Count = result.Count;
}
return source;
}
CustomNamespace.ContactDataModel model1 = new CustomNamespace.ContactDataModel();_contactProvider.GetFilteredContactsメソッドは、3つファセット(ListSubscriptions, Personal, Emails)のみを返します。従って、カスタムのものをリストに追加します:
Guid? id = entity.Id;
model1.Id = (id.HasValue ? id.GetValueOrDefault() : Guid.Empty).ToString();
model1.Email = (entity.Emails()?.PreferredEmail == null) ? null : ((entity.Emails() == null) ? null : (entity.Emails().PreferredEmail)).SmtpAddress;
model1.FirstName = entity.Personal()?.FirstName;
model1.LastName = entity.Personal()?.LastName;
model1.FavouriteBrand = entity.GetFacet<MarketingDataFacet>().FavouriteBrand;
ContactIdentifier identifier = entity.Identifiers.FirstOrDefault<ContactIdentifier>(x => (x.Source == "ListManager")) ?? entity.Identifiers.FirstOrDefault<ContactIdentifier>();
model1.Identifier = identifier?.Identifier;
model1.IdentifierSource = identifier?.Source;
return model1;
IContactSource contactSource = this._contactSourceFactory.GetSource(contactList);3. Sitecore.ListManagement.Services.Controllers.ContactsControllerコントローラは、デフォルトのListSubscriptionsStoreを使用します。同じアクションを実行するカスタム コントローラを作成し、カスタムのListSubscriptionsStoreを使用するようGetEntriesメソッドを変更します。
FilteringSource source2 = new FilteringSource(contactSource, searchFilter);
string[] facets = new string[] { "ListSubscriptions", "Personal", "Emails", "MarketingDataFacet" };
return new ContactSearchResult(this._segmentationService.GetContacts(source2, pageSize * pageIndex, pageSize, facets), (int)this._segmentationService.GetCount(contactSource));
public class CustomContactsController : ServicesApiController4. コントローラのRoutePrefix属性値をsitecore/api/customlists/{listId}/contactsに変更します。
{
// コントローラにパラメーターなしのコンストラクタを追加します。
public CustomContactsController()
{
var clp = Sitecore.DependencyInjection.ServiceLocator.ServiceProvider.GetService<IContactListProvider>();
var sp = Sitecore.DependencyInjection.ServiceLocator.ServiceProvider.GetService<ISubscriptionService>();
var csf = Sitecore.DependencyInjection.ServiceLocator.ServiceProvider.GetService<IContactSourceFactory>();
var sss = Sitecore.DependencyInjection.ServiceLocator.ServiceProvider.GetService<ISegmentationService>();
var cp = new Sitecore.Support.ListManagement.XConnect.ContactProvider(sss, csf); // create an object of the custom ContactProvider class
var or = Sitecore.DependencyInjection.ServiceLocator.ServiceProvider.GetService<IListOperationRepository>();
var lss = Sitecore.DependencyInjection.ServiceLocator.ServiceProvider.GetService<IListSubscriptionsStore<ContactDataModel>>();
int batchSize = Sitecore.Configuration.Settings.GetIntSetting("ListManagement.BatchSize", 250);
this._listSubscriptionsStore = new CustomListSubscriptionsStore(clp, sp, cp, or, batchSize); // カスタム ストアを作成します。
}
..........
[Route]
[HttpGet]
[ActionName("MyDefaultAction")]
public virtual IEnumerable<CustomNamespace.ContactDataModel> GetEntities(Guid listId, string filter = "", int pageIndex = 0, int pageSize = 20)
{
try
{
return this._listSubscriptionsStore.GetSubscribers(listId, filter, pageIndex, pageSize); // カスタム ストアはここで使用します。
}
catch (Exception exception) when (!(exception is ContactListNotFoundException))
{
this._log.Error(string.Format(CultureInfo.InvariantCulture, "[ListManagement]: Failed to get count of contacts for {0} contact list. The error has been occurred: {1}", listId, exception.Message), exception, this);
return Enumerable.Empty<ListManagmentViewFacets.ContactDataModel>();
}
}
}
public class RegisterHttpRoutes6. アセンブリを構築し、サイトのbinフォルダに配置します。
{
public void Process(PipelineArgs args)
{
GlobalConfiguration.Configure(Configure);
}
protected void Configure(HttpConfiguration configuration)
{
var routes = configuration.Routes;
routes.MapHttpRoute("CustomContacts", "sitecore/api/customlists/{listId}/contacts", new
{
controller = "CustomContacts",
action = "MyDefaultAction", // ActionName属性でのアクションの名称
});
}
}
上記の手順を実施した後、カスタム ファセットを含むCSVリストのアップロードが可能になります。
備考:このアルゴリズムは一例としてご紹介しているものです。貴社のご要件に応じて、拡張・変更することが可能です。