如何使用C#代码连接或登录Odoo?与Odoo连接后,如何从C#向Odoo数据库添加自定义字段?

时间:2018-07-18 14:32:12

标签: c# asp.net odoo xml-rpc odoo-11

我必须在C#中实现一些代码才能使用Odoo数据库登录,并请提供登录的用户ID,

public ActionResult Index()
{
        IOpenErpLogin rpcClientLogin = XmlRpcProxyGen.Create<IOpenErpLogin>();
        int userid = rpcClientLogin.login("DB_name", "User_name", "Password");
        return View();
}

[XmlRpcUrl("https://sample_db_name.dev.odoo.com")]
public interface IOpenErpLogin : IXmlRpcProxy
{
    [XmlRpcMethod("login")]
    int login(string dbName, string dbUser, string dbPwd);
}

但是,它会给出类似“错误请求”的错误。 另外,我不确定XmlRpcUrl是否对[XmlRpcUrl("")]中的URL有任何了解吗?

1 个答案:

答案 0 :(得分:2)

1)Controller.cs

public ActionResult Index()
{
        IOpenErpLogin rpcClientLogin = XmlRpcProxyGen.Create<IOpenErpLogin>();
        rpcClientLogin.NonStandard = XmlRpcNonStandard.AllowStringFaultCode;

        //login
        int userid = rpcClientLogin.login("Db_name", "Db_UserName", "Db_Password");

        //Add Contacts(Customers)
        IOpenErpAddFields rpcField = XmlRpcProxyGen.Create<IOpenErpAddFields>();
        XmlRpcStruct addPairFields = new XmlRpcStruct();
        addPairFields.Add("name", "new_test_Customer1");
        int resAdd = rpcField.create("Db_name", userid, "Db_Password", "res.partner", "create", addPairFields);



        //create new model(table) and return model_id
        //IOpenErpAddFields rpcField = XmlRpcProxyGen.Create<IOpenErpAddFields>();
        XmlRpcStruct addModelparam = new XmlRpcStruct();
        addModelparam.Add("name", "Custom_Model");
        addModelparam.Add("model", "x_customModel_name");
        addModelparam.Add("state", "manual");
        int model_id = rpcField.create("Db_name", userid, "Db_Password", "ir.model", "create", addModelparam);


        //find existing model(table) by name and add field to it
        object[] filter = new object[1];
        filter[0] = new object[3] { "model", "=", "res.partner" };
        int[] model = rpcField.search("Db_name", userid, "Db_Password", "ir.model", "search", filter);

        //create new field(column) to particular model by id and return field_id
        //XmlRpcStruct addPairFields = new XmlRpcStruct();
        addPairFields.Add("model_id", model[0]);//12 for ir.ui.menu
        addPairFields.Add("name", "x_CustomField_name");
        addPairFields.Add("field_description", "Field_label");
        addPairFields.Add("ttype", "char");
        addPairFields.Add("state", "manual");//base
        addPairFields.Add("required", true);
        int resAdd1 = rpcField.create("Db_name", userid, "Db_Password", "ir.model.fields", "create", addPairFields);

        return View();
}

2)IOpenErpLogin.cs(登录界面)

[XmlRpcUrl("http://localhost:8069/xmlrpc/2/common")]
public interface IOpenErpLogin : IXmlRpcProxy
{
    [XmlRpcMethod("login")]
    int login(string dbName, string dbUser, string dbPwd);

}

3)IOpenErpAddFields.cs(用于添加新字段的接口)

[XmlRpcUrl("http://localhost:8069/xmlrpc/2/object")]
public interface IOpenErpAddFields : IXmlRpcProxy
{
    [XmlRpcMethod("execute")]
    int create(String dbName, int userId, string dbPwd, string model, string method, XmlRpcStruct fieldValues);

    [XmlRpcMethod("execute")]
    object[] read(string dbName, int userId, string dbPwd, string model, string method, int[] ids, object[] fields);

    [XmlRpcMethod("execute")]
    int[] search(string dbName, int userId, string dbPwd, string model, string method, object[] filter);
}