Continuing along in our series of Eloqua API How-To posts, let's look at how to programmatically submit a form. 

 

We will assume that the code found Connecting to the Eloqua API - Generic Setupis being used to establish a connection to the Eloqua API.

 

This example uses the EloquaService API.  For more information on which API to use for specific functions, see my post Eloqua SOAP API - Which one do I use?.

 

Now for the code...

 

    class FormSubmit
    {
        static void Main()
        {
             try
            {
                EloquaInstance service = new EloquaInstance("Instance", "username", "password");

 

                //Build a Form Entity Type object

                //For this example, I am using a form that I have already created in Eloqua.

                EntityType entityType = new EntityType
                                            {
                                                ID = 16,
                                                Name = "TJsTestForm",
                                                Type = "Form"
                                            };

                //Create a new Array of Dynamic Entities
                DynamicEntity[] dynamicEntities = new DynamicEntity[1];

 

                // Create a new Dynamic Entity and add it to the Array of Entities
                dynamicEntities[0] = new DynamicEntity
                                         {
                                             EntityType = entityType,
         
                                             // Create a Dynamic Entity's Field Value Collection

                                             //My form contains 3 required fields, email, first, and last name
                                             FieldValueCollection = new DynamicEntityFields
                                                                        {
                                                                            {"EmailAddress", "cloud.help@eloqua.com"},
                                                                            {"FirstName", "Cloud"},
                                                                            {"LastName", "Help"},
                                                                            {"FormID", "16"}
                                                                        }
                                         };

 

                // Execute the request
                CreateResult[] result = service.ServiceProxy.Create(dynamicEntities);

 

                // Verify the status of the submit
                foreach (CreateResult t in result)
                {
                    // Successfull requests return a positive integer value for ID
                    if (t.ID != -1)
                    {
                        Console.WriteLine(String.Format("Form Submit Successful!"));
                    }
                    // Failed requests return a -1 integer value for ID
                    else
                    {
                        // Extract the Error Message and Error Code for each failed Create request
                        foreach (Error createError in t.Errors)
                        {
                            Console.WriteLine(String.Format("Code: {0}", createError.ErrorCode));
                            Console.WriteLine(String.Format("Message: {0}", createError.Message));
                        }
                    }
                }
            }


            // Customize your own error handling code
            catch (System.ServiceModel.FaultException ex)
            {
                // Catch Service Model Fault Exceptions
                Console.WriteLine(String.Format("Reason: {0}", ex.Reason));
                Console.WriteLine(String.Format("Fault Type: {0}", ex.GetType()));
                Console.WriteLine(String.Format("Fault Code: {0}", ex.Code.Name));
            }
            catch (Exception ex)
            {
                // Catch System Exceptions
                Console.WriteLine(String.Format("Exception Message: {0}", ex.Message));
            }

 

            // Wait for user input before stepping out.
            Console.WriteLine("Press any key to continue.");
            Console.ReadKey();
        }
    }

 

When the program is run, a console window will pop up showing the results of the query something like below:

 

Capture.PNG