3

I know how to programmatically add contacts to a contact list, either through the contact itself, or through the List Manager API:

xContact.SetListSubscriptionsFacet(xConnectClient, listId);

Source: https://www.brimit.com/blog/sitecore-9-list-manager-changes

var subscriptionService = serviceLocator.ServiceProvider.GetService<ISubscriptionService>();
subscriptionService.Subscribe(contactListId, contactId);

Source: https://doc.sitecore.net/sitecore_experience_platform/digital_marketing/the_list_manager/the_list_manager_api

But none of these services have anything on how to programmatically create a list. I can probably do it a dirty way by manually adding an item to /sitecore/system/Marketing Control Panel/Contact Lists (based on Contact List template), but if Sitecore needs to do some indexing or other things when creating lists, this will of course not be enough.

So how can I programmatically create a contact list?

Sitecore 9.0.2 (rev. 180604)

2 Answers 2

4

Here is the code which allows to create a contact list programatically in Sitecore:

private Guid CreateContactList()
{
    ContactListModel entity = new ContactListModel();
    Guid guid = Guid.NewGuid();
    entity.Id = guid.ToString();
    entity.Type = ListType.ContactList.ToString();
    entity.Name = "My new contact list";

    IRepository<ContactListModel> contactListRepository = (IRepository<ContactListModel>) ServiceLocator.ServiceProvider.GetService(typeof(IRepository<ContactListModel>));
    contactListRepository.Add(entity);
    return guid;
}

Remember to add references to:

  • Sitecore.ListManagement.Services.dll
  • Sitecore.Marketing.dll
  • Sitecore.Services.Core.dll
2
  • Even though JacobNielsen's code was shorter, yours actually worked! My comment under his answer has many questions, and your code solved all of them.
    – GTHvidsten
    Nov 6, 2018 at 12:52
  • Can I edit the destinationpath of the contactlist? I.e. I want it one step further down inside a folder. ../../ContactLists/FolderA/mynewcontactlist Jun 14, 2019 at 11:53
6

For Sitecore 9.x, you need to go through the IDefinitionManager<IContactListDefinition> from Sitecore.Marketing.dll. As an example:

string name = ItemUtil.ProposeValidItemName("name");
var listId = Guid.NewGuid();
string alias = ItemUtil.ProposeValidItemName(FormattableString.Invariant($"{name}-{listId}"));; //alias must be unique for each language variant
CultureInfo cultureInfo = Context.Language.CultureInfo;
string createdBy = Sitecore.Context.User.Name;

var definitionManager = ServiceLocator.ServiceProvider.GetService<IDefinitionManager<IContactListDefinition>>();
definitionManager.SaveAsync(new ContactListDefinition(listId, alias, cultureInfo, name, DateTime.UtcNow, createdBy), true);

A few notes:

The alias must be unique, which is why List Manager by default appends the item ID. It's not required to append the ID though, as long as the alias is unique.

In List Manager you will see lists created in the context language of the user, so you'll want to make sure you're setting creating the list using the corresponding CultureInfo.

8
  • just out of curiosity, is the method from my answer no longer valid in Sitecore 9?
    – Marek Musielak
    Nov 6, 2018 at 10:27
  • @MarekMusielak No, you're right, yours is valid too. I'm happy to delete this answer if you think? Nov 6, 2018 at 10:48
  • 1
    Do not delete your answer. It's only 2 lines while mine is much longer. I was just curious if mine is not correct. You're the expert! :)
    – Marek Musielak
    Nov 6, 2018 at 10:49
  • @JacobNielsen Thanks! A few questions: I notice that lists created from List Manager have the item ID appended to the item Name, and the item created from your code does not do this. Is this important and I should append the ID myself, or does it not matter? I also noticed that the item created from your code was added as "English (US)" language item, and the ones created from List Manager are "English (region)". (I'm using CultureInfo.CurrentCulture where you have culture) Why is that? And finally I created a new list, but I can't see it in ListManager. Anything else I need to do?
    – GTHvidsten
    Nov 6, 2018 at 12:25
  • @GTHvidsten I've updated the answer to clarify Nov 6, 2018 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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