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.
1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17import com.sforce.soap.partner.PartnerConnection;
18import com.sforce.soap.partner.sobject.*;
19import com.sforce.soap.partner.*;
20import com.sforce.ws.ConnectorConfig;
21import com.sforce.ws.ConnectionException;
22import com.sforce.soap.partner.Error;
23import java.io.FileNotFoundException;
24import java.io.IOException;
25import java.io.InputStreamReader;
26import java.io.BufferedReader;
27import java.util.*;
28
29public class PartnerSamples {
30 PartnerConnection partnerConnection = null;
31 private static BufferedReader reader =
32 new BufferedReader(new InputStreamReader(System.in));
33
34 public static void main(String[] args) {
35 PartnerSamples samples = new PartnerSamples();
36 if (samples.login()) {
37 // Add calls to the methods in this class.
38 // For example:
39 // samples.querySample();
40 }
41 }
42
43 private String getUserInput(String prompt) {
44 String result = "";
45 try {
46 System.out.print(prompt);
47 result = reader.readLine();
48 } catch (IOException ioe) {
49 ioe.printStackTrace();
50 }
51 return result;
52 }
53
54 private boolean login() {
55 boolean success = false;
56 String username = getUserInput("Enter username: ");
57 String password = getUserInput("Enter password: ");
58 String authEndPoint = getUserInput("Enter auth end point: ");
59
60 try {
61 ConnectorConfig config = new ConnectorConfig();
62 config.setUsername(username);
63 config.setPassword(password);
64
65 config.setAuthEndpoint(authEndPoint);
66 config.setTraceFile("traceLogs.txt");
67 config.setTraceMessage(true);
68 config.setPrettyPrintXml(true);
69
70 partnerConnection = new PartnerConnection(config);
71
72 success = true;
73 } catch (ConnectionException ce) {
74 ce.printStackTrace();
75 } catch (FileNotFoundException fnfe) {
76 fnfe.printStackTrace();
77 }
78
79 return success;
80 }
81
82 //
83 // Add your methods here.
84 //
85}
86Sample 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;.
1swfobject.registerObject("clippy.codeblock-1", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17using System;
18using System.Collections.Generic;
19using System.Linq;
20using System.Text;
21using System.Web.Services.Protocols;
22using System.Collections;
23using TemplatePartner.sforce;
24
25namespace TemplatePartner
26{
27 class PartnerSamples
28 {
29 private SforceService binding;
30
31 static void Main(string[] args)
32 {
33 PartnerSamples samples = new PartnerSamples();
34 if (samples.login())
35 {
36 // Add calls to the methods in this class.
37 // For example:
38 // samples.querySample();
39 }
40 }
41
42 private bool login()
43 {
44 Console.Write("Enter username: ");
45 string username = Console.ReadLine();
46 Console.Write("Enter password: ");
47 string password = Console.ReadLine();
48
49 // Create a service object
50 binding = new SforceService();
51
52 // Timeout after a minute
53 binding.Timeout = 60000;
54
55 // Try logging in
56 LoginResult lr;
57 try
58 {
59
60 Console.WriteLine("\nLogging in...\n");
61 lr = binding.login(username, password);
62 }
63
64 // ApiFault is a proxy stub generated from the WSDL contract when
65 // the web service was imported
66 catch (SoapException e)
67 {
68 // Write the fault code to the console
69 Console.WriteLine(e.Code);
70
71 // Write the fault message to the console
72 Console.WriteLine("An unexpected error has occurred: " + e.Message);
73
74 // Write the stack trace to the console
75 Console.WriteLine(e.StackTrace);
76
77 // Return False to indicate that the login was not successful
78 return false;
79 }
80
81 // Check if the password has expired
82 if (lr.passwordExpired)
83 {
84 Console.WriteLine("An error has occurred. Your password has expired.");
85 return false;
86 }
87
88 // Set the returned service endpoint URL
89 binding.Url = lr.serverUrl;
90
91 // Set the SOAP header with the session ID returned by
92 // the login result. This will be included in all
93 // API calls.
94 binding.SessionHeaderValue = new SessionHeader();
95 binding.SessionHeaderValue.sessionId = lr.sessionId;
96
97 // Return true to indicate that we are logged in, pointed
98 // at the right URL and have our security token in place.
99 return true;
100 }
101
102 //
103 // Add your methods here.
104 //
105}
106This partner WSDL samples are: