copy a campaign via API

SOLVED
Highlighted
Advisor

copy a campaign via API

Hi there,

 

We have a campaign that has to be run weekly. Since there is no way in constant and contact to schedule for a recurring campaign, the only way I believe you can do this is to copy a campaign and schedule that to run on a desired date time. Is there a way to make a copy of a campaign through API using the campaign Name? I didn't find any documenation for it or may be i missed one. Please advise..

 

Thanks, Sid

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Moderator

Re: copy a campaign via API

This is actually a technical problem in the content you're sending over, which is a good thing since it means we're getting close to a solution.  The content of the nodes EmailContent and EmailTextContent must be XML or HTML encoded when sent to Constant Contact via the API.  If you do not encode it, it will certainly cause errors as the XML validation will fail.

 

I also ran into one additional problem which turned out to be a defect on our side, not something you are doing wrong.  I've opened up a defect with our development team to have it fixed in a future release.  In the meantime, you will need to remove the <LastRunDate> node before sending the POST request.  Below is the XML which worked in my account.

 

 

<?xml version="1.0" encoding="UTF-8" ?> 
  <entry xmlns="http://www.w3.org/2005/Atom">
  <link href="/ws/customers/abc/campaigns/1105077954923" rel="edit" />
  <id>http://api.constantcontact.com/ws/customers/abc/campaigns/1105077954923</id>
  <title type="text">Test Custom Template Email - 4</title>
  <updated>2011-04-07T14:45:20.019Z</updated>
  <author>
  <name>Constant Contact</name>
  </author>
  <content type="application/vnd.ctct+xml">
  <Campaign xmlns="http://ws.constantcontact.com/ns/1.0/" id="http://api.constantcontact.com/ws/customers/abc/campaigns/1105077954923">
  <Name>Test Custom Template Email - 4</Name>
  <Status>Sent</Status>
  <Date>2011-04-07T14:45:20.019Z</Date>
  <LastEditDate>2011-04-07T14:50:05.454Z</LastEditDate>
  <Sent>7</Sent>
  <Opens>0</Opens>
  <Clicks>0</Clicks>
  <Bounces>0</Bounces>
  <Forwards>0</Forwards>
  <OptOuts>0</OptOuts>
  <SpamReports>0</SpamReports>
  <SharePageURL />
  <Subject>PD Survey Test</Subject>
  <FromName>ABC</FromName>
  <CampaignType>CUSTOM</CampaignType>
  <ViewAsWebpage>YES</ViewAsWebpage>
  <ViewAsWebpageLinkText>Click here</ViewAsWebpageLinkText>
  <ViewAsWebpageText>Having trouble viewing this email?</ViewAsWebpageText>
  <PermissionReminder>NO</PermissionReminder>
  <GreetingSalutation>Dear</GreetingSalutation>
  <GreetingName>FirstName</GreetingName>
  <GreetingString>Greetings!</GreetingString>
  <OrganizationName>ABC</OrganizationName>
  <OrganizationAddress1>123 Main St</OrganizationAddress1>
  <OrganizationAddress2>Suite 100</OrganizationAddress2>
  <OrganizationAddress3 />
  <OrganizationCity>Chicago</OrganizationCity>
  <OrganizationState>IL</OrganizationState>
  <OrganizationInternationalState />
  <OrganizationCountry>us</OrganizationCountry>
  <OrganizationPostalCode>99999</OrganizationPostalCode>
  <IncludeForwardEmail>YES</IncludeForwardEmail>
  <ForwardEmailLinkText>Forward email</ForwardEmailLinkText>
  <IncludeSubscribeLink>YES</IncludeSubscribeLink>
  <SubscribeLinkText>Subscribe me!</SubscribeLinkText>
  <EmailContentFormat>XHTML</EmailContentFormat>
  <EmailContent>&lt;html lang=&quot;en&quot; xml:lang=&quot;en&quot; xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xmlns:cctd=&quot;http://www.constantcontact.com/cctd&quot;&gt; &lt;body&gt;&lt;CopyRight&gt;Copyright (c) 1996-2011 Constant Contact. All rights reserved. Except as permitted under a separate written agreement with Constant Contact, neither the Constant Contact software, nor any content that appears on any Constant Contact site, including but not limited to, web pages, newsletters, or templates may be reproduced, republished, repurposed, or distributed without the prior written permission of Constant Contact. For inquiries regarding reproduction or distribution of any Constant Contact material, please contact legal@constantcontact.com.&lt;/CopyRight&gt; &lt;OpenTracking/&gt; &lt;!-- Do NOT delete previous line if you want to get statistics on the number of opened emails --&gt; &lt;CustomBlock name=&quot;letter.intro&quot; title=&quot;Personalization&quot;&gt; &lt;Greeting/&gt; &lt;/CustomBlock&gt; &lt;b&gt;Please click on the survey link below.&lt;/b&gt; &lt;Property name=&quot;Subscriber.CustomText1&quot; /&gt;&lt;br /&gt; &lt;Property name=&quot;Subscriber.CustomText2&quot; /&gt; &lt;/body&gt; &lt;/html&gt;</EmailContent>
  <EmailTextContent>&lt;Text&gt;&lt;Greeting/&gt;&lt;/Text&gt;</EmailTextContent>
  <StyleSheet />
  <ArchiveStatus />
  <ArchiveURL />
  <ContactLists>
  <ContactList id="http://api.constantcontact.com/ws/customers/abc/lists/4">
  <link xmlns="http://www.w3.org/2005/Atom" href="/ws/customers/abc/lists/4" rel="self" />
  </ContactList>
  </ContactLists>
  <FromEmail>
  <Email id="http://api.constantcontact.com/ws/customers/abc/settings/emailaddresses/1">
  <link xmlns="http://www.w3.org/2005/Atom" href="/ws/customers/abc/settings/emailaddresses/1" rel="self" />
  </Email>
  <EmailAddress>xyz@abc.com</EmailAddress>
  </FromEmail>
  <ReplyToEmail>
  <Email id="http://api.constantcontact.com/ws/customers/abc/settings/emailaddresses/1">
  <link xmlns="http://www.w3.org/2005/Atom" href="/ws/customers/abc/settings/emailaddresses/1" rel="self" />
  </Email>
  <EmailAddress>xyz@abc.com</EmailAddress>
  </ReplyToEmail>
  </Campaign>
  </content>
  <source>
  <id>http://api.constantcontact.com/ws/customers/abc/campaigns</id>
  <title type="text">Campaigns for customer: abc</title>
  <link href="campaigns" />
  <link href="campaigns" rel="self" />
  <author>
  <name>abc</name>
  </author>
  <updated>2011-04-07T14:50:05.494Z</updated>
  </source>
  </entry>
Dave Berard
Senior Product Manager, Constant Contact

View solution in original post

16 REPLIES 16
Highlighted
CTCT Employee

Re: copy a campaign via API

Hello Sid,

 

Copying a campaign in the API is definitely possible.  The best way to do this would be to do a GET call on the campaign that you would like copied.  You will receive that campaigns XML.  You will want to modify the Campaign Name, then post the XML to the campaign collection URI.

 

You can find more information about performing a GET on the original campaign here, as well as on how to then post the new XML to the campaign collection here.

 

If you have any questions about this please let me know.

Benjamin Soder
NOC Analyst
Constant Contact
Highlighted
Advisor

Re: copy a campaign via API

 I tried the code below to copy a new campaign but i get an error when i execute my code. "Web Exception: Unsupported media types".

 

 

public void CreateCampaign(string APIKey, string uName, string pWord)
    {
        CredentialCache LoginCredentials = new CredentialCache();
        LoginCredentials.Add(new Uri("https://api.constantcontact.com/ws/customers/" + uName), "Basic", new NetworkCredential(APIKey + '%' + uName, pWord));
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.constantcontact.com/ws/customers/" +
            uName + "/campaigns");
        request.Credentials = LoginCredentials;
        string createCampaign = @"<?xml version=""1.0"" encoding=""UTF-8"" ?> 
        <entry xmlns=""http://www.w3.org/2005/Atom"">
        <link href=""/ws/customers/lennoxpd/campaigns/1105057009223"" rel=""edit"" /> 
        <title type=""text"">" + DateTime.Now.ToShortDateString() + " Email Campaign" + @"</title> 
        <updated>2011-04-04T20:05:07.856Z</updated> 
        <author>
        <name>Constant Contact</name> 
        </author>
        <content type=""application/vnd.ctct+xml"">
        <Name>" + DateTime.Now.ToShortDateString() + " Email Campaign" + @"</Name> 
        <Status>Sent</Status> 
        <Date>2011-04-07T20:05:07.856Z</Date> 
        <LastEditDate>2011-04-06T14:44:56.956Z</LastEditDate> 
        <LastRunDate>2011-04-04T20:05:06.375Z</LastRunDate> 
        <Sent>7</Sent> 
        <Opens>0</Opens> 
        <Clicks>0</Clicks> 
        <Bounces>4</Bounces> 
        <Forwards>0</Forwards> 
        <OptOuts>0</OptOuts> 
        <SpamReports>0</SpamReports> 
        <Subject>Survey program test</Subject> 
        <FromName>Comp A</FromName> 
        <CampaignType>STOCK</CampaignType> 
        <ViewAsWebpage>YES</ViewAsWebpage> 
        <ViewAsWebpageLinkText>Click here</ViewAsWebpageLinkText> 
        <ViewAsWebpageText>Having trouble viewing this email?</ViewAsWebpageText> 
        <PermissionReminder>YES</PermissionReminder> 
        <PermissionReminderText /> 
        <GreetingSalutation /> 
        <GreetingString /> 
        <OrganizationName /> 
        <OrganizationAddress1>6123 Main St</OrganizationAddress1> 
        <OrganizationAddress2>Suite 985</OrganizationAddress2> 
        <OrganizationAddress3 /> 
        <OrganizationCity>City</OrganizationCity> 
        <OrganizationState>State</OrganizationState> 
        <OrganizationInternationalState /> 
        <OrganizationCountry>us</OrganizationCountry> 
        <OrganizationPostalCode>99999</OrganizationPostalCode> 
        <IncludeForwardEmail>YES</IncludeForwardEmail> 
        <ForwardEmailLinkText>Forward email</ForwardEmailLinkText> 
        <IncludeSubscribeLink>YES</IncludeSubscribeLink> 
        <SubscribeLinkText>Subscribe me!</SubscribeLinkText> 
        <ArchiveStatus /> 
        <ArchiveURL /> 
        <ContactLists>
        <link xmlns=""http://www.w3.org/2005/Atom"" href=""/ws/customers/abc/lists/4"" rel=""self"" /> 
        </ContactList>
        </ContactLists>
        <FromEmail>
        <link xmlns=""http://www.w3.org/2005/Atom"" href=""/ws/customers/abc/settings/emailaddresses/1"" rel=""self"" /> 
        </Email>
        <EmailAddress>abc@compa.com</EmailAddress> 
        </FromEmail>
        <ReplyToEmail>
        <link xmlns=""http://www.w3.org/2005/Atom"" href=""/ws/customers/abc/settings/emailaddresses/1"" rel=""self"" /> 
        </Email>
        <EmailAddress>abc@compa.com</EmailAddress> 
        </ReplyToEmail>
        </Campaign>
        </content>
        <source>
        <title type=""text"">Campaigns for customer: abc</title> 
        <link href=""campaigns"" /> 
        <link href=""campaigns"" rel=""self"" /> 
        <author>
        <name>abc</name> 
        </author>
        <updated>2011-04-06T14:44:56.999Z</updated> 
        </source>
        </entry>";
        request.Method = "POST";
        request.ContentType = "text/xml";
        UTF8Encoding encoding = new UTF8Encoding();
        byte[] byteArray = encoding.GetBytes(createCampaign);
        try
        {
            request.ContentLength = byteArray.Length;
            string XMLResponse = "Bytes to send: " + byteArray.Length;
            Stream streamRequest = request.GetRequestStream();
            streamRequest.Write(byteArray, 0, byteArray.Length);
            streamRequest.Close();
            HttpWebResponse Response = (HttpWebResponse)request.GetResponse();
            StreamReader Reader = new StreamReader(Response.GetResponseStream());
            XMLResponse += Response.StatusCode + Response.StatusDescription + Reader.ReadToEnd();
            Reader.Close();
            Response.Close();
            //return XMLResponse;
        }
        catch (WebException e)
        {
            throw new WebException("WebException: " + e.Status + "With response: " + e.Message);
        }
        catch (Exception e)
        {
            throw new Exception("Exception: " + e.Message);
        }
    }

 

 

Highlighted
Advisor

Re: copy a campaign via API

I changed my content type to "application/atom+xml", and now i get a 400 Bad Request error.

Highlighted
Moderator

Re: copy a campaign via API

400 Bad Request generally indicates that information you sent over is bad or invalid.  To get the full error message, add the following two lines to your Catch block for the WebException:

 

StreamReader s = new StreamReader(e.Response.GetResponseStream());
String FullResponse = s.ReadToEnd(); 

 

The value in FullResponse will give you our error feedback so you can actually see the error in the XML.  If you can follow up with the actual content you're getting in that response, we can provide you with a better direction to go in correcting it.

Dave Berard
Senior Product Manager, Constant Contact
Highlighted
Advisor

Re: copy a campaign via API

This is what i get now.

 

WebException: ProtocolErrorWith response: The remote server returned an error: (400) Bad Request.Full Detail: Error 400: The request contains errors in the common Atom sections, which lie outside <entry> or <Content>, such as <feed>, <id>, <author> or <updated>.

Highlighted
Moderator

Re: copy a campaign via API

I did notice there is a possible problem with your XML content, potentially caused by what you're trying to do.  The Campaign Type is set as STOCK, which indicates you are trying to create a Campaign that uses one of our templates. 

 

This is not currently possible, Campaigns using our templates are only able to be created through our web UI.  While we allow you to get information on them through the API, such as statistics and details, you can not create one via the API.  This is likely the cause of your 400 error as you are both missing required fields (<EmailContent> and <EmailTextContent>) and passing in an invalid value for <CampaignType> (STOCK where only HTML or XHTML are allowed in a POST request). 

 

If your use case is to copy and use a template based Campaign, this is not possible through the API.  Only custom HTML/XHTML templates are usable through this API feature.

Dave Berard
Senior Product Manager, Constant Contact
Highlighted
Advisor

Re: copy a campaign via API

So, i would have to create an email template in constant and contact using custom html template then, correct? Is there an option where i can do that rather than having you guys create it? 

 

I see a button "Want a custom template? Let us create it" but don't see anywhere i could create one of my own. Please let me know.

Highlighted
Moderator

Re: copy a campaign via API

The option of Custom HTML/XHTML code means that you are able to use the HTML/XHTML content for an email that you or your design group created.  For more information on how this works in your standard account, you can use the steps in this FAQ: http://constantcontact.custhelp.com/cgi-bin/constantcontact.cfg/php/enduser/std_adp.php?p_faqid=1357...

 

The same details apply when using this through our API features.  If you have your own custom HTML or XHTML email content, you can use that through the API features you're leveraging today.  If you do not have your own custom HTML or XHTML code, this option may not be for you.  The "Create a custom Template" service you are talking about is not able to be used through the API either, I would not recommend this as a solution for you for this reason.

Dave Berard
Senior Product Manager, Constant Contact
Highlighted
Advisor

Re: copy a campaign via API

Hi Dave,

 

If i do a custom template, would i be able to drag fields to the body? There are few fields that i have to pass from the list to the body of an email. When i was building a custom template, i didn't see an option to add fields to the body.

 

Also, since i will be copying a campaign via API, do I need to create the body each time i do that or is that a one time thing. Where by passing the campaign ID, the contents as well as the list will all be copied. 

 

Not sure if I explained myself very clear here.

Developer Portal

View API documentation, code samples, get your API key.

Visit Page

Constant Contact Would Like To Hear From You!

We want to hear from customers like you about your favorite features and how they have helped your business or organization. Tell us by answering a few questions in...

Read More
Featured