2

I'm trying to get all the members in my list (around 19,000 members) and am using the Mailchimp.NET.V3 package in C#.

The following code only retrieves the first 1000 members

IMailChimpManager MC = new MailChimpManager(@"xxxxxxxxxxxxxxxxxxxxxxxxx-xxx");
var listMembers = await MC.Members.GetAllAsync(ListId);

I also tried using the MemberRequest constructor but this never returns any value.

var listMembers = await MC.Members.GetAllAsync(ListId, new MemberRequest { Limit = 20000 } );

Can anyone help? Thanks!

Brendan Gooden
  • 1,460
  • 2
  • 21
  • 40

5 Answers5

1

Use the offset value.

List<Member> listMembers = new List<Member>();
IMailChimpManager manager = new MailChimpManager(MailChimpApiKey);
bool moreAvailable = true;
int offset = 0;
while (moreAvailable)
{
    var listMembers = manager.Members.GetAllAsync(yourListId, new MemberRequest
    {
        Status = Status.Subscribed,
        Limit = 250,
        Offset = offset
    }).ConfigureAwait(false);

    var Allmembers = listMembers.GetAwaiter().GetResult();
    foreach(Member member in Allmembers)
    {
        listMembers.Add(member);
    }
    if (Allmembers.Count() == 250)
        //if the count is < of 250 then it means that there aren't more results
        offset += 250;
    else
        moreAvailable = false;
}
0

You need to use the 'count' parameter, not limit.

MrPHP
  • 932
  • 2
  • 7
  • 19
0

I have done something similar using C# script on unity 3D, with basic authentication and WWW request. This script returns the members of a list on Mailchimp using API 3.0. Maybe this code helps.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class mailchimpRESTful : MonoBehaviour 
{
    /*
     *  USE using CURL:
     * 
        curl --request GET \
            --url 'https://<dataCenter>.api.mailchimp.com/3.0/lists/<your-list-name>/members/' \
            --user 'anystring:<mailchimp-api-key>'

        EXAMPLE using CURL:

        curl --request GET \
            --url 'https://us12.api.mailchimp.com/3.0/lists/era987af43/members/' \
            --user 'anystring:e419ac3fefefgkjne0559901222a3dbf-us12'
    */

    // Mailchimp API RESTful C# script GET Member list 

    private const string DC       = "<your-data-center>";       // us1,us2..us18 
    private const string LIST     = "<your-list-name>";        // Something similar to: era987af43
    private const string URL      = "<your-url>";              // "https://<dataCenter>.api.mailchimp.com/3.0/lists/<your-list-name>/members/";
    private const string USER     = "anystring";               // You can use any user
    private const string API_KEY  = "your-api-key";            // Mailchimp API-key: e419ac3fefefgkjne0559901222a3dbf-us12

    public void Start()
    {
        Request ();
    }


    public void Request()
    {
        Dictionary<string, string> headers = new Dictionary<string,string>();
        headers ["Authorization"] = "Basic " + System.Convert.ToBase64String (System.Text.Encoding.ASCII.GetBytes (USER + ":" + API_KEY));

        WWW request = new WWW(URL, null, headers);
        StartCoroutine (OnResponse (request));
    }

    private IEnumerator OnResponse(WWW req)
    {
        yield return req;
        Debug.Log ("Query Result:"+req.text);
    }
}
Iker
  • 2,018
  • 2
  • 29
  • 52
0
    private async Task<List<Member>> GetMemberListAsync(string listId)
    {
        var offset = 0;
        var moreAvailable = true;
        var listMembers = new List<Member>();

        while (moreAvailable)
        {
            var result = await manager.Members.GetAllAsync(listId, new MemberRequest
            {
                Status = Status.Subscribed,
                Limit = 250,
                Offset = offset
            });

            var resultList = result.ToList();

            foreach (var member in resultList)
            {
                listMembers.Add(member);
            }

            if (resultList.Count() == 250)
                offset += 250;
            else
                moreAvailable = false;
        }

        return listMembers;
    }
-1
var listMembers = manager.Members.GetAllAsync(ListId).ConfigureAwait(false);

var Allmembers = listMembers.GetAwaiter().GetResult();
Blue
  • 22,608
  • 7
  • 62
  • 92
  • 6
    While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, as this reduces the readability of both the code and the explanations! – Blue Feb 01 '18 at 17:42
  • The code snippet returns only 1000. The author wants to get 19,000. – curious.netter Jun 14 '21 at 13:04