Some of the most common interactions with the Newsletter Studio-API is gathered in an interface called INewsletterStudioService
.
With this you can for example:
Just use the built-in IOC-container to get an instance of the interface by injecting it as a dependency in the constructor of your controller.
The package ships with a simple macro to add a recipient to a list, you can find this in:
\App_Plugins\NewsletterStudio\Views\MacroPartials\NewsletterStudioSignup.cshtml
This showcases a simple way to add a recipient from the front end of your site, if you need to do this in your own controller, here is an example:
public class SubscribeSurfaceController : SurfaceController
{
private readonly INewsletterStudioService _newsletterStudioService;
public SubscribeSurfaceController(INewsletterStudioService newsletterStudioService)
{
_newsletterStudioService = newsletterStudioService;
}
public ActionResult Subscribe(SubscribeModel model)
{
var defaultMailingListKey = Guid.Parse("ff104e5c-a0c3-4a5b-a0eb-959685036b18"); // Replace with your mailing list key
var request = AddRecipientRequest.Create("john.doe@foobar.com")
.ForWorkspace(Guid.Parse("606D0954-DDAE-4ADF-BCB6-DD113ADD915E")) // Optional, only used with multiple workspaces
.WithFirstname("John")
.WithLastname("Doe")
.WithSource("Website")
.WithCustomField("city", "London")
.SubscribeTo(defaultMailingListKey)
.Build();
var result = _newsletterStudioService.AddRecipient(addRecipientRequest);
if (result.Success)
{
return Content("Subscribed");
}
else
{
return Content($"Error: " + result.Message);
}
}
}
The AddRecipientRequest-class contains a fluent API to set data for the operation, here are some of the methods:
The service is also when you want to send transactional emails, please have a look at the transactional email guide for a complete example.
It's possible to update any subscription status from the service as well.
var recipientKey = Guid.Parse("1495412D-538C-480E-BAF2-C747D33A3E8B");
var mailingList1Key = Guid.Parse("5EC42DB0-E46D-4966-A5B4-3B4C3C578B98");
var mailingList2Key = Guid.Parse("A7D0BDAF-7BC9-4035-B65E-DDF81816103E");
var request = AddOrUpdateSubscriptionsRequest.Create()
.Set(recipientKey,mailingList1Key,SubscriptionStatus.Subscribed)
.Set(recipientKey,mailingList2Key,SubscriptionStatus.Subscribed)
.Build();
var result = _newsletterStudioService.AddOrUpdateSubscriptions(request);
It's possible to create a custom page where you can handle the unsubscription-process. This page can for example use the API in INewsletterStudioService
to list the current subscriptions for the recipient.
First, configure the "Unsubscribe confirmation url" in the Settings
under the Workspace.
When this is configured we'll route recipients that want to unsubscribe to this page and append a token to the URL.
https://www.mypage.com/custom-unsubscribe?token=acb123.....
When rendering this page you can use the ParseUnsubscribeToken(string token)
-method of INewsletterStudioService
to get information about the recipient eg. the RecipientKey. Further down the process you can pass the RecipientKey to the "GetSubscriptionsFor()", "RemoveRecpient()" or "Unsubscribe()"-methods.
There is plenty of other useful methods on the INewsletterStudioService.