Sugar CRM 6.5.14 -使用c#调用REST API

本文关键字:调用 REST API 使用 CRM Sugar | 更新日期: 2023-09-27 18:06:27

rest api的doco少得惊人。谁能给我一个使用c#的Sugar CRM REST调用的工作示例?

我试过SugarSharp,但它只是列出服务,没有显示Data(null)

Sugar CRM 6.5.14 -使用c#调用REST API

感谢这是一个古老的问题,但对于任何遇到它的人来说,我花了一段时间才能完成所有工作,其中创建和更新关系是最难理解的。

这是我围绕v4_1 rest api编写的包装器类的一部分,希望它能有所帮助:-

public void Login()
    {
        object loginData = new
        {
            user_auth = new
            {
                user_name = Username,
                password = CalculateMD5Hash(Password)
            }
        };
        string jsonData = CreateFormattedPostRequest("login", loginData);
        var request = GetRestRequest(jsonData, "POST");
        var loginResponse = GetRestResponseByType<LoginResponse>(request);
        if (string.IsNullOrEmpty(loginResponse.id))
        {
            throw new SugarException(string.Concat("Authorisation Failed for user: {0}, did not retrieve access token", Username));
        }
        SessionID = loginResponse.id;
    }

格式化请求:-

        private string CreateFormattedPostRequest(string method, object data)
    {
        StringBuilder buffer = new StringBuilder();
        using (StringWriter writer = new StringWriter(buffer))
        {
            serializer.Serialize(data, buffer);
        }
        string result = "method=" + method;
        result += "&input_type=JSON&response_type=JSON&rest_data=" + buffer.ToString();
        return result;
    }
最后得到响应:-
private object GetRestResponseAsObject(HttpWebRequest request)
    {
        using (var response = (HttpWebResponse)request.GetResponse())
        {
            using (Stream input = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(input);
                string buffer = reader.ReadToEnd();
                var responseObj = serializer.DeserializeObject(buffer);
                return responseObj;
            }
        }
    }

下面是一个调用set_entry方法的例子:

        /// <summary>
    /// Creates or Updates a single bean, for update ensure that the name_value_list contains the ID of the record
    /// name_value_lists - Dictionary where the keys of the are the SugarBean attributes (columns), the values of the array are the values the attributes should have.
    /// </summary>
    /// <param name="module">Module to update i.e Account</param>
    /// <param name="record">key value pair object of record, include ID for update</param>
    /// <returns>Returns the updated or created Bean ID</returns>
    public string CreateUpdateBean(string module, object record)
    {
        var parameters = new Dictionary<string, object>();
        parameters.Add("session", SessionID);
        parameters.Add("module_name", module);
        parameters.Add("name_value_list", record);
        parameters.Add("track_view", false);
        string jsonData = CreateFormattedPostRequest("set_entry", parameters);
        var request = GetRestRequest(jsonData, "POST");
        var result = GetRestResponseByType<object>(request);
        return result.ToString();
    }

要用c#调用rest api SugarCRM/SuiteCRM,可以使用SugarRestSharp

创建Account示例:

var client = new SugarRestClient(TestAccount.Url, TestAccount.Username, TestAccount.Password);
Account insertAccount = AccountsModule.GetTestAccount();
// -------------------Create Account-------------------
SugarRestResponse response = AccountsModule.CreateAccount(client, insertAccount);
Assert.NotNull(response);
Assert.Equal(response.StatusCode, HttpStatusCode.OK);
string insertId = (response.Data == null) ? string.Empty : response.Data.ToString();
Assert.NotNull(insertId);
Assert.NotEmpty(insertId);
// -------------------End Create Account-------------------