Writing .NET clients

Lundalogik provides a helper library called Lundalogik Data Access that simplifies working with Lime CRM Web Service from .NET code. The library is available as a NuGet package from nuget.org.

To connect to an instance of Lime CRM Web Service, update your application .config file to resemble this configuration example:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="lundalogik.dataaccess" type="Lundalogik.DataAccess.Configuration.DataAccessConfiguration, Lundalogik.DataAccess" />
  </configSections>
  <lundalogik.dataaccess>
    <databases>
      <database name="Test" accessMethod="WebService">
        <webServiceOptions binding="WsHttp" address="http://myserver:4057/" />
      </database>
    </databases>
  </lundalogik.dataaccess>
</configuration>

It is also possible to configure a connection entirely through code. Follow this example:

using Lundalogik.DataAccess.Providers;
using Lundalogik.DataAccess;
 
var options = new Lundalogik.DataAccess.Configuration.WebServiceOptions
{
    Address = "http://myserver:8082/",
    Binding = Lundalogik.DataAccess.Configuration.WebServiceBinding.BasicHttp,
    SessionMode = Lundalogik.DataAccess.Configuration.SessionMode.Shared
};
 
var provider = new WebServiceProvider(options);
 
var db = new Database("MyDatabase", provider);

Learning by example is usually the best way of learning how to do things. This section contains a number of examples of how to do things using the library.

Database.Default.LoadDataStructure();
 
foreach (Table table in Database.Default.Tables)
{
    Console.WriteLine("Listing fields in table {0}, local name {1}:", table.Name, table.LocalName);
 
    foreach (Field field in table.Fields)
    {
        Console.WriteLine("\t{0} ({1})", field.Name, field.LocalName);
    }
 
    Console.WriteLine();
}
Databse.Default.LoadDataStructure();
 
Table campaignTable;
 
if (Database.Default.Tables.Lookup(TableLabel.Campaign, out campaignTable))
{
	Console.WriteLine("The table named \"{0}\" has the campaign table label.", 
		campaignTable.Name);
}
else        
{
	Console.WriteLine("No table having the campaign table label is published for web service access.");
}
 
Console.ReadLine();
Query q = new Query("person");
 
q.Conditions.Add("firstname", ConditionOperator.StartsWith, "John");
q.Fields.Add(new string[] { "firstname", "lastname", "email" });
 
// Retrive all records matching the conditions.
Records recsPerson = q.Execute();
 
// Iterate over the result set.
foreach (Record r in recsPerson)
{
    foreach (string fieldName in r.Fields.Names)
    {
        Console.WriteLine(string.Format("{0,-20} {1:40}", fieldName, r[fieldName]));
    }
 
    Console.WriteLine();
}		
// First open existing record then update it.
 
Record recPerson1 = Record.Open("person", 1100, new string[] { "note" });
 
recPerson1["note"] += "\nSome appended information.";		
recPerson1.Update();
 
// Immediate update without fetching the record from the server first.
 
Record recPerson2 = Record.Create("person");
 
recPerson2["note"] = "Testing 1-2-3";
recPerson2.Update(1200);
 
Console.ReadLine();
Record recPerson = Record.Create("person");
 
recPerson["firstname"] = "Amanda";
recPerson["lastname"] = "Panda";
 
recPerson.Update();
 
Console.WriteLine("New record ID: {0:D}", recPerson.Id);
 
Console.ReadLine();
Records recsPerson = new Records("person");
 
Record newPerson;
 
newPerson = recsPerson.Add();
newPerson["firstname"] = "Keyser";
newPerson["lastname"] = "Soze";
 
newPerson = recsPerson.Add();
newPerson["firstname"] = "Bosse";
newPerson["lastname"] = "Badabing";
 
foreach (Record r in recsPerson)
{
	Console.WriteLine("Record id: {0:D}", r.Id);
}
 
recsPerson.Update();
 
foreach (Record r in recsPerson)
{
	Console.WriteLine("Record id: {0:D}", r.Id);
}
 
Console.ReadLine();
int recordId;
 
Console.Write("Please enter a valid company record ID: ");
 
if (int.TryParse(Console.ReadLine(), out recordId))
{
	Record recCompany;
 
	try
	{
		recCompany = Record.Open("company", recordId, new string[] { "name", "phone", "fax" });
 
		Console.WriteLine("Name: {0}\nPhone: {1}\nFax:  {2}",
			recCompany["name"], recCompany["phone"], recCompany["fax"]);				
	}
	catch (ArgumentException)
	{
		Console.WriteLine("Invalid record ID.");				
	}   
}
else
{
	Console.WriteLine("Invalid record ID.");
}
 
Console.ReadLine();
Query q = new Query("person");
 
q.Fields.Add("firstname");
q.Fields.Add("lastname", FieldSortOrder.Ascending, 1);
 
// Retrive the top 5 records.
Records recsPerson = q.Execute(5);
 
// Iterate over the result set.
foreach (Record r in recsPerson)
{
	foreach (string fieldName in q.Fields.Names)
	{
		Console.WriteLine(string.Format("{0,-20} {1:40}", fieldName, r[fieldName]));
	}
 
	Console.WriteLine();
}
// The table is named "document"
Record recDocument = Record.Create("document");
 
recDocument["name"] = "A test file";
recDocument["filename"] = "test.txt";
 
// Add a new document, specifying the name of the document field
recDocument.Document = new Document("document");
 
// Lime gets confused if the extension is not specified
recDocument.Document.Extension = "txt";
 
// Put some UTF8 encoded text data in the file
UTF8Encoding encoding = new UTF8Encoding(true, false);		
recDocument.Document.PutData(encoding.GetBytes("Testing 1-2-3 using som simple text data"));		
 
// Create the record
recDocument.Update();
 
Console.WriteLine("Document record having ID {0:D} created.", recDocument.Id);
 
Console.ReadLine();
var procedure = new StoredProcedure("csp_compareauthcode");
 
procedure.Parameters.Add("@person", 1100);
procedure.Parameters.Add("@authcode", "banan");
procedure.Parameters.Add("@message", "", ParameterDirection.InOut);
 
ProcedureResult result;
 
result = procedure.Execute(false);
 
Console.WriteLine("Return value: {0:D}\n", result.ReturnValue);
 
Console.WriteLine("Output parameters:");
 
foreach (KeyValuePair<string, string> parameter in result.OutputParameters)
{
	Console.WriteLine("Output parameter \"{0}\" has value \"{1}\".", 
		parameter.Key, parameter.Value);
}
 
Console.WriteLine("\nResult set:");
Console.WriteLine(result.Data.ToString());
  • Last modified: 6 years ago
  • (external edit)