21 Nov’17

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

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

Written by

Krishna Bhanushali

Software Developer

Leave a Reply

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

Time limit is exhausted. Please reload the CAPTCHA.

Want to streamline your business processes?

  • This field is for validation purposes and should be left unchanged.

Recent Articles

  • Reading more then 10K records in D3FOE OData API

    11 July’ 2018

    Introduction: We all know Dynamics 365 Finance and Operations has limitation of 10K records to be fetched at a time usi...

    Read more
  • Paging in D365 Customer Engagement v9.0

    10 July’ 2018

    Introduction: The Xrm.retrieveMultipleRecords method is used to retrieve a collection of records in Dynamics 365 Custom...

    Read more
  • Set up Dynamics 365 connection in Microsoft Social Engagement

    10 July’ 2018

    Introduction: This blog explains how to Set up Dynamics 365 connection in Microsoft Social Engagement. Steps to be follo...

    Read more
  • Voice of the Customer failed to install

    10 July’ 2018

    Introduction: Many people face issues in installing Voice of Customer solution on v9 environment and trying repeatedly ...

    Read more
  • Scribe Insight AX as a Web Service Find Block issue

    10 July’ 2018

    Introduction: If we need to look up for any value from AX then we do it by using a Find Block in Scribe Insight Eg: Basi...

    Read more