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

1

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;
                }
            }
        }

  • 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) – Raed Abu Garbeiah May 16 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", "testUser@gmail.com", 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 May 16 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! – Raed Abu Garbeiah May 16 at 15:18
  • how can I call the 2 task (Create contact) and (Subscribe contact to list), inside controller ActionResult ? – Raed Abu Garbeiah May 22 at 15:32

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.