In a previous post, we discussed how to List Emails and Display Content using the REST API with PHP.

In today's post, we're going to do the same in c# and we'll add a few things.

 

We're going to show you how to manage the following email operations :

  • Retrieve an Email
  • List Emails
  • Create Email
  • Update Email
  • Delete Email

 

The code assumes that you've initialized the REST client. Please see the following post on making Requests to the REST API using c#.

The complete source code for the examples shared here is available here on Github.

The models are available here.

 

Setting up the client :

- See Requests to the REST API using c#.

 

 

Retrieve an Email

Let's start by retrieving emails from your Eloqua install.

Given the ID of an Email, the following method will invoke an HTTP GET request to the API to retrieve the email.

 

     public Email GetEmail(int id)
        {
            RestRequest request = new RestRequest(Method.GET)
                                      {
                                          Resource = "/assets/email/" + id,
                                          RootElement = "Email",
                                          RequestFormat = DataFormat.Json
                                      };

            IRestResponse<Email> response = _client.Execute<Email>(request);
            return response.Data;
        }

 

 

Searching for Emails

The following method can be used to search for Emails

 

public List<Email> GetEmail(string searchTerm ,int page ,int pageSize)
        {
            RestRequest request = new RestRequest(Method.GET)
                                      {
                                          Resource =
                                              string.Format("/assets/emails?search={0}&page={1}&count={2}", searchTerm,
                                                            page, pageSize),
                                          RequestFormat = DataFormat.Json
                                      };

            IRestResponse<RequestObjectList<Email>> response = _client.Execute<RequestObjectList<Email>>(request);

            Console.WriteLine("Total :" + response.Data.total);

            return response.Data.elements;
        } 

 

 

Create Email

Now let's create a new Email :

The following function invokes an HTTP POST request to create the Email

 

     public EmailCreateEmail(Email email)
        {

            RestRequest request = new RestRequest(Method.POST)
                                      {
                                          Resource = "/assets/email",
                                          RequestFormat = DataFormat.Json
                                      };
            request.AddBody(email);

            IRestResponse<Email> response = _client.Execute<Email>(request);

            return response.Data;
}

// 

 

Let's create the definition of a new Email and Post it to the server :

 

Email email = new Email()
            {
                name = "sample email",
                emailFooterId = 1,
                emailHeaderId = 1,
                encodingId = 1,
                emailGroupId = 1,
                subject = "sample",
                htmlContent = new RawHtmlContent()
                                  {
                                      type = "RawHtmlContent",
                                      html = "<html><head></head><body>test</body></html>"
                                  }
            };

 

Update and Delete operations are available in the complete source code on Github.

Please note that the REST API is not yet public and the code samples available here are my own work. I'll be glad to help support and fix issues in the code, but please understand that Eloqua will not be able to support this - as it is my own.

 

Thanks,
Fred