1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6
7using System.ServiceModel;
8using Walkthrough.sforce;
9
10namespace Walkthrough
11{
12 class QuickstartApiSample
13 {
14 private static SoapClient loginClient; // for login endpoint
15 private static SoapClient client; // for API endpoint
16 private static SessionHeader header;
17 private static EndpointAddress endpoint;
18
19 static void Main(string[] args)
20 {
21 QuickstartApiSample sample = new QuickstartApiSample();
22 sample.run();
23 }
24
25 public void run()
26 {
27 // Make a login call
28 if (login())
29 {
30 // Do a describe global
31 describeGlobalSample();
32
33 // Describe an account object
34 describeSObjectsSample();
35
36 // Retrieve some data using a query
37 querySample();
38
39 // Log out
40 logout();
41 }
42 }
43
44 private bool login()
45 {
46 Console.Write("Enter username: ");
47 string username = Console.ReadLine();
48 Console.Write("Enter password: ");
49 string password = Console.ReadLine();
50
51 // Create a SoapClient specifically for logging in
52 loginClient = new SoapClient();
53
54 // (combine pw and token if necessary)
55 LoginResult lr;
56 try
57 {
58 Console.WriteLine("\nLogging in...\n");
59 lr = loginClient.login(null, username, password);
60 }
61 catch (Exception e)
62 {
63 // Write the fault message to the console
64 Console.WriteLine("An unexpected error has occurred: " + e.Message);
65
66 // Write the stack trace to the console
67 Console.WriteLine(e.StackTrace);
68 return false;
69 }
70
71 // Check if the password has expired
72 if (lr.passwordExpired)
73 {
74 Console.WriteLine("An error has occurred. Your password has expired.");
75 return false;
76 }
77
78 /** Once the client application has logged in successfully, it will use
79 * the results of the login call to reset the endpoint of the service
80 * to the virtual server instance that is servicing your organization
81 */
82
83 // On successful login, cache session info and API endpoint info
84 endpoint = new EndpointAddress(lr.serverUrl);
85
86 /** The sample client application now has a cached EndpointAddress
87 * that is pointing to the correct endpoint. Next, the sample client
88 * application sets a persistent SOAP header that contains the
89 * valid sessionId for our login credentials. To do this, the sample
90 * client application creates a new SessionHeader object. Add the session
91 * ID returned from the login to the session header
92 */
93 header = new SessionHeader();
94 header.sessionId = lr.sessionId;
95
96 // Create and cache an API endpoint client
97 client = new SoapClient("Soap", endpoint);
98
99 printUserInfo(lr, lr.serverUrl);
100
101 // Return true to indicate that we are logged in, pointed
102 // at the right URL and have our security token in place.
103 return true;
104 }
105
106 private void printUserInfo(LoginResult lr, String authEP)
107 {
108 try
109 {
110 GetUserInfoResult userInfo = lr.userInfo;
111
112 Console.WriteLine("\nLogging in ...\n");
113 Console.WriteLine("UserID: " + userInfo.userId);
114 Console.WriteLine("User Full Name: " +
115 userInfo.userFullName);
116 Console.WriteLine("User Email: " +
117 userInfo.userEmail);
118 Console.WriteLine();
119 Console.WriteLine("SessionID: " +
120 lr.sessionId);
121 Console.WriteLine("Auth End Point: " +
122 authEP);
123 Console.WriteLine("Service End Point: " +
124 lr.serverUrl);
125 Console.WriteLine();
126 }
127 catch (Exception e)
128 {
129 Console.WriteLine("An unexpected error has occurred: " + e.Message +
130 " Stack trace: " + e.StackTrace);
131 }
132 }
133
134 private void logout()
135 {
136 try
137 {
138 client.logout(header);
139 Console.WriteLine("Logged out.");
140 }
141 catch (Exception e)
142 {
143 // Write the fault message to the console
144 Console.WriteLine("An unexpected error has occurred: " + e.Message);
145
146 // Write the stack trace to the console
147 Console.WriteLine(e.StackTrace);
148 }
149 }
150
151 /**
152 * To determine the objects that are available to the logged-in
153 * user, the sample client application executes a describeGlobal
154 * call, which returns all of the objects that are visible to
155 * the logged-in user. This call should not be made more than
156 * once per session, as the data returned from the call likely
157 * does not change frequently. The DescribeGlobalResult is
158 * simply echoed to the console.
159 */
160 private void describeGlobalSample()
161 {
162 try
163 {
164 // describeGlobal() returns an array of object results that
165 // includes the object names that are available to the logged-in user.
166 DescribeGlobalResult dgr = client.describeGlobal(
167 header, // session header
168 null // package version header
169 );
170
171 Console.WriteLine("\nDescribe Global Results:\n");
172 // Loop through the array echoing the object names to the console
173 for (int i = 0; i < dgr.sobjects.Length; i++)
174 {
175 Console.WriteLine(dgr.sobjects[i].name);
176 }
177 }
178 catch (Exception e)
179 {
180 Console.WriteLine("An exception has occurred: " + e.Message +
181 "\nStack trace: " + e.StackTrace);
182 }
183 }
184
185 /**
186 * The following method illustrates the type of metadata
187 * information that can be obtained for each object available
188 * to the user. The sample client application executes a
189 * describeSObject call on a given object and then echoes
190 * the returned metadata information to the console. Object
191 * metadata information includes permissions, field types
192 * and length and available values for picklist fields
193 * and types for referenceTo fields.
194 */
195 private void describeSObjectsSample()
196 {
197 Console.Write("\nType the name of the object to " +
198 "describe (try Account): ");
199 string objectType = Console.ReadLine();
200 try
201 {
202
203 // Call describeSObjects() passing in an array with one object type name
204 DescribeSObjectResult[] dsrArray =
205 client.describeSObjects(
206 header, // session header
207 null, // package version header
208 null, // locale options
209 new string[] { objectType } // object name array
210 );
211
212 // Since we described only one sObject, we should have only
213 // one element in the DescribeSObjectResult array.
214 DescribeSObjectResult dsr = dsrArray[0];
215
216 // First, get some object properties
217 Console.WriteLine("\n\nObject Name: " + dsr.name);
218
219 if (dsr.custom) Console.WriteLine("Custom Object");
220 if (dsr.label != null) Console.WriteLine("Label: " + dsr.label);
221
222 // Get the permissions on the object
223 if (dsr.createable) Console.WriteLine("Createable");
224 if (dsr.deletable) Console.WriteLine("Deleteable");
225 if (dsr.queryable) Console.WriteLine("Queryable");
226 if (dsr.replicateable) Console.WriteLine("Replicateable");
227 if (dsr.retrieveable) Console.WriteLine("Retrieveable");
228 if (dsr.searchable) Console.WriteLine("Searchable");
229 if (dsr.undeletable) Console.WriteLine("Undeleteable");
230 if (dsr.updateable) Console.WriteLine("Updateable");
231
232 Console.WriteLine("Number of fields: " + dsr.fields.Length);
233
234 // Now, retrieve metadata for each field
235 for (int i = 0; i < dsr.fields.Length; i++)
236 {
237 // Get the field
238 Field field = dsr.fields[i];
239
240 // Write some field properties
241 Console.WriteLine("Field name: " + field.name);
242 Console.WriteLine("\tField Label: " + field.label);
243
244 // This next property indicates that this
245 // field is searched when using
246 // the name search group in SOSL
247 if (field.nameField)
248 Console.WriteLine("\tThis is a name field.");
249
250 if (field.restrictedPicklist)
251 Console.WriteLine("This is a RESTRICTED picklist field.");
252
253 Console.WriteLine("\tType is: " + field.type.ToString());
254
255 if (field.length > 0)
256 Console.WriteLine("\tLength: " + field.length);
257
258 if (field.scale > 0)
259 Console.WriteLine("\tScale: " + field.scale);
260
261 if (field.precision > 0)
262 Console.WriteLine("\tPrecision: " + field.precision);
263
264 if (field.digits > 0)
265 Console.WriteLine("\tDigits: " + field.digits);
266
267 if (field.custom)
268 Console.WriteLine("\tThis is a custom field.");
269
270 // Write the permissions of this field
271 if (field.nillable) Console.WriteLine("\tCan be nulled.");
272 if (field.createable) Console.WriteLine("\tCreateable");
273 if (field.filterable) Console.WriteLine("\tFilterable");
274 if (field.updateable) Console.WriteLine("\tUpdateable");
275
276 // If this is a picklist field, show the picklist values
277 if (field.type.Equals(fieldType.picklist))
278 {
279 Console.WriteLine("\tPicklist Values");
280 for (int j = 0; j < field.picklistValues.Length; j++)
281 Console.WriteLine("\t\t" + field.picklistValues[j].value);
282 }
283
284 // If this is a foreign key field (reference),
285 // show the values
286 if (field.type.Equals(fieldType.reference))
287 {
288 Console.WriteLine("\tCan reference these objects:");
289 for (int j = 0; j < field.referenceTo.Length; j++)
290 Console.WriteLine("\t\t" + field.referenceTo[j]);
291 }
292 Console.WriteLine("");
293 }
294 }
295 catch (Exception e)
296 {
297 Console.WriteLine("An exception has occurred: " + e.Message +
298 "\nStack trace: " + e.StackTrace);
299 }
300 Console.WriteLine("Press ENTER to continue...");
301 Console.ReadLine();
302 }
303
304 private void querySample()
305 {
306 String soqlQuery = "SELECT FirstName, LastName FROM Contact";
307 try
308 {
309 QueryResult qr = client.query(
310 header, // session header
311 null, // query options
312 null, // mru options
313 null, // package version header
314 soqlQuery // query string
315 );
316
317 bool done = false;
318
319 if (qr.size > 0)
320 {
321 Console.WriteLine("Logged-in user can see "
322 + qr.records.Length + " contact records.");
323
324 while (!done)
325 {
326 Console.WriteLine("");
327 sObject[] records = qr.records;
328 for (int i = 0; i < records.Length; i++)
329 {
330 Contact con = (Contact)records[i];
331 string fName = con.FirstName;
332 string lName = con.LastName;
333 if (fName == null)
334 Console.WriteLine("Contact " + (i + 1) + ": " + lName);
335 else
336 Console.WriteLine("Contact " + (i + 1) + ": " + fName
337 + " " + lName);
338 }
339
340 if (qr.done)
341 {
342 done = true;
343 }
344 else
345 {
346 qr = client.queryMore(
347 header, // session header
348 null, // query options
349 qr.queryLocator // query locator
350 );
351 }
352 }
353 }
354 else
355 {
356 Console.WriteLine("No records found.");
357 }
358 }
359 catch (Exception ex)
360 {
361 Console.WriteLine("\nFailed to execute query succesfully," +
362 "error message was: \n{0}", ex.Message);
363 }
364 Console.WriteLine("\nPress ENTER to continue...");
365 Console.ReadLine();
366 }
367 }
368}