We switched our implementation from pulling in the WSDL as a SERVICE reference to a WEB reference. But we are not able to authenticate.Perviouly we used SOAPClient and added the credentials using binding and endpoint. What is method to add authentication credentials to the PartnerAPI object before calling PartnerAPI.Create.

  • With "Service Reference to Web Reference", do you mean to say that you migrated from Salesforce Soap services to Salesforce Rest Services? Feb 20 '15 at 17:31

You should create a SessionHeader on the proxy class that a web reference creates. It will be a property. In the SessionHeader you need to set the SessionId.

The code will be something like (modified version of Integrating Force.com with Microsoft .NET):

string userName = "username@domain.com";
string password = "somecomplexpasswordPlusSecurityToken";
SforceService sfdcBinding = new SforceService();
LoginResult currentLoginResult = null;
   currentLoginResult = sfdcBinding.login(userName, password);
catch (Exception e) {
   sfdcBinding = null;
   throw e;

// Configure the service to use the correct instance and session details
sfdcBinding.Url = currentLoginResult.serverUrl;
sfdcBinding.SessionHeaderValue = new SessionHeader();
sfdcBinding.SessionHeaderValue.sessionId = currentLoginResult.sessionId;

QueryResult queryResult = sfdcBinding.query("select FirstName from Lead where email = 'john.smith@salesforce.com'");

if (queryResult.size > 0) {
  //put some code in here to handle the records being returned
  int i = 0;
  Lead lead = (Lead)queryResult.records[i];
  string firstName = lead.FirstName;
} else {
  //put some code in here to handle no records being returned
  string message = "No records returned.";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.