The reason for this blog is that I needed to implement a subscription for MailChimp onto a SharePoint 2010 website.
You can’t use the signup embed code from MailChimp because in .NET you can’t use form tags.
Unfortunately there are no add-ons available for SharePoint, so I decided to look at the API that is available from MailChimp.
The best way to do so is using a wrapper for the language of your choice, in the API documentation there are several options mentioned for .NET
I used the PerceptiveMCAPI wrapper because it was the most descriptive.
The code itself is not very special and is mostly coming from the manual.
Then why are you writing this blog you might think. Well first all the information I used was found all over the internet so it saves you the search, second when integrating the whole thing into SharePoint I ran into a few problems.
A great help was the blog from Matt J Roden for the wrapper and the one from Ian Picknell that helped me with the signing process of the wrapper, because in when deploying to SharePoint you need strong named assembly’s and the PerceptiveMCAPI is not by default.
There is one thing to mention when using the guidance of Ian, SharePoint 2010 uses .NET 3.5 and on my machine I had also 4 installed. Which means that if you use the tool ilasm directly from the startmenu, you’re going to run into the following error when deploying:
Error occurred in deployment step ‘Add Solution’: Could not load file or assembly ‘C:/Windows/Temp/solution-c4d0571a-f4d5-44d1-b6a9-58dee513aa1a/PerceptiveMCAPI.dll’ or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.
To fix this you need to use the .NET 2 version of the ilasm tool which in my case was found here: C:WindowsMicrosoft.NETFramework64v2.0.50727
To make it very simple for you and to save you some work, I attached the dll’s. I also attached the .NET 4 version for those who want to use it in SharePoint 2013.
Download file
Next a description of the steps to setup the project in Visual Studio 2010.
The experienced developers can stop reading here, but for those who are not … read on.
Create an empty SharePoint project and choose to deploy it as a farm solution.
Then add a User Control to your page. (I choose a User Control because I could not use a webpart in the spot where I wanted to use it).
If all goes well you’ll end up with a structure like below.

Next add a reference to the PerceptiveMCAPI.

Contents of the files:
Ascx:
<code>
<asp:TextBox ID=”Email” runat=”server” onclick=”this.value=”;”>Type hier je e-mail adres</asp:TextBox>
<asp:Button ID=”Subscribe” runat=”server” Text=”Verzenden” type=”button”
onclick=”Subscribe_Click” CssClass=”nb-button” />
<div id=”social” runat=”server”></div>
</code>
Ascx.cs:
<code>
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using PerceptiveMCAPI;
using PerceptiveMCAPI.Types;
using PerceptiveMCAPI.Methods;
namespace MailChimp.ControlTemplates.MailChimp
{
public partial class SubscribeMaillist : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Subscribe_Click(object sender, EventArgs e)
{
listSubscribe cmd = new listSubscribe();
listSubscribeParms newlistSubscribeParms = new listSubscribeParms
{
apikey = “Enter your MailChimp API key here”,
id = “Here comes your list id”,
email_address = Email.Text,
double_optin = false,
email_type = EnumValues.emailType.html,
replace_interests = true,
send_welcome = true,
update_existing = true
};
listSubscribeInput newlistSubscribeInput = new listSubscribeInput(newlistSubscribeParms);
var subscribeSuccess = cmd.Execute(newlistSubscribeInput);
if (subscribeSuccess.api_ErrorMessages.Count > 0)
{
social.InnerHtml = “<span>” + subscribeSuccess.api_Request + subscribeSuccess.api_Response +
subscribeSuccess.api_ErrorMessages + subscribeSuccess.api_ValidatorMessages + “</span>”;
}
else
{
social.InnerHtml = “<span>Inschrijving voltooid<br>u ontvangt een bevestiging per e-mail.</span>”;
Email.Text = “Type hier je e-mail adres”;
}
}
}
}
</code>
To make sure that the dll’s are also deployed you need to open the package in Visual Studio, go to the advanced tab and add the dll’s.
-
-
image003
-
-
image004
All there is left is to modify the web.config
You can find this also in the manual of the PerceptiveMCAPI.
In the configSections
<section name=”MailChimpAPIconfig” type=”PerceptiveMCAPI.MCAPISettings, PerceptiveMCAPI” />
And somewhere else (I’d put mine behind </system.webserver>)
<MailChimpAPIconfig>
<MCAPI SecureAccess=”False” Validate=”False” DataCenter=”auto” apikey=”your API key” />
</MailChimpAPIconfig>
And you need to modify the trust of your webapplication to Full trust (Yeah I know, but in my case this was already a fact so I did not look into CAS policy’s)
<trust level=”Full” originUrl=”” />
If you can’t modify the trust level then here’s a good explanation about them by Tyler Holmes
And then after deployment all you need to do is edit your pagelayout or masterpage to use the User Control.
On top of the page:
<%@ Register TagPrefix=”portiva” src=”~/_controltemplates/MailChimp/SubscribeMaillist.ascx” TagName=”MailChimp” %>
And where you want the control to appear:
<portiva:MailChimp id=”nieuwsbrief” runat=”server”/>
And then there it is:
