Vous êtes sur la page 1sur 12

!"#$"%$ '()*++*,) -.

$/
1he ldeal soluLlon for a problem 8log: !""#$%%&'()**)+,-*.#*/((+0-&
Mall: anLonlo.musarra[gmall.com

141111 1
This uocument is issueu with license Cieative Commons Attiibution-NonCommeicial-ShaieAlike


!"#$%#&' ! !"#$%& ()*+ !"# !"#$%&'(
Buiing this aiticle we will see how to make a .NET client to access the
infoimation iecoiueu on the CRN system anu use the client as the basis foi the
iealization of a simple application .NET C# that is able to peifoim a seiies of
elementaiy opeiations as: login, cieate a new contact anu ietiieve uata.
SugaiCRN (SugarCRM Inc.) expoits to the outsiue of the inteifaces that
suppoit S0AP anu REST piotocols, thiough these inteifaces pioviue access to
uata thiough a set of CR0B opeiations (SugarCRM Inc.). The veision of
SugaiCRN, which we will use, is the 6.1 Community Euition (SugarCRM Inc.).
We use the S0AP piotocol foi inteiaction between oui application .NET anu
SugaiCRN. Theie aie uiffeient ways to cieate a C# S0AP client can communicate
with a web seivice using .NET Fiamewoik (Microsoft) (Wikipedia).

The steps to follow to ieach oui goal aie:
ueneiating the S0AP client
Compiling a S0AP client, such as .NET Libiaiy Assembly
Cieate the application .NET C#

The tools we use to complete the woik aie:
0123 4+*567$+8 9:;: Runtime anu uevelopment tools. The iefeience
veision useu in this aiticle, is 4.u of the fiamewoik. The Web seivices
suppoit is howevei also suppoiteu by pievious veisions of the
fiamewoik.
9<*+=:6>6.$=: 0pen Souice Bevelopment Enviionment foi .NET. The
iefeience veision useu in this aiticle, is 4.1 (IC#Code)

The aiticle is intenueu foi an auuience that has the basic knowleuge of
softwaie uevelopment .NET platfoim anu in paiticulai by using the C# language,
as well as having some familiaiity with the technology of web seivices.

The entiie pioject souice coue maue in the aiticle is available on uitBub
iepositoiy at:
!""#($%%12"!'3+0-&%)&'()**)%4'1)*56785984:;<8=>?8@-"A/"5B2/C"D23*)*E








!"#$"%$ '()*++*,) -.$/
1he ldeal soluLlon for a problem 8log: !""#$%%&'()**)+,-*.#*/((+0-&
Mall: anLonlo.musarra[gmall.com

141111 2
This uocument is issueu with license Cieative Commons Attiibution-NonCommeicial-ShaieAlike


!" #$% '(%)*+,- ,. /012 34+%-*
To communicate with the SugaiCRN web seivice you must cieate a pioxy
class, a softwaie element that allows us to tianspaiently access the web seivice
without woiiying about the way this is calleu anu useu. The Pioxy class is
ueiiveu uiiectly fiom the WSBL uocument that uesciibes the web seivice.
SugaiCRN, veision S.S, intiouuceu suppoit foi veisioning to its API exposeu as
REST web seivice is S0AP, the veision of the API iefeience is 2.1 (SugarCRM
Inc.).
Thiough the ShaipBevelop IBE, using the Auu Web Refeience featuie, the
cieation of the pioxy class is faiily stiaightfoiwaiu. The cieation of the pioxy
class is an opeiation that can also be peifoimeu using the SBK tools .NET, in Box
2 shows how to cieate anu builu.
Following a biief summaiy of the tasks that must iun on ShaipBevelop to
cieate oui assembly that will be useu by the application .NET C# to communicate
with SugaiCRN:

Cieating a new Pioject Solution
Cieating the Pioxy Class thiough the Auu Web Refeience
Builuing the Pioject

The piocess of cieating the pioxy class iequiies the WSBL of the web
seivice of SugaiCRN, the WSBL can be iemote (http s) oi local (file system). In
oui case we will use an instance of SugaiCRN installeu on the platfoim PBPFog
(PHPFog) anu can be ieacheu at !""#($%%(!2*'(F0*&+#!#G-1)##+0-&. The figuies
shown to follow illustiate some of the impoitant phases in of the piocess of
cieating the pioject of ShaipBevelop.


4%/(+6 ? 3<6 @+6*#%$" $A "67 =+$B6@# *) * C.*)) D%E+*+F0

!"#$"%$ '()*++*,) -.$/
1he ldeal soluLlon for a problem 8log: !""#$%%&'()**)+,-*.#*/((+0-&
Mall: anLonlo.musarra[gmall.com

141111 S
This uocument is issueu with license Cieative Commons Attiibution-NonCommeicial-ShaieAlike


The new pioject must be cieateu using the Class Libiaiy template anu
specifying the piofile .NET appiopiiate (in oui case .NET Client Piofile 4). The
pioject will be cieateu within a solution that will take the name specifieu in the
Solution Name (see Figuie 1).



4%/(+6 G !@@6)) #$ #<6 A("@#%$" $A !HH I6E J6A6+6"@60
0nce the pioject is cieateu you can pioceeu with the cieation of the pioxy
class using the Auu Web Refeience accessible thiough the context menu of the
pioject.



4%/(+6 K !HH I6E J6A6+6"@6L 9(/*+CJ' 76E )6+>%@6,) I9:D0
The WSBL uocument that uesciibes the web seivice is accessible at
!""#($%%(!2*'(F0*&+#!#G-1)##+0-&%(/*H20/%H>8?%(-)#+#!#I,(.B. Foi ieasons of
simplicity I ueciueu to simplify the Refeience Name anu Namespace than those
pioposeu by uefault (see Figuie S). 0nce uownloaueu you can view the WSBL
opeiations exposeu by the web seivice thiough the tab "Available Web Seivices"
(see Figuie 4).

!"#$"%$ '()*++*,) -.$/
1he ldeal soluLlon for a problem 8log: !""#$%%&'()**)+,-*.#*/((+0-&
Mall: anLonlo.musarra[gmall.com

141111 4
This uocument is issueu with license Cieative Commons Attiibution-NonCommeicial-ShaieAlike



4%/(+6 M N=6+*#%$") 6O=$)6H EF #<6 76E )6+>%@60


4%/(+6 P Q%67 $A #<6 =+$B6@# *A#6+ #<6 $=6+*#%$" !HH I6E J6A6+6"@60
Figuie S shows the iesult obtaineu as the iesult of the Web Refeience. The
Refeience.cs file, contains oui Pioxy Class. To complete this fiist phase it only
iemains to complete the pioject, the builu piocess will cieate within the bin
uiiectoiy, the ull assembly.





!"#$"%$ '()*++*,) -.$/
1he ldeal soluLlon for a problem 8log: !""#$%%&'()**)+,-*.#*/((+0-&
Mall: anLonlo.musarra[gmall.com

141111 S
This uocument is issueu with license Cieative Commons Attiibution-NonCommeicial-ShaieAlike


5" #$% '(%)*+,- ,. *$% "67# 38 1994+')*+,-
Insiue the solution cieateu foi the pioject of the Pioxy class, we auu a new
C# pioject which we will call, foi example, SugaiCRN_S0AP_Console_Client,
using the template Winuows Console (Winuows Applications categoiy).
Befoie pioceeuing fuithei, we auu the new pioject, a iefeience to
System.Web.Seivices anu SugaiCRN_CE_S0AP_v21_ClientLibiaiy libiaiies. The
iefeience to the libiaiies must be auueu using the Auu Refeience fiom the
context menu of the pioject (see Figuie 6 anu Figuie 7).

The sample application we aie builuing must meet the following flow:

a) Log in to the specifieu usei;
b) Reau some infoimation about the system;
c) Reauing foi infoimation about who is loggeu in;
u) Inseiting a new contact;
e) Reauing uata fiom the contact listeu above;
f) 0sei logout.

Let us uesciibe biiefly what the Web seivice opeiations that we must use
in oiuei to iealize the specifieu flow anu ways of using the application itself. In
Table 1 aie shown in oiuei of the opeiations that use. Foi moie infoimation
about SugaiCRN API shoulu consult the uocumentation.


4%/(+6 R !HH J6A6+6"@60

!"#$"%$ '()*++*,) -.$/
1he ldeal soluLlon for a problem 8log: !""#$%%&'()**)+,-*.#*/((+0-&
Mall: anLonlo.musarra[gmall.com

141111 6
This uocument is issueu with license Cieative Commons Attiibution-NonCommeicial-ShaieAlike



4%/(+6 S D%)# $A +6A6+6"@6)0

N+H6+ N=6+*#%$") :6)@+%=#%$"
? get_seivei_info Retuins a set of veiy useful infoimation to iuentify the
uetails of installing SugaiCRN (example: veision,
euition, etc ...).
G login Peifoim the login piocess foi the usei specifieu,
ietuins the session iuentifiei (SessionIB). The value of
the SessionIB is useu in all opeiations that iequiie the
iuentification of the cuiient usei.
K get_usei_iu Retuins the IB of the usei specifying the cuiient
session.
M set_entiy Inseit an item foi the specifieu mouule, foi example, a
contact, leaus, accounts, etc ...
P get_entiy Retuins an object that iefeis to the specifieu mouule.
R logout Cuiient usei logs out.
3*E.6 ? D%)# $A I6E )6+>%@6 $=6+*#%$") ()6H EF #<6 *==.%@*#%$"0
0ui application (like Winuows Console) will accept as input a useiname anu
passwoiu, plus the auuiess (0RL) you wish to connect SugaiCRN instance.

Usage: SugarCRMClient.exe {username} {password} [SugarCRM URL]
D%)#%"/ ? T)%"/ #<6 @.%6"# 9(/*+CJ' >%* @$")$.60
The use of the client uoes not necessaiily iequiie you to specify the 0RL of
SugaiCRN, if not specifieu, will use the 0RL fiom which you cieateu the S0AP
client (Pioxy Class). Listing 1 shows how to use the commanu-line client anu
Listing 2 shows an example of use.


SugarCRMClient.exe amusarra amusarra https://shirus-crm.phpfogapps.com
D%)#%"/ G 2O*5=.6 $A @.%6"# ()*/60
!"#$"%$ '()*++*,) -.$/
1he ldeal soluLlon for a problem 8log: !""#$%%&'()**)+,-*.#*/((+0-&
Mall: anLonlo.musarra[gmall.com

141111 7
This uocument is issueu with license Cieative Commons Attiibution-NonCommeicial-ShaieAlike


The 0RL must not be to the WSBL uocument but iathei auuiess the ioot of
SugaiCRN, the application will then be iesponsible to constiuct the full 0RL of
the API. The output of the application will be uisplayeu on the console.
Summaiily we have uefineu the functions iequiieu anu how the application
shoulu be useu, at this point we biiefly analyze the coue of oui application. Coue
blocks aie shown to follow the flow in the oiuei inuicateu above.


// Create a new instance of SugarCRM SOAP Proxy Client
shirus.crm.phpfogapp.com.sugarsoap client = new shirus.crm.phpfogapp.com.sugarsoap();

if (SugarCRM_URL != null) {
client.Url = SugarCRM_URL.AbsoluteUri + "service/v2_1/soap.php";
}

/**
* Step 1) Try login to SugarCRM istance with username and password
*/

// Prepare a User Auth Object
shirus.crm.phpfogapp.com.user_auth userAuthInfo = new
shirus.crm.phpfogapp.com.user_auth();

userAuthInfo.user_name = sugarCRMUserName;
userAuthInfo.password = GetMD5Hash(sugarCRMPassword,false);

// Execute a login as admin
shirus.crm.phpfogapp.com.entry_value userSession = client.login(userAuthInfo,
"SugarCRM Console Client 1.0.0", null);

D%)#%"/ K D$/%" $=6+*#%$"0


/**
* Step 2) Get SugarCRM Server Info
*/
Console.WriteLine("SugarCRM EndPoint URL: " + client.Url);
Console.WriteLine("SugarCRM Server Version: " + client.get_server_info().version);
Console.WriteLine("SugarCRM Server Edition: " + client.get_server_info().flavor);
Console.WriteLine("SugarCRM Server Time: " + client.get_server_info().gmt_time);

/**
* Step 3) Get logged user info
*/
String userId = client.get_user_id(userSession.id);

Console.WriteLine("Welcome Your SessionID: " + userSession.id);
Console.WriteLine("Get user data...");
Console.WriteLine("---------- BEGIN USER DATA ----------");

shirus.crm.phpfogapp.com.get_entry_result_version2 userInfo =
client.get_entry(userSession.id, userSession.module_name, userId, null, null);

foreach (shirus.crm.phpfogapp.com.name_value nameValue in
userInfo.entry_list[0].name_value_list) {
if (nameValue.value.Length > 0 && nameValue.name != "user_hash") {
Console.WriteLine(nameValue.name + ": " + nameValue.value);
}
!"#$"%$ '()*++*,) -.$/
1he ldeal soluLlon for a problem 8log: !""#$%%&'()**)+,-*.#*/((+0-&
Mall: anLonlo.musarra[gmall.com

141111 8
This uocument is issueu with license Cieative Commons Attiibution-NonCommeicial-ShaieAlike


}

Console.WriteLine("---------- END USER DATA ----------");

D%)#%"/ M 9(/*+CJ' *"H )6+>6+ %"A$+5*#%$" 7<6" #<6 ()6+ .$//6H0


/**
* Step 4) Insert new contact
*/
Console.WriteLine("Try insert new Contact...");

Dictionary<string, string> contactsData = new Dictionary<string, string>();
contactsData.Add("first_name", "Antonio");
contactsData.Add("last_name","Musarra");
contactsData.Add("title", "IT Senior Consultant");
contactsData.Add("description", "Contacts created bye .NET SOAP Client");
contactsData.Add("email1","antonio.musarra@gmail.com");

shirus.crm.phpfogapp.com.name_value[] name_value_list = new
shirus.crm.phpfogapp.com.name_value[contactsData.Count];

int i = 0;
foreach (KeyValuePair<string, string> kvp in contactsData) {
name_value_list[i] = new shirus.crm.phpfogapp.com.name_value();
name_value_list[i].name = kvp.Key;
name_value_list[i].value = kvp.Value;
i++;
}

shirus.crm.phpfogapp.com.new_set_entry_result contactResult =
client.set_entry(userSession.id, "Contacts", name_value_list);
Console.WriteLine("New Contact as Id:" + contactResult.id);

D%)#%"/ P U")6+#%"/ * "67 @$"#*@#0


/**
* Step 5) Get My Contacts
*/
Console.WriteLine("Get data for Contacts: " + contactResult.id);
Console.WriteLine("---------- BEGIN CONTACTS DATA ----------");
shirus.crm.phpfogapp.com.get_entry_result_version2 myContacts =
client.get_entry(userSession.id, "Contacts", contactResult.id, null, null);

foreach (shirus.crm.phpfogapp.com.name_value nameValue in
myContacts.entry_list[0].name_value_list) {
if (nameValue.value.Length > 0) {
Console.WriteLine(nameValue.name + ": " + nameValue.value);
}
}
Console.WriteLine("---------- END CONTACTS DATA ----------");

/**
* Step 6) Logout
*/
client.logout(userSession.id);
Console.WriteLine("User disconnected");

!"#$"%$ '()*++*,) -.$/
1he ldeal soluLlon for a problem 8log: !""#$%%&'()**)+,-*.#*/((+0-&
Mall: anLonlo.musarra[gmall.com

141111 9
This uocument is issueu with license Cieative Commons Attiibution-NonCommeicial-ShaieAlike


D%)#%"/ R J6#+%6>%"/ @$"#*@# H*#*V *"H ()6+ .$/$(#0

I iemembei that the entiie pioject is available on the public iepositoiy
uitBub
!""#($%%12"!'3+0-&%)&'()**)%4'1)*56785984:;<8=>?8@-"A/"5B2/C"D23*)*E



4%/(+6 W J(""%"/ #<6 @.%6"# 9(/*+CJ'C.%6"# XYZ0


4%/(+6 [ J(""%"/ #<6 @.%6"# 9(/*+CJ'C.%6"# X?Z0



!"#$"%$ '()*++*,) -.$/
1he ldeal soluLlon for a problem 8log: !""#$%%&'()**)+,-*.#*/((+0-&
Mall: anLonlo.musarra[gmall.com

141111 1u
This uocument is issueu with license Cieative Commons Attiibution-NonCommeicial-ShaieAlike


199%-:+'%;

9(/*+CJ' $" \]\4$/
The SugaiCRN instance to which we iefeiieu is installeu on the clouu platfoim
PBPFog whose uata access aie as follows:

0RL: https:shiius-cim.phpfogapp.com
0sei Account:
Auministiatoi => auminaumin
0sei => amusaiiaamusaiia
-$O ? D$/%" %"A$+5*#%$" A$+ #<6 9(/*+CJ' %")#*"@60

3<6 @+6*#%$" $A #<6 \+$OF C.*)) >%* 9:; 0123
Thiough the web seivice's WSBL uocument, we can cieate the pioxy class using
the SBK tools .NET. The wsul.exe tool allows us to geneiate the pioxy class:

wsdl /language:CS /v /n:shirus.crm.phpfogapp.com /o:
SugarCRM_CE_SOAP_V21_ClientLibrary.cs https://shirus-
crm.phpfogapp.com/service/v2_1/soap.php?wsdl

The language paiametei allows us to specify one of the languages suppoiteu by.
NET Fiamewoik (in the example is iequiieu to geneiate a C # class). The iesult
of the pievious opeiation is the geneiation
SugaiCRN_CE_S0AP_v21_ClientLibiaiy.cs file, which contains the coue foi the
Pioxy Class foi SugaiCRN S0AP seivice.

At this point we just have to fill in the Pioxy Class, always using the SBK .NET:

csc /t:library /out: SugarCRM_CE_SOAP_V21_ClientLibrary.dll
SugarCRM_CE_SOAP_V21_ClientLibrary.cs /r:system.dll /r:system.xml.dll
/r:system.web.services.dll

In this way, we have compileu the class as a class libiaiy (t: libiaiy) inuicating
the iesult of compiling the assembly SugaiCRN_CE_S0AP_v21_ClientLibiaiy.ull,
anu using the metauata specifieu in the paiameteis i. At this point the file
SugaiCRN_CE_S0AP_v21_ClientLibiaiy.ull, can be useu as a libiaiy fiom. NET
applications.
-$O G C+6*#%"/ #<6 \+$OF C.*)) >%* #<6 9:;0 1230





!"#$"%$ '()*++*,) -.$/
1he ldeal soluLlon for a problem 8log: !""#$%%&'()**)+,-*.#*/((+0-&
Mall: anLonlo.musarra[gmall.com

141111 11
This uocument is issueu with license Cieative Commons Attiibution-NonCommeicial-ShaieAlike


<+=4+,>()9$?
Antonio Nusaiia's Blog. PBP Application on the Clouu. Su August 2u11.
Antonio Nusaiia's Blog. Antonio Nusaiia.
<http:musaiia.woiupiess.com2u11u8Suphp-application-on-the-clouu>.

IBN. Best piactices foi Web seivices veisioning. Su }an 2uu4. IBN
BevelopeiWoiks. Nichael Ellis (msellisca.ibm.com) Kyle Biown
(biownkylus.ibm.com).
<http:www.ibm.comuevelopeiwoikswebseiviceslibiaiyws-veision>.

IC#Coue. ShaipBevelop. 2uuu. ShaipBevelop. IC#Coue.
<http:shaipuevelop.net0penSouiceSB>.

Niciosoft. .NET Fiamewoik. 2u11. Niciosoft NSBN. Niciosoft.
<http:msun.miciosoft.comen-ennetfiamewoik>.

. Piinciples of Seivice Besign: Seivice veisioning . August 2uuS. Niciosoft
NSBN. }ohn Evuemon. <http:msun.miciosoft.comen-
uslibiaiyms9S4726.aspx>.

PBPFog. Rock-soliu Clouu Platfoim foi PBP. August 2u1u. Rock-soliu Clouu
Platfoim foi PBP. Lucas Cailson. <https:www.phpfog.com>.

S0A Woilu Nagazine. Besign Stiategies foi Web Seivices veisioning. S Apiil
2uu4. S0A Woilu Nagazine. Anjali Anagol-Subbaiao Chiis Peltz. <http:soa.sys-
con.comnoue44SS6>.

SugaiCRN Inc. Sugai Community Euition Bocumentation. 2u1u. Sugai
Community Euition Bocumentation. SugaiCRN Inc.
<http:www.sugaicim.comcimsuppoituocumentationSugaiCommunityEui
tion6.1-uocs-Bevelopei_uuiues-Sugai_Bevelopei_uuiue_6.1.u-
Chaptei%2u2%2uApplication%2uFiamewoik.html#9uuu244>.

. Sugai Community Euition Bocumentation. 2u1u. Sugai Community Euition
Bocumentation | SugaiCRN - Commeicial 0pen Souice CRN. SugaiCRN Inc.
<http:www.sugaicim.comcimsuppoituocumentationSugaiCommunityEui
tion>.

. Sugai Community Euition Bocumentation - API Befinition. 2u1u. Sugai
Community Euition Bocumentation. SugaiCRN Inc.
<http:www.sugaicim.comcimsuppoituocumentationSugaiCommunityEui
tion6.1-uocs-Bevelopei_uuiues-Sugai_Bevelopei_uuiue_6.1.u-
Chaptei%2u2%2uApplication%2uFiamewoik.html#9uuuSuS>.

. SugaiCRN - Commeicial 0pen Souice CRN. 2uu4. SugaiCRN - Commeicial
0pen Souice CRN. SugaiCRN Inc. <http:www.sugaicim.comcim>.
!"#$"%$ '()*++*,) -.$/
1he ldeal soluLlon for a problem 8log: !""#$%%&'()**)+,-*.#*/((+0-&
Mall: anLonlo.musarra[gmall.com

141111 12
This uocument is issueu with license Cieative Commons Attiibution-NonCommeicial-ShaieAlike


Wikipeuia. .NET Fiamewoik. 1S }uly 2u11. Wikipeuia - The Fiee Encyclopeuia.
Wikipeuia. <http:en.wikipeuia.oigwiki.NET_Fiamewoik>.