Search code examples
c#asp.netconstantcontact

Constant Contact - Update Contact


I am trying to update a contact which already exists in my list by using Utility.UpdateContactFullForm(authenticationData, contact);

However, I get an error message stating: The remote server returned an error: (500) Internal Server Error.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.WebException: The remote server returned an error: (500) Internal Server Error.

Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

I have referenced the ConstantContactBO.dll and the ConstantContactUtility;

I can't seem to figure out the problem, any help is much appreciated. Thanks.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ConstantContactBO;
using ConstantContactUtility;


public partial class hmm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string nextChunkId;
        Contact contact = new Contact();
        //Authentication Data
        AuthenticationData authenticationData = new AuthenticationData();
        authenticationData.Username = "";
        authenticationData.Password = "";
        authenticationData.ApiKey = "";

        // get user Contact List collection
        IList<ContactList> lists = Utility.GetUserContactListCollection(authenticationData, out nextChunkId);

        //Search for Contact By Email
        string x = "a@a.com";
        string[] emailAddress = new string[] { x.Trim() };
        IList<Contact> myList = Utility.SearchContactByEmail(authenticationData, emailAddress, out nextChunkId);

        for (int i = 0; i < myList.Count; i++)
        {
            contact.Id = myList[0].Id;
            contact.FirstName = Server.HtmlEncode("abc");
            contact.LastName = Server.HtmlEncode("def");
            contact.EmailAddress = Server.HtmlEncode("a@a.com");

            ContactOptInList theList = new ContactOptInList();
            theList.OptInSource = ContactOptSource.ActionByContact;
            theList.ContactList = new ContactList("39");
            contact.ContactLists.Add(theList);
              Utility.UpdateContactFullForm(authenticationData, contact);
        }


    }
}

Solution

  • I had missed out on including the following:

     Contact thisContact = Utility.GetContactDetailsById(authenticationData, myContact[0].Id);
               // //Add Lists
                ContactOptInList newList = new ContactOptInList();
                //thisContact.OptInSource = ContactOptSource.ActionByCustomer;
                newList.ContactList = new ContactList("39"); //Contact list you want to add them to
                newList.ContactList = new ContactList("10"); //Contact list you want to add them to
                thisContact.ContactLists.Add(newList);
    
                //Update contact
                Utility.UpdateContactFullForm(authenticationData, thisContact);