› Dynamics 365, Enterprise › Dynamics CRM Marketing list members sync with MailChimp- Part 2

Dynamics CRM Marketing list members sync with MailChimp- Part 2

 Krishna Bhanushali   Leave a comment

Introduction:

This blog is the continuation of Part 1 and it will show you the code that are required for the action to work and the workflows created in CRM.

Steps:

1. Add a button on marketing list using Ribbon Workbench and script which calls the action.

2. Create an action “MailChimpBatchCreateCall” that triggers on Sync button click.

When the button is clicked, action is called using JavaScript.

This action Retrieves configuration and Marketing list records from CRM and add the members to Mail Chimp Marketing list by Mail Chimp API Call (Batch request).

The code to be included in the action is given below: This is a POST request as we are creating list members in MainChimp

using (WebClientEx client = new WebClientEx())

{

    string authorizationKey = string.Empty;

    authorizationKey =             Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format(CultureInfo.InvariantCulture, "{0}:{1}", username, api)));

    client.Timeout = 60000;

    client.Headers.Add(HttpRequestHeader.ContentType, "application/json");

    client.Headers.Add(HttpRequestHeader.Authorization, "Basic " + authorizationKey);

    tracer.Trace("jsonData: " + jsonData);

    createBatchResponseJSON = client.UploadString(basicURL, jsonData);

}

tracer.Trace("createBatchResponse :" + createBatchResponseJSON);

Model.MailChimpContactCreateBatchResponse createBatchResponse = GetInfoFromJSON(createBatchResponseJSON);

CreateMailChimpSyncRecord(createBatchResponse, tracer, service, marketinglistid);

The JSON request to be passed is given below:

The JSON response for the BATCH call is given below:

3. Create an action “MailChimp status” which triggers on MailChimp Sync record creation.

Mail Chimp API call is made to get the status of the synchronization process. Batch ID is the unique value which helps to retrieve the status of sync call. If status is finished, sync process is completed.

The code to be included in the action is given below: This is a GET request to check the status and update the status record in CRM.

//// Call the web service

using (WebClientEx client = new WebClientEx())

{

    string authorizationKey = string.Empty;

    authorizationKey = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format(CultureInfo.InvariantCulture, "{0}:{1}", username, api)));

    basicURL = basicURL + "/" + batchId;

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(basicURL);

    request.Accept = "application/json";

    request.Method = "GET";

    request.Headers.Add("Authorization", "Basic " + authorizationKey);

    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())

    using (Stream stream = response.GetResponseStream())

    using (StreamReader reader = new StreamReader(stream))

    {

        getBatchResponseJSON = reader.ReadToEnd();

    }

}

tracer.Trace("createBatchResponse :" + getBatchResponseJSON);

Model.MailChimpContactCreateBatchResponse createBatchResponse = GetInfoFromJSON(getBatchResponseJSON);

//// Update the MailChimp Sync record with new status

UpdateMailChimpSyncRecord(createBatchResponse, tracer, service, currentRecord.Id);

The JSON response for the GET request is given below:

4. Create workflows that keep on checking the status of the batch call. The initial batch call is a sync process and we get a status showing no. of record finished and pending. Thus, the syncing process takes place in background. Therefore, we need to create workflows to keep on checking in specific interval.

In the initial call, we have kept waiting time of 5 min and then kept waiting time of 1 hour and called the same workflow again till we get the status of batch call as “finished”.

a. Main Workflow:

b. Child Workflow 1 Check Mail Chimp Sync.

c. Child Workflow 2 Check Mail Chimp Sync.

For more code details, you can refer the GitHub link.

Hope it help you and thus we can integrate the CRM with Mailchimp and make use of MailChimp API calls listed in their documentation.

Members can be added individually or by using the batch operations. You can refer the below links from MaiChimp  which shows how we can make individual and batch calls.
i. Creating a new member
ii. Batch Operations


Liked it? Share it.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.