Sitecore Forms with Mailchimp integration
Our marketer interested
to create a subscribe form and the information provided by contact should be
stored to the subscription list, As the client wants to use 3rd party Mailchimp
application to store the subscriber details, so in order to achieve this in Sitecore
Forms out of the box there is no Submit Action available to add the contact to
MailChimp,
So I decided to go with
custom Submit Action to implement the required functionality.
Steps to achieve :
- Create the required form in with Sitecore Forms
- Create custom Submit Action for adding a contact to the
Mailchimp Subscriber list.
using System.Linq;
using System.Net;
using Newtonsoft.Json;
using Sitecore.Diagnostics;
using Sitecore.ExperienceForms.Models;
using Sitecore.ExperienceForms.Mvc.Models.Fields;
using Sitecore.ExperienceForms.Processing;
using Sitecore.ExperienceForms.Processing.Actions;
/// <summary>
/// Custom Submit action for adding contact
details to MailChimp
/// </summary>
public class SubscribeMailchimp :
SubmitActionBase<string>
{
public SubscribeMailchimp(ISubmitActionData
submitActionData) :
base(submitActionData)
{ }
protected override bool Execute(string data,
FormSubmitContext formSubmitContext)
{
string name = string.Empty;
string phonoNo = string.Empty;
string email = string.Empty;
// Getting all input values
from the Form
foreach (IViewModel
field in formSubmitContext.Fields)
{
if (((InputViewModel<string>)field).Title == "Name")
{
name
= ((InputViewModel<string>)field).Value;
}
if (((InputViewModel<string>)field).Title == "Phone No")
{
phonoNo
= ((InputViewModel<string>)field).Value;
}
if (((InputViewModel<string>)field).Title == "Email")
{
email
= ((InputViewModel<string>)field).Value;
}
}
string requestData =
JsonConvert.SerializeObject(
new
{
name
= name,
phoneno
= phonoNo,
email_address
= email,
status_if_new
= CommonConstants.Subscribed
});
string hashedEmailAddress =
CommonUtility.CalculateMD5Hash(email.ToLower());
string uri = string.Format("https://us19.api.mailchimp.com/3.0/lists/xxxxxxxxx/members/{0}", hashedEmailAddress);
WebHeaderCollection
headerCollection = new WebHeaderCollection
{
{ "Accept", "application/json" },
{ "Authorization", "apikey " + "xxxxxxxxxxxxxxxxxxxxxxxxxxx-us19" }
};
// Send request
to add details
CommonUtility.SendRequest(headerCollection, "PUT", uri, requestData);
formSubmitContext.RedirectOnSuccess
= true;
formSubmitContext.Abort();
return true;
}
protected override bool TryParse(string value, out string target)
{
target
= string.Empty;
return true;
}
}
public static class CommonUtility
{
/// <summary>
/// Send request to add
the contact details to Mailchimp
/// </summary>
public static string SendRequest(WebHeaderCollection
headerCollection, string httpMethod, string Uri, string data)
{
try
{
using (WebClient webClient
= new WebClient())
{
webClient.Headers
= headerCollection;
return webClient.UploadString(Uri,
httpMethod, data);
}
}
catch (WebException we)
{
using (StreamReader sr
= new StreamReader(we.Response.GetResponseStream()))
{
return sr.ReadToEnd();
}
}
}
private static string CalculateMD5Hash(string input)
{
// Step 1, calculate MD5
hash from input.
System.Security.Cryptography.MD5
md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes =
System.Text.Encoding.ASCII.GetBytes(input);
byte[] hash =
md5.ComputeHash(inputBytes);
// Step 2, convert byte
array to hex string.
StringBuilder
sb = new StringBuilder();
foreach (byte @byte in hash)
{
sb.Append(@byte.ToString("X2"));
}
return sb.ToString();
}
}
· Create custom Submit Action to execute the class
that we created to store details to Mailchimp.
·
Attach this Submit Action to Sitecore Forms
Button that we created.
·
Once we submit the form we can able to see the details added to MailChimp.
Happy Programming 😊
Comments
Post a Comment