Sitecore 8 EXM 从 listmanager 添加联系人到列表

Sitecore 8 EXM add a contact to list from listmanager

我正在使用 Sitecore 8 和新的电子邮件体验管理器模块。 我已经配置了一个时事通讯电子邮件消息,其中包含列表管理器中的一个空列表作为收件人。

通过自制表格订阅时事通讯时,我会收到一个电子邮件地址和一个姓名。 现在我想用这个邮件和名字建立一个新的联系,并通过代码将它添加到我的列表管理器的列表中。

有没有办法通过 api 调用此列表并向其中添加联系人?

基本上你可以按照这个例子去做

<%@ Page Language="c#" %>
<%@ Import Namespace="Sitecore.Analytics" %>
<%@ Import Namespace="Testing.ContactFacets.Model" %>
<!DOCTYPE html>
<html>
  <head>
    <title>Add Employee Data</title>
  </head>
  <body>
    <%
      var contact = Tracker.Current.Contact;
      var data = contact.GetFacet<IEmployeeData>("Employee Data");
      data.EmployeeId = "ABC123";
    %>
    <p>Employee data contact facet updated.</p>
    <p>Contact ID: <b><%=contact.ContactId.ToString()%></b></p>
    <p>Employee #: <b><%=data.EmployeeId%></b></p>
  </body>
</html>

然后在放弃会话时写入更改,就像这样

<%@ Page language="c#" %>
<script runat="server">
  void Page_Load(object sender, System.EventArgs e) {
      Session.Abandon();
  }
</script> 
<!DOCTYPE html>
<html>
  <head>
    <title>Session Abandon</title>
  </head>
  <body>
  </body>
</html>

关注此 link 获取来源和更多信息 - http://www.sitecore.net/learn/blogs/technical-blogs/getting-to-know-sitecore/posts/2014/09/introducing-contact-facets

创建联系人,您可以使用下面的示例代码,联系人姓名通常是域名加上用户名,例如domain\username.

public static Contact CreateContact([NotNull] string contactName, [NotNull] string contactEmail, [NotNull] string contactLanguage)
{
  Assert.ArgumentNotNullOrEmpty(contactName, "contactName");
  Assert.ArgumentNotNullOrEmpty(contactEmail, "contactEmail");
  Assert.ArgumentNotNullOrEmpty(contactLanguage, "contactLanguage");

  var contactRepository = new ContactRepository();

  var contact = contactRepository.LoadContactReadOnly(contactName);
  if (contact != null)
  {
    return contact;
  }
  contact = contactRepository.CreateContact(ID.NewID);
  contact.Identifiers.AuthenticationLevel = AuthenticationLevel.None;
  contact.System.Classification = 0;
  contact.ContactSaveMode = ContactSaveMode.AlwaysSave;
  contact.Identifiers.Identifier = contactName;
  contact.System.OverrideClassification = 0;
  contact.System.Value = 0;
  contact.System.VisitCount = 0;

  var contactPreferences = contact.GetFacet<IContactPreferences>("Preferences");
  contactPreferences.Language = contactLanguage;

  var contactEmailAddresses = contact.GetFacet<IContactEmailAddresses>("Emails");
  contactEmailAddresses.Entries.Create("test").SmtpAddress = contactEmail;
  contactEmailAddresses.Preferred = "test";

  var contactPersonalInfo = contact.GetFacet<IContactPersonalInfo>("Personal");
  contactPersonalInfo.FirstName = contactName;
  contactPersonalInfo.Surname = "recipient";

  contactRepository.SaveContact(contact, new ContactSaveOptions(true, null));

  return contact;
}

创建联系人后,使用以下示例代码将联系人添加到收件人列表。

  var repository = new ListManagerCollectionRepository();
  var recipientList = repository.GetEditableRecipientCollection(recipientListId);
  if (recipientList != null)
  {
    var xdbContact = new XdbContactId(contactId);
    if (!recipientList.Contains(xdbContact, true).Value)
    {
      recipientList.AddRecipient(xdbContact);
    }
  }

我遇到了完全相同的问题,即列表管理器在将联系人添加到收件人列表后报告 0 个联系人。

我仔细调查了这个问题,发现将联系人添加到收件人列表实际上只是在 "sitecore_analytics_index" 索引中的联系人上设置了一个字段(假设您使用 Mongo/XDB 作为底层存储).具体来说,Sitecore 应使用值 "ContactLists:{recipientListGuid}" 更新联系人文档中的 "contact.tags" 字段。我尝试用 Luke 打开索引以验证该字段确实没有在索引中设置。索引位于C:\inetpub\wwwroot[Sitename]\Data\indexes\sitecore_analytics_index.

这让我得出结论,您必须在将联系人添加到收件人列表后保存联系人。

总而言之,以下代码对我有用:

var ecm = EcmFactory.GetDefaultFactory();
XdbContactId contactId = /* some valid contact id */;
LeaseOwner leaseOwner = new LeaseOwner("UpdateContact-" + Guid.NewGuid().ToString(), LeaseOwnerType.OutOfRequestWorker);
Sitecore.Analytics.Tracking.Contact contact;
string webClusterName;
var status = ecm.Gateways.AnalyticsGateway.TryGetContactForUpdate(contactId.Value,
    leaseOwner,
    TimeSpan.FromSeconds(5),
     TimeSpan.FromSeconds(5),
     out contact, out webClusterName);

var recipientList = ecm.Bl.RecipientCollectionRepository.GetEditableRecipientCollection(recipientListId);
  if (recipientList != null)
  {
    if (!recipientList.Contains(contactId, true).Value)
    {
      recipientList.AddRecipient(contactId);
    }
  }

contact.ContactSaveMode = ContactSaveMode.AlwaysSave;
var contactRepository = new ContactRepository();
var success = contactRepository.SaveContact(contact, new ContactSaveOptions(true, leaseOwner));

注意,以上代码用于更新场景。在你的情况下,我想你只需要移动这段代码:

contactRepository.SaveContact(contact, new ContactSaveOptions(true, null));

之后:

var recipientList = EcmFactory.GetDefaultFactory().Bl.RecipientCollectionRepository.GetEditableRecipientCollection(recipientListId);
  if (recipientList != null)
  {
    var xdbContact = new XdbContactId(contactId);
    if (!recipientList.Contains(xdbContact, true).Value)
    {
      recipientList.AddRecipient(xdbContact);
    }
  }

更新:实际上,只有当保存的联系人是 Sitecore Analytics 当前跟踪的联系人时,以上内容才有效。

如果您有可用的跟踪器并且不需要立即更新,则以下应该有效(请注意,联系人会在会话到期时添加到列表中):

//private const string ContactListTagName = "ContactLists";

var contact = Tracker.Current.Contact;

// Identify
if (contact.Identifiers.IdentificationLevel < ContactIdentificationLevel.Known)
{
    Tracker.Current.Session.Identify(email);
}

// Set Email
var contactEmail = contact.GetFacet<IContactEmailAddresses>("Emails");

// Create an email address if not already present

// This can be named anything, but must be the same as "Preferred" if you want
// this email to show in the Experience Profiles backend. 
if (!contactEmail.Entries.Contains("Preferred"))
    contactEmail.Entries.Create("Preferred");

// set the email
var emailEntry = contactEmail.Entries["Preferred"];
emailEntry.SmtpAddress = email;
contactEmail.Preferred = "Preferred";

// set FirstName and Surname (required for List Manager, "N/A" might not be ideal but I don't know how Sitecore behaves with empty strings)
var personal = contact.GetFacet<IContactPersonalInfo>("Personal");
personal.FirstName = personal.FirstName ?? "N/A";
personal.Surname = personal.Surname ?? "N/A";

// Add preferred language
var preferences = contact.GetFacet<IContactPreferences>("Preferences");
preferences.Language = Context.Language.Name;

// Here is the actual adding to the list by adding tags
using (new SecurityDisabler())
{
    var id = ID.Parse("CONTACTLISTID");
    contact.Tags.Set(ContactListTagName, id.ToString().ToUpperInvariant());
}

您好, 马库斯