Sample code for 30+ languages & platforms
SQL Server Requires Chilkat v11.0.0+

Constant Contact V3: Create a contact

See more Constant Contact Examples

Create an account-entered contact and read its new ID. Use the email permission that accurately describes the contact’s relationship with the account. The optional postal address belongs in the street_addresses array. On a duplicate, look up the existing contact before deciding whether an update is appropriate. Authorize the app first and use the same credential store.

Constant Contact developer guide

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @iTmp1 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Assumes Chilkat is unlocked. See Global Unlock Sample.
    DECLARE @vault int
    EXEC @hr = sp_OACreate 'Chilkat.Secrets', @vault OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @vault, 'Location', 'local_manager'
    EXEC sp_OASetProperty @vault, 'AutoSplit', 1
    DECLARE @entry int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @entry OUT

    EXEC sp_OAMethod @entry, 'UpdateString', @success OUT, 'appName', 'ConstantContactV3'
    EXEC sp_OAMethod @entry, 'UpdateString', @success OUT, 'service', 'OAuth2'
    EXEC sp_OAMethod @entry, 'UpdateString', @success OUT, 'username', 'my-account'
    DECLARE @tokens int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @tokens OUT

    DECLARE @loaded int
    EXEC sp_OAMethod @vault, 'GetSecretJson', @loaded OUT, @entry, @tokens
    IF @loaded <> 1
      BEGIN

        PRINT 'Run the desktop authorization example using this account and store.'
        EXEC @hr = sp_OADestroy @vault
        EXEC @hr = sp_OADestroy @entry
        EXEC @hr = sp_OADestroy @tokens
        RETURN
      END
    EXEC sp_OAMethod @tokens, 'JsonTypeOf', @iTmp0 OUT, 'access_token'
    IF @iTmp0 <> 1
      BEGIN

        PRINT 'The saved authorization has no access token.'
        EXEC @hr = sp_OADestroy @vault
        EXEC @hr = sp_OADestroy @entry
        EXEC @hr = sp_OADestroy @tokens
        RETURN
      END
    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT

    EXEC sp_OASetProperty @http, 'RequireSslCertVerify', 1
    EXEC sp_OAMethod @tokens, 'StringOf', @sTmp0 OUT, 'access_token'
    EXEC sp_OASetProperty @http, 'AuthToken', @sTmp0
    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    DECLARE @body int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @body OUT

    EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'create_source', 'Account'
    EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'first_name', 'Alex'
    EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'last_name', 'Rivera'
    EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'email_address.address', 'reader+newsletter@example.com'
    --  This sample assumes the account has recorded a qualifying existing relationship.
    EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'email_address.permission_to_send', 'implicit'
    EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'street_addresses[0].kind', 'work'
    EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'street_addresses[0].street', '100 Main Street'
    EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'street_addresses[0].city', 'Boston'
    EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'street_addresses[0].state', 'MA'
    EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'street_addresses[0].country', 'United States'
    EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', 'https://api.cc.email/v3/contacts', @body, 'application/json', @resp
    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    IF @iTmp0 = 409
      BEGIN

        PRINT 'Contact already exists. Look up the email address to obtain its ID.'
        EXEC @hr = sp_OADestroy @vault
        EXEC @hr = sp_OADestroy @entry
        EXEC @hr = sp_OADestroy @tokens
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @body
        RETURN
      END
    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    IF (@success <> 1) or (@iTmp0 <> 201)
      BEGIN

        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
        PRINT 'Expected HTTP 201. Received ' + @iTmp0
        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
        IF @iTmp0 = 401
          BEGIN

            PRINT 'Refresh the saved access token or authorize again.'
          END
        EXEC @hr = sp_OADestroy @vault
        EXEC @hr = sp_OADestroy @entry
        EXEC @hr = sp_OADestroy @tokens
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @body
        RETURN
      END
    DECLARE @created int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @created OUT

    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    EXEC sp_OAMethod @created, 'Load', @success OUT, @sTmp0
    IF @success <> 1
      BEGIN

        PRINT 'Expected a JSON response.'
        EXEC @hr = sp_OADestroy @vault
        EXEC @hr = sp_OADestroy @entry
        EXEC @hr = sp_OADestroy @tokens
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @created
        RETURN
      END
    EXEC sp_OAMethod @created, 'JsonTypeOf', @iTmp0 OUT, 'contact_id'
    EXEC sp_OAMethod @created, 'StringOfEquals', @iTmp1 OUT, 'contact_id', '', 1
    IF (@iTmp0 <> 1) or (@iTmp1 = 1)
      BEGIN

        PRINT 'Missing contact_id in the response.'
        EXEC @hr = sp_OADestroy @vault
        EXEC @hr = sp_OADestroy @entry
        EXEC @hr = sp_OADestroy @tokens
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @created
        RETURN
      END

    EXEC sp_OAMethod @created, 'StringOf', @sTmp0 OUT, 'contact_id'
    PRINT 'Created contact ID: ' + @sTmp0

    EXEC @hr = sp_OADestroy @vault
    EXEC @hr = sp_OADestroy @entry
    EXEC @hr = sp_OADestroy @tokens
    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @resp
    EXEC @hr = sp_OADestroy @body
    EXEC @hr = sp_OADestroy @created


END
GO