I've recently received a simple request to create a newsletter component. It was expected to get visitor's email, save it as contact and add it to newsletter list.
I've found couple of example while searching but they were developed for old Sitecore versions. I was searching if there is useful API provided by Sitecore.
And yes, I've found what I was looking for: The List Manager API
The code above seems clean and simple. But there is a problem: Where is that "ISubscriptionService"? There is no detail about which library we should use.
I had a look at some of the libraries using dotPeek and found then. It is inside Sitecore.ListManager.XConnect
This API has another useful method called "CreateSubscribedContact" to create contact but it was not suitable for my case. Because I was only had visitor's email and this was expecting some other details. So, I created contacts with another way and then added them to the list via "Subscribe" method. You can have a look at how it looks like:
Hope, this saves someone's time :)
I've found couple of example while searching but they were developed for old Sitecore versions. I was searching if there is useful API provided by Sitecore.
And yes, I've found what I was looking for: The List Manager API
var subscriptionService = ServiceLocator.ServiceProvider.GetService<ISubscriptionService>();
subscriptionService.Subscribe(contactListId, contactId);
The code above seems clean and simple. But there is a problem: Where is that "ISubscriptionService"? There is no detail about which library we should use.
I had a look at some of the libraries using dotPeek and found then. It is inside Sitecore.ListManager.XConnect
public void CreateSubscribedContact(Guid listId, string identifier, string source, string firstName, string lastName, string email) { Assert.ArgumentNotNullOrEmpty(identifier, nameof (identifier)); Assert.ArgumentNotNullOrEmpty(source, nameof (source)); Assert.ArgumentNotNullOrEmpty(firstName, nameof (firstName)); Assert.ArgumentNotNullOrEmpty(lastName, nameof (lastName)); Assert.ArgumentNotNullOrEmpty(email, nameof (email)); Assert.ArgumentCondition((listId != Guid.Empty ? 1 : 0) != 0, nameof (listId), FormattableString.Invariant(FormattableStringFactory.Create("{0} is not a valid Guid.", (object) nameof (listId)))); this._xdbRequestPerformer.RequestWithRetry((Action<IXdbContext>) (client => { Contact contact = client.Get<Contact>((IEntityReference<Contact>) new IdentifiedContactReference(source, identifier), (ExpandOptions) new ContactExpandOptions(new string[3] { "ListSubscriptions", "Personal", "Emails" })); if (contact == null) { contact = new Contact(new ContactIdentifier[1] { new ContactIdentifier(source, identifier, ContactIdentifierType.Known) }); client.AddContact(contact); } SubscriptionService.SetPersonalFacet(firstName, lastName, contact, client); SubscriptionService.SetEmailFacet(email, contact, client); contact.SetListSubscriptionsFacet(client, listId, new Guid()); client.Submit(); }), "create subscribed contacts"); }
Hope, this saves someone's time :)
Comments
Post a Comment