JSINFO.bootstrap3.toc = [{"link":"#connecting","title":"Connecting","level":1},{"link":"#using-a-configuration-file","title":"Using a configuration file","level":2},{"link":"#programmatic-access","title":"Programmatic access","level":2},{"link":"#code-examples","title":"Code examples","level":1},{"link":"#working-with-metadata","title":"Working with metadata","level":1},{"link":"#looking-up-a-table","title":"Looking up a table","level":1},{"link":"#executing-a-query","title":"Executing a query","level":1},{"link":"#updating-a-record","title":"Updating a record","level":1},{"link":"#adding-a-record","title":"Adding a record","level":1},{"link":"#adding-multiple-records","title":"Adding multiple records","level":1},{"link":"#opening-a-specific-record","title":"Opening a specific record","level":1},{"link":"#limiting-the-number-of-returned-records","title":"Limiting the number of returned records","level":1},{"link":"#adding-a-document","title":"Adding a document","level":1},{"link":"#executing-a-stored-procedure","title":"Executing a stored procedure","level":1}];
Table of Contents¶
- Connecting
- Code examples
- Working with metadata
- Looking up a table
- Executing a query
- Updating a record
- Adding a record
- Adding multiple records
- Opening a specific record
- Limiting the number of returned records
- Adding a document
- Executing a stored procedure
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.
Connecting¶
Using a configuration file¶
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>
Programmatic access¶
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);
Code examples¶
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.
Working with metadata¶
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();
}
Looking up a table¶
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();
Executing a query¶
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();
}
Updating a record¶
// 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();
Adding a record¶
Record recPerson = Record.Create("person");
recPerson["firstname"] = "Amanda";
recPerson["lastname"] = "Panda";
recPerson.Update();
Console.WriteLine("New record ID: {0:D}", recPerson.Id);
Console.ReadLine();
Adding multiple records¶
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();
Opening a specific record¶
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();
Limiting the number of returned records¶
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();
}
Adding a document¶
// 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();
Executing a stored procedure¶
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: 5 years ago
- (external edit)