cancel
Showing results for 
Search instead for 
Did you mean: 

V2 API How to Add Lists to an Existing Contact?

Highlighted
Advisor

V2 API How to Add Lists to an Existing Contact?

I am puzzled as to how to go about placing an existing contact into a list that it isn't already in via Version 2 apis. I am using the C# wrapper and have no experience using the low level APIs. But if that is the only way to do this, I would appreciate knowing it.

 

Here is the situation. A user can add  and/or remove themselves from various CC lists from our home grown website. The homegrown website updates our homegrown database only. It doesn't update the Contact record at our Constant Contact site. 

 

I am attempting to write a C# program that finds users that have made such changes and update the lists to which these users will belong at the Constant Contact site.

 

Since this can involve both adding and removing users from/to lists, the easiest thing for me to do was to retrieve the user via the GetContact wrapper code and then remove them from all lists via the DeleteContactFromLists wrapper code which returns a new contact object. I had thought I could then simply use the Lists.Add method to add them to whatever lists they now want to be included in.

 

The problem is that whenever you get an existing Contact object either with the GetContact method or the DeleteContactFromLists method, the returned object's Lists attribute comes back read-only preventing me from using the List.Add method.

 

I am using a workaround for this problem which I don't like at all. The workaround involves creating a net new Contact object and copying all of the attributes form the one with the read only list (including the Id) to the new list. I can then use the Lists.Add method on this new list. But the disadvantage of this approach -asside from it's messiness- is that if in future you modify or add any new attributes to a Contact object, this program would need to be modified to copy the new attributes.

 

Please let me know if there is a better way to do this

2 REPLIES 2
Regular Advisor

Re: V2 API How to Add Lists to an Existing Contact?

Hello,

 

Thanks for posting!  It looks like the set methods for some attributes have been set to private in the SDK.  Removing the private modifier in the set method of the Contacts object will remove the read-only effect of the attribute.  Its strange to see a private set method with no way in the class to pass a message.  I'm updating the SDK in our repo now to make the set methods public.

 

Regards,

 

Andrew B.

API Support, Constant Contact

Highlighted
Advisor

Re: V2 API How to Add Lists to an Existing Contact?

I did a global search in the sdk for private set and patched to remove the word "private". Rebuilt the sdk and I'm still having problems. Here is a somewhat more detailed description of what's happening to me.

 

Use the following method to retrieve a list of contacts

        public static ResultSet<Contact> GetContactFromList(string listId)         {             ContactList cl = api.GetList(listId);

            Pagination pag;

            //Get first (up to) 50 individuals             ResultSet<Contact> retVal = api.GetContactsFromList(cl);             pag = retVal.Meta.Pagination;

            //Retrieve the rest (if any)             while (pag.Next != null)             {                 ResultSet<Contact> moreFolks = api.GetContactsFromList(pag);                 foreach (Contact c in moreFolks.Results)                     retVal.Results.Add(c);                 pag = moreFolks.Meta.Pagination;             }                         return retVal;         }

Caller invokes this method like this...

            ContactList previouslyRemovedList = CCAPIAAccess.GetList(ConfigurationManager.AppSettings["RemovedSaveList"]);

Then...

            try             {              foreach (Contact c in contactsToBeRemoved.Results)              {    ...                  //Retrieving contact because I notice that the GetContactsFromList only retrieves skeleton Contact objects                  Contact c1 = CCAPIAAccess.api.GetContact(c.Id);    //Not shown here is code where I used GetList to retrieve previouslyRemovedList                  c1.Lists.Add(previouslyRemovedList);

          ...              }             }      catch (Exception e)      {   //e.Message states that "Collection was of a fixed size."             }