I am trying to remove a contact from a contact list using next code:

using (var client = SitecoreXConnectClientConfiguration.GetClient())
{
    var reference = new IdentifiedContactReference(source, email);
    var contact = client.Get(reference, new ContactExpandOptions(new string[]
    {
        "ListSubscriptions"
    }));
    if (contact == null) return;

    Guid contactListId = new Guid(ListId);

    IDefinitionManager<IContactListDefinition> manager = ServiceLocator.ServiceProvider.GetDefinitionManagerFactory().GetDefinitionManager<IContactListDefinition>();


    var contactList = manager.Get(contactListId, new CultureInfo("en"), true);


    contact.RemoveListSubscriptions(client, true, new Guid[]
    {
        contactListId
    });
    client.Submit();
}    

I can see the list in the ListSubscriptions facet but is not removed. Am I doing something wrong?

up vote 6 down vote accepted

We use code like this and it works fine. It is similar to yours however there are some differences:

using (var client = SitecoreXConnectClientConfiguration.GetClient())
{
    try
    {
        var identifier = new IdentifiedContactReference(IdentifierSources.Email, email);
        var contact = client.Get(identifier, new ContactExpandOptions(ListSubscriptions.DefaultFacetKey));
        var contactList = contact.ListSubscriptions()?.Subscriptions?.FirstOrDefault(x => x.ListDefinitionId.Equals(contactListId));

        if (contactList == null)
        {
            return ContactListOperationResultEnum.ContactNotOnTheList;
        }

        var subscriptions = contact.ListSubscriptions();
        subscriptions.Subscriptions.Remove(contactList);
        client.SetListSubscriptions(contact, subscriptions);
        client.Submit();

        return ContactListOperationResultEnum.Success;
    }
    catch (XdbExecutionException ex)
    {
        _logger.Error($"Exception during removing contact from list {contactListId}", ex, this);
        return ContactListOperationResultEnum.Error;
    }
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.