In today's post, we'll show you how to create the following Contact Segment :

 

 

A complete working sample of the code is available here on Github.

The code assumes that you've initialized the REST client. Please see the following post on making Eloqua REST API - C# requests.

The models are available here.

 

We'll start by defining the Filter Critiera : "Sent any Email". This object contains two important properties :

  • activityRestriction : at least once
  • timeRestriction : within the last week

 

            // Activity Restriction (number of sends)
            var activityRestriction = new NumericValueCondition
                                          {
                                              id = -500020,
                                              @operator = "withinLast",
                                              type = "NumericValueCondition",
                                              value = 1
                                          };

            // Time Restriction (sent within the last)
            var timeRestriction = new DateValueCondition
                                      {
                                          id = -21,
                                          @operator = "withinLast",
                                          type = "DateValueCondition",
                                          value = new RelativeDate
                                                      {
                                                          id = "-500021",
                                                          offset = 1,
                                                          timePeriod = "week",
                                                          type = "RelativeDate"
                                                      }
};
    

 

Next, we'll create a container for our Filter Criteria :

 

          // The endpoint expects a list, but we'll only include 1 criteria
            List<ContactSegmentSample.Models.Criteria.Criterion> criteria = new List<ContactSegmentSample.Models.Criteria.Criterion>();
            criteria.Add(emailSentCriterion);
    

 

Contact Segments can contain one or many Contact Filters. In our example, we're only using 1 Filter, but the API expects this as a list.

We're going to create another list, but please note that we're only adding a single Filter.

 

            // Next we'll create a Contact Filter
            ContactFilter filter = new ContactFilter
                                       {
                                           criteria = criteria,
                                           id = -500012,
                                           name = "Filter Sample",
                                           scope = "local",
                                           statement = "-500014",
                                           type = "ContactFilter"
                                       };

            ContactFilterSegmentElement filterElement = new ContactFilterSegmentElement()
                                                            {
                                                                filter = filter,
                                                                id = -500013,
                                                                isIncluded = true,
                                                                type = "ContactFilterSegmentElement"
                                                            };

            // The Segment expects one or more Filters, we'll include 1
            List<SegmentElement> filterElements = new List<SegmentElement>();
filterElements.Add(filterElement);
    

 

The ContactSegment model is the main container that we'll be sending to the API. It contains all of the elements that we're created above :

 

                   ContactSegment segment = new ContactSegment
                                         {
                                             detph = "complete",
                                             id = -500002,
                                             elements = filterElements,
                                             name = "sample segment",
                                             type = "ContactSegment"
};
    

 

Finally, we'll provide a function that accepts this ContactSegment as an argument and invokes an HTTP POST request to create the record :

 

        publicContactSegment CreateSegment(ContactSegment segment)
        {
            RestRequest request = new RestRequest(Method.POST)
                                      {
                                          RequestFormat = DataFormat.Json,
                                          Resource = "/assets/contact/segment"
                                      };
            request.AddBody(segment);

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

 

We've started with a simple Contact Segment, but please let us know if you're interested in working with other Filters or Criteria and we'll be glad to add them to the project.

 

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