0

我们需要创建自己的 C# WebAPI 项目,该项目将连接 Business Central 并将数据插入/更新到 Items 中。搜索后,我们发现使用 Business Central API 可以做到这一点,如下面的链接所示。 https:// docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-develop-connect-apps  

在这里,我们无法理解,

  • 我们应该如何连接到 Business Central?
  • 我们如何调用items API以及参数是什么?
  • 我们如何查看可用 API 的列表?
  • 为什么我们需要创建其他连接应用程序?

如果有人有 C# 代码片段/示例,请与我们分享,这对我们非常有帮助。

或者 是否有任何可用的 SDK 与 CRM SDK 相同?

4

1 回答 1

0

我们应该如何连接到 Business Central?

如果您正在谈论使用代码将其连接到 BC,那么您将需要 Business Central 用户的用户名和 Web 访问密钥,这与 AAD 用户凭据不同。

我们如何调用items API以及参数是什么?

请找到下面的示例代码,它可能会帮助您了解如何调用 API

    string url = "https://api.businesscentral.dynamics.com/v2.0/<<Your Environment Name>>/api/v1.0/companies(<<GUID of the company for which item/items needs to be retrieved/added/updated>>)/items";
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    HttpClient httpClient = new HttpClient();

    String username = "<<UserName>>";
    String password = "<<WebAccessKey>>";
    String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));

    //Get Request for all Items
    HttpRequestMessage readRequest = new HttpRequestMessage(HttpMethod.Get, url);
    readRequest.Headers.Add("Authorization", "Basic " + encoded);
    HttpResponseMessage readResponse = httpClient.SendAsync(readRequest).Result;
    string content = string.Empty;

    using (StreamReader stream = new StreamReader(readResponse.Content.ReadAsStreamAsync().Result))
    {
        content = stream.ReadToEnd();
    }

    //Get Request for a specific Item

    string specificItemURl = url + "(<<Guid of the item to be reteieved>>)";
    HttpRequestMessage readSpecificRequest = new HttpRequestMessage(HttpMethod.Get, specificItemURl);
    readSpecificRequest.Headers.Add("Authorization", "Basic " + encoded);
    HttpResponseMessage readSpecificResponse = httpClient.SendAsync(readSpecificRequest).Result;
    string specificItem = string.Empty;

    using (StreamReader streamSpecificItem = new StreamReader(readSpecificResponse.Content.ReadAsStreamAsync().Result))
    {
        specificItem = streamSpecificItem.ReadToEnd();
    }

    //Create Request
    string jsonObject = "{'number':'18962-S','displayName':'ATHENS Desk 3','type':'Inventory','itemCategoryId':'6d4e5f4d-8ad1-ea11-bb85-000d3a2a9e6e','itemCategoryCode':'TABLE','blocked':false,'baseUnitOfMeasureId':'354f6647-8ad1-ea11-bb85-000d3a2a9e6e','gtin':'','unitPrice':1000.8,'priceIncludesTax':false,'unitCost':780.7,'taxGroupId':'fc4c5f4d-8ad1-ea11-bb85-000d3a2a9e6e','taxGroupCode':'FURNITURE','baseUnitOfMeasure':{'code':'PCS','displayName':'Piece','symbol':null,'unitConversion':null}}";
    HttpRequestMessage createRequest = new HttpRequestMessage(HttpMethod.Post, url);  
    createRequest.Content = new StringContent(jsonObject, Encoding.UTF8, "application/json");
    createRequest.Headers.Add("Authorization", "Basic " + encoded);
    HttpResponseMessage createResponse = httpClient.SendAsync(createRequest).Result;

我们如何查看可用 API 的列表?

您需要使用具有管理角色的用户登录到 Business Central 实例。登录后单击功能区中的服务,然后选择 Web 服务。您应该能够查看所有 api 端点。

希望这对您想要实现的目标有所帮助。

于 2020-08-30T19:00:58.010 回答