0

I work with Sitecore 9.1, and I added new contact list from list manager my new list name Subscript_list, I want to save contact information (Email, first name, and Last name) programmatically, I created Form contains the fields (Email, first name, and Last name),I found many articles, but the articles focus to save data to XConnect and send email using EXM Module.

How can I save data from my form directly to my new contact list?

Thanks in Advance.

enter image description here

3

2 Answers 2

2

You should create new Contact and add a new subscription to ListSubscriptions facet.

Create contact:

       public async Task<bool> CreateContact(
            string source,
            string identifier,
            string firstName,
            string lastName,
            string email)
        {
            using (XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    IdentifiedContactReference reference = new IdentifiedContactReference(source, identifier);

                    var contactTask = client.GetAsync(
                        reference,
                        new ContactExpandOptions()
                    );

                    Contact existingContact = await contactTask;

                    if (existingContact != null)
                    {
                        return false;
                    }

                    var contactIdentifiers = new[]
                    {
                        new ContactIdentifier(source, identifier, ContactIdentifierType.Known),

                    };

                    Contact contact = new Contact(contactIdentifiers);

                    var personal = new PersonalInformation
                    {
                        FirstName = firstName,
                        LastName = lastName
                    };


                    var preferredEmail = new EmailAddress(email, true);
                    var emails = new EmailAddressList(preferredEmail, "Work");

                    client.AddContact(contact);
                    client.SetPersonal(contact, personal);
                    client.SetEmails(contact, emails);

                    await client.SubmitAsync();

                    return true;
                }
                catch (XdbExecutionException ex)
                {
                    _logService.Error(ex.Message, ex);
                    return false;
                }
            }
        }

Subscribe contact to list:

 public async Task<bool> SubscribeContact(string source, string identifier, Guid listId)
        {
            using (XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
            {
                try
                {
                    IdentifiedContactReference reference = new IdentifiedContactReference(source, identifier);

                    var contactTask = client.GetAsync(
                        reference,
                        new ContactExpandOptions(
                            ListSubscriptions.DefaultFacetKey
                            )
                    );

                    Contact contact = await contactTask;

                    if (contact == null)
                    {
                        return false;
                    }

                    if (contact.Identifiers.All(x => x.Source != "ListManager"))
                        client.AddContactIdentifier(contact, new ContactIdentifier("ListManager", Guid.NewGuid().ToString(), ContactIdentifierType.Known));

                    var subscriptions = contact.ListSubscriptions() ?? new ListSubscriptions();
                    var subscription = new ContactListSubscription(DateTime.UtcNow, true, listId);
                    subscriptions.Subscriptions.Add(subscription);
                    client.SetListSubscriptions(contact, subscriptions);   

                    await client.SubmitAsync();

                    return true;
                }
                catch (XdbExecutionException ex)
                {
                    _logService.Error(ex.Message, ex);
                    return false;
                }
            }
        }

4
  • Thanks for your answer, but can you please tell me what the value of parameters (source and identifier ) public async Task<bool> SubscribeContact(string source, string identifier, Guid listId) Commented May 16, 2019 at 13:05
  • Contact can have several sources for identification. In your case, you can just create a constant for source "public const string IdentificationSource = "MyApplication"; " and use it instead of 'source'. As identifier you can use Email for example (or any other unique identifier of user). Something like " new ContactIdentifier("MyApplication", "[email protected]", ContactIdentifierType.Known)". Source and identifier will be used by you in future to retrieve Contact from xConnect, or to identify user on your website "Tracker.Current.Session.IdentifyAs(source, identifier)".
    – x3mxray
    Commented May 16, 2019 at 15:02
  • Thank you so much, I will use this code to Creating a custom submit action for my Form , I used Sitecore Forms Thanks you again! Commented May 16, 2019 at 15:18
  • how can I call the 2 task (Create contact) and (Subscribe contact to list), inside controller ActionResult ? Commented May 22, 2019 at 15:32
0

To update your answer for saving on Stack Exchange, here is a polished version:


To add contacts, you need to create a CSV contact list and upload it into the List Manager. Ensure your CSV file includes all required fields such as email, phone number, first name, and last name.

The Contact List is essentially a list of contacts sourced from xConnect. When you update a contact in xConnect, these changes will automatically reflect in the Contact List.

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.