2009-11-03

Using VB 2008 Express as client to consume SugarCRM Web service

In additional to my previous example of using AXIS as web client, I now use VB.NET as another example to consume web service. Moreover, I will just the open source SugarCRM as web service provider.

SugarCRM
The wsdl link is as follows (the host name depends on your installation. I use localhost here)
http://127.0.0.1/sugarcrm/soap.php?wsdl
as shown in the following IE screen dump


VB
The set up is a little bit tricky, as the initial menu does not have any web service item. The steps are as follows:
(1) Project > Add Service Reference


(2) Click the "Advanced" button (Do not type the WDSL link yet)


(3) Click the "Add Web Reference" button


(4) Type the WSDL link and press "Go" button. The web service description will be displayed. Then choose a reference name you like (I use SugarCRM) and press "Add Reference"


(5) You will find VB has added the WS reference in the Solution Explorer


(6) Ironically, VB will now have the "Add Web Referebce" menu item available in the Project menu


7) Now you can start to write codes to consume the web service. Since my example just illustrate the principle, I have hard-coded many things. The logic is there is a Button1. If I click this button, it will call the login method of SugarCRM. The session id returned will be displayed as Label1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim soapservice As New SugarCRM.sugarsoap
Dim credential As New SugarCRM.user_auth
Dim opensession As New SugarCRM.set_entry_result
credential.user_name = "admin"
credential.password = getMd5Hash("password")
credential.version = "1.0"
opensession = soapservice.login(credential, "SugarCRM")
Label1.Text = opensession.id
End Sub


The convenience of using VB is its IntelliSense which can enumerate the available options.


I have declared three objects
soapservice - the client stub
credential - the first argument of the service, containing the user id, md5 of the password and the version.
openssion - the returned result of the service. One of the most important field is the session ID

The result is shown below