Skip to content

Instantly share code, notes, and snippets.

@sugarknowledge
Created September 18, 2012 18:47
Show Gist options
  • Save sugarknowledge/3745001 to your computer and use it in GitHub Desktop.
Save sugarknowledge/3745001 to your computer and use it in GitHub Desktop.
C# Example using SOAP to create or update and account
68c4781f-75d1-223a-5d8f-5058bc4e39ea
using System;
using System.Text;
using System.Security.Cryptography;
using System.Collections.Specialized;
namespace SugarSoap
{
class Program
{
static void Main(string[] args)
{
//login -------------------------------------------------------------------------------------------
string UserName = "admin";
string Password = "password";
string URL = "http://{site_url}/service/v4/soap.php";
//SugarCRM is a web reference added that points to http://{site_url}/service/v4/soap.php?wsdl
SugarCRM.sugarsoap SugarClient = new SugarCRM.sugarsoap();
SugarClient.Timeout = 900000;
SugarClient.Url = URL;
string SessionID = String.Empty;
//Create authentication object
SugarCRM.user_auth UserAuth = new SugarCRM.user_auth();
//Populate credentials
UserAuth.user_name = UserName;
UserAuth.password = getMD5(Password);
//Try to authenticate
SugarCRM.name_value[] LoginList = new SugarCRM.name_value[0];
SugarCRM.entry_value LoginResult = SugarClient.login(UserAuth, "SoapTest", LoginList);
//get session id
SessionID = LoginResult.id;
//create account -----------------------------------------------------------------------------------
NameValueCollection fieldListCollection = new NameValueCollection();
//to update a record, you will nee to pass in a record id as commented below
//fieldListCollection.Add("id", "68c4781f-75d1-223a-5d8f-5058bc4e39ea");
fieldListCollection.Add("name", "Test Account");
//this is just a trick to avoid having to manually specify index values for name_value[]
SugarCRM.name_value[] fieldList = new SugarCRM.name_value[fieldListCollection.Count];
int count = 0;
foreach (string name in fieldListCollection)
{
foreach (string value in fieldListCollection.GetValues(name))
{
SugarCRM.name_value field = new SugarCRM.name_value();
field.name = name; field.value = value;
fieldList[count] = field;
}
count++;
}
try
{
SugarCRM.new_set_entry_result result = SugarClient.set_entry(SessionID, "Accounts", fieldList);
string RecordID = result.id;
//show record id to user
Console.WriteLine(RecordID);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.Source);
}
//Pause Window
Console.ReadLine();
}
static private string getMD5(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();
}
}
}
@zeonz
Copy link

zeonz commented Sep 20, 2012

Hi, do you have any example for retrieve data from sugarcrm using set_entry or set_entry_list? Thank you

@zeonz
Copy link

zeonz commented Sep 20, 2012

sorry i mean get_entry or get_entry_list. Thank you

@wardluke
Copy link

Hi, I don't understand the purpose of the inner foreach here, count is incremented outside of the loop, so all the values are assigned to the same fieldList item?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment