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.
1 Answer
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 = "[email protected]";
string password = "somecomplexpasswordPlusSecurityToken";
SforceService sfdcBinding = new SforceService();
LoginResult currentLoginResult = null;
try
{
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 = '[email protected]'");
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.";
}