Newer Version Available
Examples Using the Partner WSDL
After you generate the proxy client code and set up your development environment, you can start writing your client application. First, your application needs to log into the Salesforce service using the partner authentication endpoint. After a successful login, you can execute the sample methods.
For your convenience, template classes are provided, one in Java and one in C#, that make a login call. You can use them to execute the sample methods provided later in this section.
Sample template class for Java: This sample prompts the user to enter the username, password, and authentication endpoint. Next, it logs the user in. For the authentication endpoint URL, pass in the endpoint found in the partner WSDL file.
1import com.sforce.soap.partner.PartnerConnection;
2import com.sforce.soap.partner.sobject.*;
3import com.sforce.soap.partner.*;
4import com.sforce.ws.ConnectorConfig;
5import com.sforce.ws.ConnectionException;
6import com.sforce.soap.partner.Error;
7import java.io.FileNotFoundException;
8import java.io.IOException;
9import java.io.InputStreamReader;
10import java.io.BufferedReader;
11import java.util.*;
12
13public class PartnerSamples {
14 PartnerConnection partnerConnection = null;
15 private static BufferedReader reader =
16 new BufferedReader(new InputStreamReader(System.in));
17
18 public static void main(String[] args) {
19 PartnerSamples samples = new PartnerSamples();
20 if (samples.login()) {
21 // Add calls to the methods in this class.
22 // For example:
23 // samples.querySample();
24 }
25 }
26
27 private String getUserInput(String prompt) {
28 String result = "";
29 try {
30 System.out.print(prompt);
31 result = reader.readLine();
32 } catch (IOException ioe) {
33 ioe.printStackTrace();
34 }
35 return result;
36 }
37
38 private boolean login() {
39 boolean success = false;
40 String username = getUserInput("Enter username: ");
41 String password = getUserInput("Enter password: ");
42 String authEndPoint = getUserInput("Enter auth end point: ");
43
44 try {
45 ConnectorConfig config = new ConnectorConfig();
46 config.setUsername(username);
47 config.setPassword(password);
48
49 config.setAuthEndpoint(authEndPoint);
50 config.setTraceFile("traceLogs.txt");
51 config.setTraceMessage(true);
52 config.setPrettyPrintXml(true);
53
54 partnerConnection = new PartnerConnection(config);
55
56 success = true;
57 } catch (ConnectionException ce) {
58 ce.printStackTrace();
59 } catch (FileNotFoundException fnfe) {
60 fnfe.printStackTrace();
61 }
62
63 return success;
64 }
65
66 //
67 // Add your methods here.
68 //
69}Sample template class for C#: This sample prompts the user to enter the username and password. Next, it logs the user in. The project name for this sample is assumed to be TemplatePartner and the Web reference name sforce. If these values are different for your project, make sure to change the using directive to appropriate values for your project: using your_project_name.web_reference_name;.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Web.Services.Protocols;
6using System.Collections;
7using TemplatePartner.sforce;
8
9namespace TemplatePartner
10{
11 class PartnerSamples
12 {
13 private SforceService binding;
14
15 static void Main(string[] args)
16 {
17 PartnerSamples samples = new PartnerSamples();
18 if (samples.login())
19 {
20 // Add calls to the methods in this class.
21 // For example:
22 // samples.querySample();
23 }
24 }
25
26 private bool login()
27 {
28 Console.Write("Enter username: ");
29 string username = Console.ReadLine();
30 Console.Write("Enter password: ");
31 string password = Console.ReadLine();
32
33 // Create a service object
34 binding = new SforceService();
35
36 // Timeout after a minute
37 binding.Timeout = 60000;
38
39 // Try logging in
40 LoginResult lr;
41 try
42 {
43
44 Console.WriteLine("\nLogging in...\n");
45 lr = binding.login(username, password);
46 }
47
48 // ApiFault is a proxy stub generated from the WSDL contract when
49 // the web service was imported
50 catch (SoapException e)
51 {
52 // Write the fault code to the console
53 Console.WriteLine(e.Code);
54
55 // Write the fault message to the console
56 Console.WriteLine("An unexpected error has occurred: " + e.Message);
57
58 // Write the stack trace to the console
59 Console.WriteLine(e.StackTrace);
60
61 // Return False to indicate that the login was not successful
62 return false;
63 }
64
65 // Check if the password has expired
66 if (lr.passwordExpired)
67 {
68 Console.WriteLine("An error has occurred. Your password has expired.");
69 return false;
70 }
71
72 // Set the returned service endpoint URL
73 binding.Url = lr.serverUrl;
74
75 // Set the SOAP header with the session ID returned by
76 // the login result. This will be included in all
77 // API calls.
78 binding.SessionHeaderValue = new SessionHeader();
79 binding.SessionHeaderValue.sessionId = lr.sessionId;
80
81 // Return true to indicate that we are logged in, pointed
82 // at the right URL and have our security token in place.
83 return true;
84 }
85
86 //
87 // Add your methods here.
88 //
89}This partner WSDL samples are: