Skip to main content

Working with Sitecore List Manager API (Version 9.0.1)

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

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


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:

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

Popular posts from this blog

Sitecore Commerce – XC9 Tips – Missing Commerce Components in SXA Toolbox on Experience Editor

I've recently had an issue that commerce components were missing in SXA Toolbox. I setup Sitecore Commerce on top of an existing instance and I already had a SXA website working on it. The idea was to add commerce components and functionality to my existing website. But after commerce setup, the toolbox was still showing default SXA components and commerce components were missing although I add commerce tenant and website modules: I checked Available Renderings under Presentation folder, there was no problem, commerce renderings were there. I created another tenant and website to see if it shows the commerce components in toolbox. Nothing seemed different but I was seeing commerce components for new website and it was missing on existing one. Then, I noticed two things: 1- Selected catalog was empty in content editor (/sitecore/Commerce/Catalog Management/Catalogs) even if I see Habitat_Master catalog in Merchandising section on commerce management panel. 2- Bootstrap ...

Modern Observability for Sitecore 10.4 on AKS: Grafana, Alloy, Loki, and Prometheus

In this post, I’ll walk through how I extended the base Sitecore XM 10.4 AKS setup with a modern observability stack using Grafana, Alloy, Loki, and Prometheus. This setup provides deep insights into both infrastructure and application health, with powerful log aggregation and visualization. Project Overview Base:  Sitecore XM 10.4 running on Azure Kubernetes Service (AKS) Enhancements:  Added a full Grafana observability stack: Grafana  for dashboards and visualization Alloy  (Grafana Alloy, formerly Promtail) for log collection and multiline parsing Loki  for log aggregation and querying Prometheus  for metrics collection All configuration...

Sitecore 9 - Creating Custom Segmentation Rule

Predicates are important to create rules for marketing automation and segmented lists. Before going through the details, I am going to remind what I am implementing again. Imagine that you have a website displaying movies. Visitors are able to see movie details and take some actions like save movie or share it.  You want to follow the visitors' activities and you want to take some marketing actions based on those activities. For example, if a contact visits a movie more than X time or she/he saves a movie, you want to send those movies to an external system. In addition, there is going to be a limit to send same movie. Such as, it will not be possible to send same movie more than 2 times.  According to my scenario, I need a rule to filter contacts who visits a movie more than X time. Maybe, we can add one more condition there like in Y days. To creating rule will give flexibility to marketing managers that then they can easily add or remove multiple rules and procee...