使用 C# 代码将收件人添加到现有收件人列表 - Sitecore Email campaign manager

Add recipients to existing recipient list with C# code - Sitecore Email campaign manager

我想使用代码将新收件人添加到现有收件人列表中。我尝试使用以下代码,但没有用。

TargetAudience recipientList = Factory.GetTargetAudience("RecipientListId");
if ((recipientList != null))
{
       Contact contact = //I dont know how to create object for this, because it is protected class
       contact.Profile.Email = "my Email";
       contact.Profile.Name = "My Name";
       contact.Profile.FullName = "Full Name";
       recipientList.Subscribers.Add(contact);
 }

请帮助我实现这个目标,

提前致谢

您可以从用户的用户名中获取联系人。 此方法通过电子邮件地址获取联系人,并包含从电子邮件地址获取用户名的代码。

 public Contact GetContact(string email)
 {
        // managerRoot is the top level ECM item
        ManagerRoot managerRootFromId = Factory.GetManagerRootFromID(managerRoot.ID.ToString());
        var username = Util.AddressToUserName(email);
        string commonDomain = managerRootFromId.Settings.CommonDomain;
        string fullName = commonDomain + "\" + Util.AddressToUserName(username);

        if (User.Exists(fullName))
        {
            return Contact.FromName(fullName);
        }
        return null;
    }

然后您应该可以将联系人添加到订阅列表中。

或者一旦您有了联系人,您就可以设置配置文件值并使用订阅方法。

contact.InnerUser.Profile["Fullname"] = string.Format("{0} {1}",person.Firstname,person.Surname);
contact.Subscribe(subscriptionLists);

您还可以通过使用以下代码提供电子邮件地址作为本地名称来添加 ECM 用户。

 protected static Contact CreateAnonymousECMUser(string localName, ManagerRoot root)
 {
        Contact contact = (Contact)null;
        if (root != null && !string.IsNullOrEmpty(localName))
        {
            string commonDomain = root.Settings.CommonDomain;
            Assert.IsNotNullOrEmpty(commonDomain, EcmTexts.Localize("The Common Domain setting is not set.", new object[0]));
            string str = commonDomain + "\" + Util.AddressToUserName(localName);
            while (User.Exists(str))
                str = str + "_";
            string password = new PasswordGenerator()
            {
                MinimumCharacters = 14
            }.Generate();
            System.Web.Security.Membership.CreateUser(str, password, localName);
            contact = Contact.FromName(str);
            contact.Profile.ProfileItemId = root.Settings.SubscriberProfile;
            contact.Profile.Save();
        }
        return contact;
  }