My last post in this series consisted of a code snippet to Eloqua API How To: Query a Contact by Email Address.

 

In this post, I will be showing how to retrieve Metadata for an Entity, in this case, a Contact.

 

Once again, I will be assuming that the code found Connecting to the Eloqua API - Generic Setup is being used to establish a connection to the Eloqua API.

 

For this example, we will once again be using 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 MetaDataRequest
    {

         static void Main()
          {


            String entity = "contact";

 

            try
            {
               

               //Create the service instance using your credentials

               EloquaInstance service = new EloquaInstance("instance", "userid", "password");

               

               // Execute a request to list all Entity Types
                ListEntityTypesResult entityTypesResults = service.ServiceProxy.ListEntityTypes();

               

               // Extract the name of each Entity Type
                foreach (String type in entityTypesResults.EntityTypes)
                {
                    Console.WriteLine(type);
                }

               

                // Contact is a member of the Base Entity Type
                // Execute a request to describe the Base Entity Type
                DescribeEntityTypeResult typeResult = service.ServiceProxy.DescribeEntityType("Base");

               

               // Instantiate a new Entity Type object
                EntityType entityType = new EntityType();

              

                // Extract the ID, Name and Type of each Entity Type
                foreach (EntityType entityTypeResult in typeResult.EntityTypes)
                {
                    if (String.Compare(entity, entityTypeResult.Name, true) == 0)
                    {
                        entityType.ID = entityTypeResult.ID;
                        entityType.Name = entityTypeResult.Name;
                        entityType.Type = entityTypeResult.Type;
                        break;
                    }
                }

                // Execute a request to describe the Contact Entity
                DescribeEntityResult result = service.ServiceProxy.DescribeEntity(entityType);

              

                // Extract the Entity's Properties
                Console.WriteLine(String.Format("Is Createable: {0}", result.IsCreateable));
                Console.WriteLine(String.Format("Is Deletable: {0}", result.IsDeletable));
                Console.WriteLine(String.Format("Is Queryable: {0}", result.IsQueryable));
                Console.WriteLine(String.Format("Is Retrievable: {0}", result.IsRetrievable));
                Console.WriteLine(String.Format("Is Updateable: {0}", result.IsUpdateable));

              

                // Extract the Entity's Field Metadata
                foreach (DynamicEntityFieldDefinition fieldDef in result.Fields)
                {
                    Console.WriteLine(String.Format("Data Type: {0}", fieldDef.DataType));
                    Console.WriteLine(String.Format("Default Value: {0}", fieldDef.DefaultValue));
                    Console.WriteLine(String.Format("Display Name: {0}", fieldDef.DisplayName));
                    Console.WriteLine(String.Format("Internal Name: {0}", fieldDef.InternalName));
                    Console.WriteLine(String.Format("Is Custom: {0}", fieldDef.IsCustom));
                    Console.WriteLine(String.Format("Is Required: {0}", fieldDef.IsRequired));
                    Console.WriteLine(String.Format("Is Writeable: {0}", fieldDef.IsWriteable));
                    Console.WriteLine(String.Format("Length: {0}", fieldDef.Length));
                }

            

            }
           

            // 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