-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
160 lines (121 loc) · 6.23 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using EloquaTestClient.EloquaServiceNew;
namespace EloquaTestClient
{
class Program
{
static void Main()
{
bool success = false;
int contactId = 0;
var contactIDs = new int[1];
try
{
#region Setup the Eloqua service
//var service = new EloquaInstance("TestInstance", "Webtrends\\eloqua.api", "fyG9e1opgc!");
EloquaServiceClient service = new EloquaServiceClient();
service.ClientCredentials.UserName.UserName = @"";
service.ClientCredentials.UserName.Password = "";
#endregion
#region Create a contact using a Dynamic Entity
// Build a Contact Entity Type object
EntityType entityType = new EntityType { ID = 0, Name = "Contact", Type = "Base" };
// Create an 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();
dynamicEntities[0].EntityType = entityType;
// Create a Dynamic Entity's Field Value Collection
dynamicEntities[0].FieldValueCollection = new DynamicEntityFields();
// Add the Contact's Email Address field to the Dynamic Entity’s field collection
dynamicEntities[0].FieldValueCollection.Add("C_EmailAddress", "cloud_support@eloqua.com");
// Add the Contact's First Name field to the Dynamic Entity’s field collection
dynamicEntities[0].FieldValueCollection.Add("C_FirstName", "Cloud");
// Execute the request
var result = service.Create(dynamicEntities);
// Verify the status of each Contact Create request in the results
foreach (CreateResult t in result)
{
// Successfull requests return a positive integer value for ID
if (t.ID != -1)
{
contactId = t.ID;
success = true;
}
// 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));
}
}
}
#endregion
#region Retrieve the contact dynamically
if (success)
{
// Set the ID of the Contact Entity
contactIDs[0] = contactId;
// Create a new list containing the fields you want populated
List<string> fieldList = new List<string>();
// Add the Contact’s Email Address to the field list
fieldList.Add("C_EmailAddress");
// Add the Contact’s First Name to the field list
fieldList.Add("C_FirstName");
// Build a Dynamic Entity array to store the results
DynamicEntity[] retrievedEntities;
// If the field list is empty - the request will return all Entity Fields
// Otherwise, only fields defined in the field list are returned
if (fieldList.Count == 0)
{
// Execute the request and return all of the Entity's fields
retrievedEntities = service.Retrieve(entityType, contactIDs, null);
}
else
{
// Execute the request and return only the selected Entity fields
retrievedEntities = service.Retrieve(entityType, contactIDs, fieldList.ToArray());
}
// If a result was found, extract the field values for each Dynamic Entity
if (retrievedEntities.Length > 0)
{
foreach (DynamicEntity dynamicEntity in retrievedEntities)
{
// Extract the Field Name and Value for each element in the collection
foreach (KeyValuePair<string, string> keyValPair in dynamicEntity.FieldValueCollection)
{
Console.WriteLine("New Contact Created Successfully!");
Console.WriteLine(String.Format("Field Name: {0}", keyValPair.Key));
Console.WriteLine(String.Format("Field Value: {0}", keyValPair.Value));
}
}
}
}
#endregion
// Customize your own error handling
}
catch (System.ServiceModel.FaultException ex)
{
// Catch Service Model Fault Exceptions
Console.WriteLine(String.Format("Reson: {0}", ex.Reason.ToString()));
Console.WriteLine(String.Format("Fault Type: {0}", ex.GetType().ToString()));
Console.WriteLine(String.Format("Fault Code: {0}", ex.Code.Name.ToString()));
}
catch (Exception ex)
{
// Catch System Exceptions
Console.WriteLine(String.Format("Exception Message: {0}", ex.Message.ToString()));
}
// Wait for user input before stepping out.
Console.WriteLine("Press any key to continue.");
Console.ReadKey();
}
}
}