Search code examples
c#mailchimp-api-v3.0

Adding Tags to MailChimp Member object


I'm using MailChimp.Net.V3 (version 4 of this package) by Brandon Seydel. I'm trying to add a Tag a Member using the following code

using MailChimp.Net;

private async Task MaintainTagsAsync()
{
    string apikey = "<apikey>";
    string listid = "<listid>";
    string emailaddress = "[email protected]";

    try {
      MailChimpManager Mcm = new MailChimpManager(apikey);
      Member m = await Mcm.Members.GetAsync(listid, emailaddress);
      m.Tags.Add(new MemberTag
      {
        Name = "MyTag"
      });
      m.TagsCount = +1;
      await Mcm.Members.AddOrUpdateAsync(listid, m);
    }
    catch (Exception e) {}
}

The above code appears to execute OK but when I inspect the Member, I find that the Tag has not been added. I've tried it with and without incrementing the TagsCount property with the same result.

Does anyone have any experience of using this functionality and can throw any light on where I'm going wrong.


Solution

  • I have now found a method which appears to work. Please see the following code:

    using MailChimp.Net;
    
    private async Task MaintainTagsAsync()
    {
        string apikey = "<apikey>";
        string listid = "<listid>";
        string emailaddress = "[email protected]";
    
        try {
          MailChimpManager Mcm = new MailChimpManager(apikey);
          Member m = await Mcm.Members.GetAsync(listid, emailaddress);
          Tags tags = new Tags();
          tags.MemberTags.Add(new Tag() {
                Name = "MyTag",
                Status = "active"
          });
          await Mcm.Members.AddTagsAsync(listId, emailaddress, tags);
        }
        catch (Exception e) {}
    }