We all started somewhere! Share your experience on the Get Advice: Let's Get Started Sweepstakes thread and be entered to win a $100 credit on your Constant Contact account.

C# SDK -- Remove contact from specific list

ChadH57
Regular Participant

C# SDK -- Remove contact from specific list

I am trying to remove a single contact from a specific list.  I am using the C# SDK but do not see a method that will allow me to do this.   

 

Any help would be appreciated.

 

13 REPLIES 13
ChadH57
Regular Participant

I have scoured the support forum and google.  I think that this functionality is made a little more complicated that it needs to be (since many people ask for it and expect this normal functionality to be a part of the SDK), but maybe I am missing something.

In the following code, I want to add to the NewList and remove from the NoActList.

 

    private void AddToNew(Contact contact, List<ContactList> contactLists)
    {
        List<string> email = new List<string>();
        email.Add(txtEmail.Text.ToLower());
        ContactOptInList NewList = new ContactOptInList();
        NewList.ContactList = new ConstantContactBO.ContactList("5");

        ContactOptInList NoActList = new ContactOptInList();
        NoActList.ContactList = new ConstantContactBO.ContactList("6");

        string _nextChunkId;
        IList<Contact> lstContact = Utility.SearchContactByEmail(authdata, email, out _nextChunkId);

        //if they are already in a list
        if (lstContact.Count > 0)
        {
            Contact c = Utility.GetContactDetailsById(authdata, lstContact[0].Id);
            c.OptInSource = ContactOptSource.ActionByCustomer;
            c.ContactLists.Add(NewList);
            c.ContactLists.Remove(NoActList);

            try
            {
                Utility.UpdateContactFullForm(authdata, c);
            }
            catch (Exception ex)
            {
            }
        }

 

 

Why doesn't this work?


Chad,

 

To update a contact it would be best to do a GET on the contact that way the XML you get is well formed.  You update the nodes you want to change.  In this case, you would be changing the ContactLists node reflect what lists this contact would be on and then complete a PUT to update the contact.

 

If this is not working for you, please reply with the error code and response you are receiving as well as the XML you are sending to us.

 

WIth this, we should be able to look into why this is not working properly for you.

 

Thanks, 

 

 

Andrew Weber
Associate Product Manager for EventSpot
ChadH57
Regular Participant

Can you provide a link to constant contact documentation performing the method you are suggesting?

Chad,

 

The code below will search for all contacts with a specific email and remove it from all lists but list 22.

 

            string _nextChunk;

            List<string> myEmail = new List<string>();

            myEmail.Add("aweber@constantcontact.com");
            IList<Contact> myList = Utility.SearchContactByEmail(authdata, myEmail, out _nextChunk);

            ContactOptInList newList = new ContactOptInList();
            newList.ContactList = new ConstantContactBO.ContactList("22");

            foreach (Contact contact in myList)
            {
                contact.ContactLists.Add(newList);
                Utility.UpdateContactFullForm(authdata, contact);
            }

 

Andrew Weber
Associate Product Manager for EventSpot
ChadH57
Regular Participant

Thanks.  

 

So what you are telling me is that there is no way to actually remove someone from a specific email list without getting all of the lists they are subscribed to, "adding" those sans the one list you don't want them in, and resubmitting them?  

 

Seriously?

Hey Chad,

 

You are correct.  Our API takes the submitted XML for Lists and completely wipes out the old data for the new data.

Andrew Weber
Associate Product Manager for EventSpot

Hi Andrew,

 

"Our API takes the submitted XML for Lists and completely wipes out the old data for the new data."

 

This behavior isn't what I just experienced. I am trying to move a contact from a list to another and I am doing so by sending a PUT request with the list node only containing one list (the list to move the contact to).

 

What I experienced is that the API returns a contact assigned to no list (list node empty), while the website shows the contact in both list (the old and the new one).

 

Could you help?

 

Regards,

Richard

Hello Richard,

 

It is the intended behavior that a contact should change to match the data that is sent by the application and what you are seeing is not the intended or normal behavior. In ordre to look into what is happening, we will need details on the contact that you are working on, as well as the account that you are working with.

 

If it is possible, it would be best to email us at webservices@constantcontact.com with the following info:

 

- Username for the Constant Contact account where you are having issues

- Email address of the contact that is being worked on

- If possible, the contact ID of the contact that is being worked on

 

Best Regards,

 

Elijah G.
API Support Engineer
JudyC10
Occasional Participant

Contact c = Utility.GetContactDetailsById(authdata, ContactID);

ContactOptInList newList = new ContactOptInList();

 

newList.ContactList = new ConstantContactBO.ContactList("33");
c.ContactLists.Add(newList);

 

c.OptInSource = ContactOptSource.ActionByCustomer;
Utility.UpdateContactFullForm(authdata, c);

 

So based on what you've posted in this thread this contact with a contactid of ContactID should only be on list 33?

Because when I execute this code they are still in a group they were previously assigned to.

ChadH57
Regular Participant

Julie,

 

I use the following code, and it works about 3/4 of the time.  It's frustrating.  BTW listnum param is the numerical id of your lists.  

 private void AddToList(string listNum)
    {

        List<string> email = new List<string>();
        email.Add(txtEmail.Text.ToLower());

        ContactOptInList theList = new ContactOptInList();
        theList.ContactList = new ConstantContactBO.ContactList(listNum);
        theList.OptInSource = ContactOptSource.ActionByCustomer;

        string _nextChunkId;
        IList<Contact> lstContact = Utility.SearchContactByEmail(authdata, email, out _nextChunkId);

        //if they are already in a list
        if (lstContact.Count > 0)
        {
            string _nextChunk;

            List<string> myEmail = new List<string>();

            myEmail.Add(txtEmail.Text.ToLower());
            IList<Contact> myList = Utility.SearchContactByEmail(authdata, myEmail, out _nextChunk);

            foreach (Contact c in myList)
            {
                c.ContactLists.Add(theList);

                try
                {
                    Utility.UpdateContactFullForm(authdata, c);
                }
                catch (Exception ex)
                {
                }
            }
        }
        else
        {
            Contact c = new Contact();
            c.EmailAddress = txtEmail.Text.ToLower();
            c.OptInSource = ContactOptSource.ActionByCustomer;
            c.ContactLists.Add(theList);

            try
            {
                Utility.CreateNewContact(authdata, c);
            }
            catch (Exception ex)
            {
            }
        }



    }

 

 

Feel free to give our API support a shout if you're still having issues with this, and we can jump in and take a look.

Nick Galbraith
Support Engineer
JudyC10
Occasional Participant

The above code also deletes the FirstName and LastName (probably more fields) of the contact. For as much information about your SDK and your Wrapper you supply there is a lot missing. I would think that removing a contact from a list would be a pretty common function.

Hi Judy,

 

Your code is pretty close to correct, though it should not remove any information you already have (such as FirstName or LastName).  You would just need to add one line.  This is because in C#, the .add() method just adds another item to the collection, not overwriting or changing.  Without clearing the list, the API would correctly just append the new list and keep all the original lists.

 

Since you're pulling all of the data from us, including the original FirstName and LastName, you should not be losing any data in the update here.  I haven't experienced this in my tests of the C# wrapper, all my updates using your code are correctly keeping the information the same.  If you keep running into that problem, plesae check to make sure that your Contact c correctly has the values in it after calling Utility.GetContactDetailsById(authdata, ContactId) method before you're sending the update. 

 

Contact c = Utility.GetContactDetailsById(authdata, ContactID);

ContactOptInList newList = new ContactOptInList();

 

newList.ContactList = new ConstantContactBO.ContactList("33");

c.ContactLists.Clear(); // Add this line to clear out original lists, otherwise you are appending instead of changing

c.ContactLists.Add(newList);

 

c.OptInSource = ContactOptSource.ActionByCustomer;

Utility.UpdateContactFullForm(authdata, c);

Dave Berard
Senior Product Manager, Constant Contact
Developer Portal

View API documentation, code samples, get your API key.

Visit Page