PortaCapena.OdooJsonRpcClient
1.0.21
dotnet add package PortaCapena.OdooJsonRpcClient --version 1.0.21
NuGet\Install-Package PortaCapena.OdooJsonRpcClient -Version 1.0.21
<PackageReference Include="PortaCapena.OdooJsonRpcClient" Version="1.0.21" />
<PackageVersion Include="PortaCapena.OdooJsonRpcClient" Version="1.0.21" />
<PackageReference Include="PortaCapena.OdooJsonRpcClient" />
paket add PortaCapena.OdooJsonRpcClient --version 1.0.21
#r "nuget: PortaCapena.OdooJsonRpcClient, 1.0.21"
#:package PortaCapena.OdooJsonRpcClient@1.0.21
#addin nuget:?package=PortaCapena.OdooJsonRpcClient&version=1.0.21
#tool nuget:?package=PortaCapena.OdooJsonRpcClient&version=1.0.21
OdooJsonRpcClient
OdooJsonRpcClient is a C# library (.NET Standard) for communication with Odoo.
Installation
Use the package manager console, nuget package manager or check on NuGet.org
Install-Package PortaCapena.OdooJsonRpcClient
First steps
Start your work with check version of Odoo. To this request You need only a valid url address.
var config = new OdooConfig(
apiUrl: "https://odoo-api-url.com", // "http://localhost:8069"
dbName: "odoo-db-name",
userName: "admin",
password: "admin"
);
var odooClient = new OdooClient(config);
var versionResult = await odooClient.GetVersionAsync();
When U can connect to odoo, try use login method. (There is no need to use this method later. Logging is going on in the background e.g. OdooClient, OdooRepository)
var loginResult = await odooClient.LoginAsync();
If You have correct data to login, its time to get model that U interested off.
var tableName = "product.product";
var modelResult = await odooClient.GetModelAsync(tableName);
var model = OdooModelMapper.GetDotNetModel(tableName, modelResult.Value);
Method GetModelAsync returns model with odoo specification.
Method GetDotNetModel() from class OdooModelMapper returns string of class declaration that U can create and paste to Your project. Please don't use the models from the example project. Models are different depending on odoo version and added extensions.
[OdooTableName("product.product")]
[JsonConverter(typeof(OdooModelConverter))]
public class OdooProductProduct : IOdooModel
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("display_name")]
public string DisplayName { get; set; }
[JsonProperty("price")]
public double? Price { get; set; }
// product.template
[JsonProperty("product_tmpl_id")]
public int ProductTmplId { get; set; }
[JsonProperty("activity_exception_decoration")]
public ActivityExceptionDecorationOdooEnum? ActivityExceptionDecoration { get; set; }
...
}
[JsonConverter(typeof(StringEnumConverter))]
public enum ActivityExceptionDecorationOdooEnum
{
[EnumMember(Value = "warning")]
Alert = 1,
[EnumMember(Value = "danger")]
Error = 2,
}
JSON API (Odoo 19+)
Odoo 19+ has a new external API ("JSON-2"); JSON-RPC used by default will be removed in Odoo 22.
Pass OdooApiProtocol.JsonApi and use an API key as password - everything else works the same.
var config = new OdooConfig(
apiUrl: "https://odoo-api-url.com",
dbName: "odoo-db-name",
userName: "admin",
password: "<API key>",
protocol: OdooApiProtocol.JsonApi
);
OdooRepository
Read
var repository = new OdooRepository<ProductProductOdooModel>(config);
var products = await repository.Query().ToListAsync();
In Repository U can use OdooQueryBuilder to create queries.
var products = await repository.Query()
.Where(x => x.Name, OdooOperator.EqualsTo, "test product name")
.Where(x => x.WriteDate, OdooOperator.GreaterThanOrEqualTo, new DateTime(2020, 12, 2))
.Select(x => new
{
x.Name,
x.Description,
x.WriteDate
})
.OrderByDescending(x => x.Id)
.Take(10)
.ToListAsync();
Create
var model = OdooDictionaryModel.Create(() => new ProductProductOdooModel()
{
Name = "test product name"
});
var result = await repository.CreateAsync(model);
Update
U can update only this fields that U are interested in. For updating many records use UpdateRangeAsync.
var model = OdooDictionaryModel.Create(() => new ProductProductOdooModel()
{
Name = "test product name updated"
});
var result = await repository.UpdateAsync(model, productId);
Delete
var deleteProductResult = await repository.DeleteAsync(productId);
Action
Perform any actions on a model. For example use the code below to confirm a sale order and turn it from a quote to an order.
var repository = new OdooRepository<SaleOrderOdooModel>(config);
var confirmResult = await repository.ActionAsync("action_confirm", orderId);
OdooClient
OdooRepository is a wrapper for OdooClient. This class give option for building more advanced requests or not implemented in this library.
IOdooCreateModel
If we create an object instance that we want to pass to odoo but do not fill all fields, they will be automatically assigned default values (Create and update methods). In this case is impossible to distinguish whether we want to set null value or not touch this property. The first solution for that is create models based on odoo model with only fields that we want to use. To that use IOdooCreateModel interface. To not create multiple models use OdooDictionaryModel.
[OdooTableName("product.product")]
public class OdooCreateProduct : IOdooCreateModel
{
[JsonProperty("name")]
public string Name { get; set; }
}
var model = new OdooCreateProduct()
{
Name = "Prod test Kg",
};
var createResult = await repository.CreateAsync(model);
OdooDictionaryModel
This class is second solution for the problem of passing models with default or null values to odoo.
var model = OdooDictionaryModel.Create(() => new ProductProductOdooModel()
{
Name = "test name",
Description = "test description",
});
if(condition)
model.Add(x => x.WriteDate, new DateTime());
var createResult = await odooRepository.CreateAsync(model);
or
var model2 = new OdooDictionaryModel("res.partner") {
{ "name", "test name" },
{ "country_id", 20 },
{ "city", "test city" },
{ "zip", "12345" },
{ "street", "test address" },
{ "company_type", "company" },
};
var odooClient = new OdooClient(TestConfig);
var createResult = await odooClient.CreateAsync(model2);
OdooContext
Context is mainly used to send to odoo values such as language or time zone, but U can use it for more advanced requests. U can set it in OdooConfig for multiple usage or only once adding it to request as parameter (also in repository query).
var context = new OdooContext("pl_PL");
context.Language = "nl_BE";
var id = await odooRepository.CreateAsync(model, context);
Advanced queries
To make Your queries faster use Select method and get only fields that U are interested of. If U reduced the amount of field in your models use SelectSimplifiedModel().
OdooFilter
For more advanced queries U can use OdooFilter or OdooFilterOfT. To use or try:
var filter = OdooFilter<ResCompanyOdooModel>.Create()
.Or()
.EqualTo(x => x.Name, "My Company (San Francisco)")
.EqualTo(x => x.Name, "PL Company");
var filter = OdooFilter.Create()
.Or()
.EqualTo("name", "My Company (San Francisco)")
.EqualTo("name", "PL Company");
var products = await repository.Query()
.Where(filter)
.ToListAsync();
Deep where
Get ProductProductOdooModel where CountryCode is "BE" in ResCompanyOdooModel (CompanyId)
var repository = new OdooRepository<ProductProductOdooModel>(config);
var products = await repository.Query()
.Where<ResCompanyOdooModel>(
x => x.CompanyId,
y => y.CountryCode, OdooOperator.EqualsTo, "BE")
.FirstOrDefaultAsync();
var products = await repository.Query()
.Where<ResCompanyOdooModel, AccountTaxOdooModel>(
x => x.PropertyAccountExpenseId,
y => y.AccountSaleTaxId,
z => z.CountryCode, OdooOperator.EqualsTo, "BE")
.FirstOrDefaultAsync();
Odoo Request and Result models examples
Request
{
"id":948165322,
"jsonrpc":"2.0",
"method":"call",
"params":{
"service":"object",
"method":"execute",
"args":[
"odoo_db_name",
2,
"odoo_user_name",
"product.product",
"search_read",
[
[
"name",
"=",
"Bioboxen 610l"
],
[
"write_date",
">=",
"2020-12-02 00:00:00"
]
],
[
"name",
"description",
"write_date"
]
]
}
}
Result
{
"jsonrpc":"2.0",
"id":948165322,
"result":[
{
"id":324,
"name":"Bioboxen 610l",
"description":false,
"write_date":"2020-12-02 08:47:08"
}
]
}
Invoice PDF
var wizardRepository = new OdooRepository<AccountMoveSendWizardOdooModel>(config);
var wizardId = await wizardRepository.CreateAsync(OdooDictionaryModel.Create(() => new AccountMoveSendWizardOdooModel()
{
MoveId = invoiceId,
SendingMethods = new string[0]
}));
await wizardRepository.ActionAsync("action_send_and_print", wizardId.Value);
var invoiceRepository = new OdooRepository<AccountMoveOdooModel>(config);
var invoice = await invoiceRepository.Query().ById(invoiceId).FirstAsync();
var pdf = Convert.FromBase64String(invoice.Value.InvoicePdfReportFile);
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
Running integration tests locally
Integration tests in PortaCapena.OdooJsonRpcClient.Example need a running Odoo instance. The docker folder contains one self-contained setup per Odoo version - each has its own docker-compose.yml, seed.py with test data and database, and they can run at the same time:
| Version | Folder | Url | Database | Login | Used for |
|---|---|---|---|---|---|
| Odoo 15 | docker/odoo15 |
http://localhost:8069 | db-test |
admin / admin |
JSON-RPC tests |
| Odoo 19 | docker/odoo19 |
http://localhost:8070 | db-test-19 |
admin / admin, API key in seed.py |
JSON-2 API tests |
docker compose -f docker/odoo15/docker-compose.yml up -d # first start initializes the database (a few minutes)
dotnet test PortaCapena.OdooJsonRpcClient.Example
docker compose -f docker/odoo15/docker-compose.yml down -v # stop and remove all data
On first start the database is created with demo data, modules and extra test data from the version's seed.py (Odoo 15: accounting, sales, purchase, inventory, coupons, CRM, project, HR, MRP, languages en_US, nl_NL, pl_PL, companies, partners, lot-tracked products, orders, invoice; Odoo 19: sales, purchase, inventory, partners and a fixed API key for admin). To change modules or seed data, edit the version's docker-compose.yml / seed.py and recreate it with down -v and up -d.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Newtonsoft.Json (>= 13.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated | |
|---|---|---|---|
| 1.0.21 | 230 | 11/5/2026 | |
| 1.0.20 | 167,187 | 4/4/2023 | |
| 1.0.19 | 5,253 | 11/23/2022 | |
| 1.0.18 | 49,849 | 1/14/2022 | |
| 1.0.17 | 801 | 11/8/2021 | |
| 1.0.16 | 26,765 | 8/6/2021 | |
| 1.0.15 | 1,718 | 6/10/2021 | |
| 1.0.14 | 757 | 5/21/2021 | |
| 1.0.13 | 831 | 5/21/2021 | |
| 1.0.12 | 637 | 5/17/2021 | |
| 1.0.11 | 1,323 | 4/26/2021 | |
| 1.0.10 | 1,667 | 2/8/2021 | |
| 1.0.9 | 752 | 2/4/2021 | |
| 1.0.8 | 617 | 2/4/2021 | |
| 1.0.7 | 610 | 2/4/2021 | |
| 1.0.6 | 615 | 2/4/2021 | |
| 1.0.5 | 691 | 2/1/2021 | |
| 1.0.4 | 749 | 12/17/2020 | |
| 1.0.3 | 712 | 12/9/2020 | |
| 1.0.2 | 713 | 12/8/2020 |