cancel
Showing results for 
Search instead for 
Did you mean: 

Retrieve a list of all campaigns

All Star

Retrieve a list of all campaigns

How can I retrieve a list of all campaigns I have created/sent to date?  I am trying this 

https://api.constantcontact.com/v2/emailmarketing/campaigns?status=ALL&limit=50 

but it limits me to just 50. I have nearly 200 in my account today and I want to cycle through them and pull the statistics of each for my personal database I am creating. 

4 REPLIES 4
Moderator

Re: Retrieve a list of all campaigns

Hello,

 

Thank you for reaching out to Constant Contact API Support.

 

The Constant Contact API uses paginated output for handling responses with many results. When doing a GET call for all campaigns, you can set a limit of how many results you get per page. 50 is both the default limit, and also the upper limit on how many campiagns can be called at once. If there are more results than the limit you set then the response you get will include a next_link for you to append to your query for another page of results; in your case this would return the next 50 results and the link to a third page, and so on.

 

Please see this documentation for details on how to use handle paginated output: https://developer.constantcontact.com/docs/developer-guides/paginated-output.html

 

Let me know if you have any other questions and I'll be happy to help in any way I can!

 

Sincerely,
David B.
API Support Specialist

All Star

Re: Retrieve a list of all campaigns

Do you have a sample of how this is done in C#?

 

This is my initial call

 

    var AllCampaigns = EmailCampaignService.GetCampaigns(CampaignStatus.SENT, null, null, null);

 

 I am not sure how I would set the pagination object to pass in the last parameter or to test to set it?

Honored Contributor

Re: Retrieve a list of all campaigns

Hello,

 

when you use the services for endpoints that have pagination, the ResultSet object that is returned contains any applicable pagination data. Here's an example of how you can get all of the email campaigns for an account:

 

ConstantContactFactory SetupFactory()
{
    // To create an instance of the ConstantContactFactory class, you will need an API key and Access Token. 
    // You can find information about how to obtain these in our Get Started guide here:
    // http://developer.constantcontact.com/get-started.html

    IUserServiceContext userServiceContext = new UserServiceContext("ACCESS_TOKEN", "API_KEY");

    return new ConstantContactFactory(userServiceContext);

}


IList<EmailCampaign> GetAllCampaigns(CampaignStatus? status, int? limit)
{
    List<EmailCampaign> campaigns = new List<EmailCampaign>();

    ConstantContactFactory _cc = SetupFactory();
    IEmailCampaignService _service = _cc.CreateEmailCampaignService();

    var campaignData = _service.GetCampaigns(status, limit, null, null);
    campaigns.AddRange(campaignData.Results);

    while(campaignData.Meta.Pagination.Next != null)
    {
        campaignData = _service.GetCampaigns(null,null,null,campaignData.Meta.Pagination);
        campaigns.AddRange(campaignData.Results);
    }

    return campaigns;
}

// the first paramter is the campaign status. You can use null for all campaigns, or you can 
// use the pre-defined values of the CampaignStatus enum. I'm using the maximum limit of 500.
var campaignList = GetAllCampaigns(null, 500); 

It is important to note that this specific code is not fault tolerant since it's just a sample. you would want to use a try-catch block to handle the possible cases of invalid (or expired) authentication or an API outage.

 

Please let us know if you have any questions!

 

Sincerely,

Elijah G.
API Support Engineer
Highlighted
All Star

Re: Retrieve a list of all campaigns

All set