4

I need to programmatically add contact to a contact list. How to do this in Sitecore 9.0.1? I find a documentation. Here it says this below line will add contact to a list.

    Contact contact = client.Get<Contact>(
        new ContactReference(contactId),
        new ContactExpandOptions(new string[1] { "ListSubscriptions" })
    );

    contact.SetListSubscriptionsFacet(client, listId);

    client.Submit();

But I don't see this contact on my list in List Manager. Do I need to do more thing to see this contact?

0

2 Answers 2

3

Besides implementing the chorpo's code, please try enabling the indexing of anonymous contact data. To do that, please go to the following file locations of your xconnect site:

\App_data\config\sitecore\SearchIndexer\sc.Xdb.Collection.IndexerSettings.xml

\App_data\jobs\continuous\IndexWorker\App_data\Config\Sitecore\SearchIndexer\sc.Xdb.Collection.IndexerSettings.xml

Then change the following tag in to true.

<IndexAnonymousContactData>true</IndexAnonymousContactData>

After that rebuild the xDb:

Go to the .xconnect\Website\App_data\jobs\continuous\IndexWorker and open the console window in administrator mode and run the command XConnectSearchIndexer -rr

You should see the contacts now.

6

With your code you are always changing facet value on contact so contact will be part of only one contact list (last one assigned) at the end.

You are also effectively replacing all other list subscriptions to that particular contact. This is bad practice.

You need to use this code snippet to achieve it:

ListSubscriptions subscriptions = new ListSubscriptions();
var listId = Guid.NewGuid(); /* Replace with real list ID */
var isActive = true;
var added = DateTime.UtcNow;

ContactListSubscription subscription = new ContactListSubscription(added, isActive, listId);

subscriptions.Subscriptions.Add(subscription);
client.SetListSubscriptions(contact, subscriptions);

Taken from Sitecore xConnect docu page

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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