Last time, we looked at importing the SugarCRM SOAP classes into Visual Studion Express 2008. Now, let’s actually make use of it
Most of the calls use some sort of session ID, which is a string that Sugar uses to determine the validity and possibly origin of a SOAP request. The SOAP interface is, after all publically available.
This is retrieved by a logon process, as follows-
private string session;
// user auth object
private user_auth user_authorisation = new user_auth();
private void doLogin()
{
// login stuff
// authorisation object
user_authorisation.user_name = “username”;
user_authorisation.password = computeMD5String(“mysecretpassword”);
sugar_SOAP_obj.loginCompleted += new loginCompletedEventHandler(sugar_SOAP_obj_loginCompleted);
sugar_SOAP_obj.loginAsync(user_authorisation, “”);
Console.WriteLine(“login initiated”);
}// login completed actions
private void sugar_SOAP_obj_loginCompleted(object sender, loginCompletedEventArgs e)
{
sugar_SOAP_obj.loginCompleted -= new loginCompletedEventHandler(sugar_SOAP_obj_loginCompleted);
Console.WriteLine(“Login has completed with the result:” + e.Result.id);
session = e.Result.id;
}
The MD5 hash function is as follows:
private int i = 0;
// compute MD5 string for authentication
public string computeMD5String(string PlainText)
{
MD5 md5 = MD5.Create();
byte[] inputBuffer = System.Text.Encoding.ASCII.GetBytes(PlainText);
byte[] outputBuffer = md5.ComputeHash(inputBuffer);//Convert the byte[] to a hex-string
StringBuilder builder = new StringBuilder(outputBuffer.Length);
for (int i = 0; i < outputBuffer.Length; i++)
{
builder.Append(outputBuffer[i].ToString(“X2″));
}return builder.ToString();
}
The code above does the following:
1) Creates a string to hold the session.
2) Creates a new user authorisation object, which contains the username and *the hashed password*- I got stuck for half a day working out it needs to be hashed!
3) doLogin populates the user authorisation object with teh appropriate values, creates a listener for the completion of teh authorisation process, then actually initiates the process.
4) sugar_SOAP_obj_loginCompleted function assigns the session string to the session variable. This looks something like ueuqq9obclhbrbrbqk1mat0s07
You may notice that there are non-asynchronous functions available, but I prefer to use asynchronous calls, because they don’t ‘hang’ the application while waiting for a long call to complete. This may save you from calls from users whinging that the application is non-responsive, and async calls allow for triggering a progress bar or ‘Please Wait’ message or something similar.
Now we’re logged in, with a session ID, we can start to do more useful things. Next time, we’ll look at adding a contact using SOAP calls.
