Whether your application creates new contacts or just manipulates existing ones, contacts and groups are a core part of Eloqua’s marketing automation capabilities, and you will likely find yourself adding contacts to groups regularly.  This is quite a simple process using the Eloqua API.  To do so, you’ll need the ContactGroupID, which you can retrieve with a DescribeAssetType call.  We'll use the code for the standard setup for the Eloqua API that we looked at earlier.

 

Once you have this ID, to add members to the group is simple. For each contact, create a new DynamicEntity, of type Contact, with the ID of the contact you want to add.  Then, create a DynamicAsset of type ContactGroup, with the ID of the contact group you would like to add the contact to.  Then, call AddGroupMember to add the DynamicEntity to the DynamicAsset, and your contact is added to the group.

 

 

        public List<EloquaContact> ImportContactsIntoGroup(List<EloquaContact> tmpContacts, int intContactGroupID)

        {

            if (intContactGroupID > 0)

            {

                foreach (EloquaContact tmpContact in tmpContacts)

                {

                    if (tmpContact.ContactID != 0)

                    {

                        try

                        {

 

                            // Execute the request

                            EloquaServiceNew.EntityType entityType = new EloquaServiceNew.EntityType();

 

                            entityType.ID = 0;

                            entityType.Name = "Contact";

                            entityType.Type = "Base";

 

                            // Instantiate a new Dynamic Entity of type Contact and supply the EntityID

                            EloquaServiceNew.DynamicEntity entity = new EloquaServiceNew.DynamicEntity { EntityType = entityType, Id = tmpContact.ContactID };

 

                            // Build a Contact Group Asset Type object

                            EloquaServiceNew.AssetType assetType = new EloquaServiceNew.AssetType();

 

                            assetType.ID = 0;

                            assetType.Type = "ContactGroup";

                            assetType.Name = "ContactGroupName";

 

                            // Instantiate a new Dynamic Asset of type ContactGroup and supply the AssetID

                            EloquaServiceNew.DynamicAsset asset = new EloquaServiceNew.DynamicAsset { AssetType = assetType, Id = intContactGroupID };

 

                            // Execute the request

                            EloquaServiceNew.GroupMemberResult result = serviceProxy.AddGroupMember(entity, asset);

 

                            if (result.Success)

                            {

                            }

                            else

                            {

                                foreach (EloquaServiceNew.Error error in result.Errors)

                                {

                                    //Console.WriteLine(error.ErrorCode);

                                    //Console.WriteLine(error.Message);

                                }

                            }

                        }

                        catch (Exception ex)

                        {

                            Trace.TraceWarning(String.Format("Exception in EloquaInstance:ImportContactsIntoGroup {0} :: {1}", ex.Message, ex.InnerException), "Exception");

                            throw;

                        }

                    }

                }

            }

            return tmpContacts;

        }