Previous posts have shown how to create a Contact or a Data Card, and even how to add a Contact to a Contact Group

 

But what if the group that you want to add a contact to does not already exist in Eloqua?

 

In this post we will look at how to create a new Contact Group in Eloqua.

Note: we will assume that the code from here is also being used.


On to the code...

 

class CreateContactGroup
    {
        static void Main()
        {
            try
            {               
                //Set up the Eloqua Service
                var service = new EloquaInstance("instance", "userid", "password");

                // Build a Contact Group Asset Type object
                var assetType = new AssetType {ID = 0, Name = "ContactGroupName", Type = "ContactGroup"};

                // Create a new Array of Dynamic Assets
                DynamicAsset[] dynamicAssets = new DynamicAsset[1];

                // Create a new Dynamic Asset
                dynamicAssets[0] = new DynamicAsset
                                       {
                                           AssetType = assetType,
                                           FieldValueCollection = new DynamicAssetFields()
                                       };

                // Create a Dynamic Asset's Field Value Collection
                // Add the Contact Group's Name field to the Field Collection
                dynamicAssets[0].FieldValueCollection.Add("name", "TJs API Contacts");

                // Add the Contact Group's Description field to the Field Collection
                dynamicAssets[0].FieldValueCollection.Add("description", "A contact group created through the SOAP API");

                // Execute the request
                CreateAssetResult[] result = service.ServiceProxy.CreateAsset(dynamicAssets);

                // Verify the status of each Contact Group Create request in the results
                for (int i = 0; i < result.Length; i++)
                {
                    // Successfull requests return a positive integer value for ID
                    if (result[i].ID != -1)
                    {
                        Console.WriteLine("Success");
                    }
                    // Failed requests return a -1 integer value for ID
                    else
                    {
                        Console.WriteLine("Failure");

                        // Extract the Error Message and Error Code for each failed Create request
                        foreach (Error createError in result[i].Errors)
                        {
                            Console.WriteLine(String.Format("Code: {0}", createError.ErrorCode));
                            Console.WriteLine(String.Format("Message: {0}", createError.Message));
                        }
                    }
                }
            }
            catch (System.ServiceModel.FaultException ex)
            {
                // Customize your own Error handling code.
                Console.WriteLine(String.Format("Reson: {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)
            {
                // Customize your own Error handling code.
                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 code executes, as long as the group name does not already exist in the instance that you are using, the new group will be created in Eloqua.  See Eloqua API How To: Describe an Asset Type for details on how to determine if a group of the same name already exists in an Eloqua instance.

newContactGroup.png

 

Once the Group has been successfully created, you will have the ID of the group that you can use to add a Contact to it.