How to connect or login with Odoo using c# code? A

2019-04-12 00:02发布

I have to implement some code in c# for login with Odoo database and give the logged user id please have a look,

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);
}

But, it give error like "Bad Request". Also i'm not sure for XmlRpcUrl, have you any idea about what URL in to [XmlRpcUrl("")] ?

1条回答
小情绪 Triste *
2楼-- · 2019-04-12 00:51

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 (Interface for login)

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

}

3) IOpenErpAddFields.cs (Interface for add new fields)

[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);
}
查看更多
登录 后发表回答