Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# D365 Business Central,我们需要使用C代码将记录创建到项目中_C#_Dynamics 365_Dynamics Business Central - Fatal编程技术网

C# D365 Business Central,我们需要使用C代码将记录创建到项目中

C# D365 Business Central,我们需要使用C代码将记录创建到项目中,c#,dynamics-365,dynamics-business-central,C#,Dynamics 365,Dynamics Business Central,我们需要创建自己的C#WebAPI项目,该项目将连接Business Central并将数据插入/更新到项目中。在执行搜索后,我们发现,使用下面链接中给出的业务中心API可以实现这一点。https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-develop-connect-apps 在这里,我们无法理解 我们应该如何连接到商务中心 我们如何调用items API以及参数是什

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

在这里,我们无法理解

  • 我们应该如何连接到商务中心
  • 我们如何调用items API以及参数是什么
  • 我们如何查看可用API的列表
  • 为什么我们需要创建其他连接应用程序
如果有人有C#代码片段/示例,请与我们分享,它将对我们非常有帮助


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

我们应该如何连接到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;
stringurl=”https://api.businesscentral.dynamics.com/v2.0//api/v1.0/companies()/项目”;
HttpWebRequest请求=(HttpWebRequest)WebRequest.Create(url);
HttpClient HttpClient=新HttpClient();
字符串username=“”;
字符串密码=”;
String encoded=System.Convert.ToBase64String(System.Text.Encoding.GetEncoding(“ISO-8859-1”).GetBytes(用户名+”:“+密码));
//获取所有项目的请求
HttpRequestMessage readRequest=新的HttpRequestMessage(HttpMethod.Get,url);
readRequest.Headers.Add(“授权”、“基本”+编码);
HttpResponseMessageReadResponse=httpClient.SendAsync(readRequest).Result;
string content=string.Empty;
使用(StreamReader stream=newstreamreader(readResponse.Content.ReadAsStreamAsync().Result))
{
content=stream.ReadToEnd();
}
//获取特定项目的请求
字符串specificItemURl=url+“()”;
HttpRequestMessage readSpecificRequest=新的HttpRequestMessage(HttpMethod.Get,specificItemURl);
readSpecificRequest.Headers.Add(“授权”、“基本”+编码);
HttpResponseMessage readSpecificResponse=httpClient.SendAsync(readSpecificRequest).Result;
string specificItem=string.Empty;
使用(StreamReader streamSpecificItem=new StreamReader(readSpecificResponse.Content.ReadAsStreamAsync().Result))
{
specificItem=streamSpecificItem.ReadToEnd();
}
//创建请求
字符串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','gtin':'unitPrice','1000.8,'PriceIncludestaxedAX':false,'unitCost','780.7,'Taxad8F4C4A9E4C'e','taxGroupCode':'FURNITURE','baseUnitOfMeasure':{'code':'PCS','displayName':'Piece','symbol':null,'unitConversion':null};
HttpRequestMessage createRequest=新的HttpRequestMessage(HttpMethod.Post,url);
createRequest.Content=newStringContent(jsonObject,Encoding.UTF8,“application/json”);
createRequest.Headers.Add(“授权”、“基本”+编码);
HttpResponseMessageCreateResponse=httpClient.SendAsync(createRequest).Result;
我们如何查看可用API的列表

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


希望这能帮助您实现目标。

谢谢Shivkant,实际上这段代码给出了一个错误“底层连接已关闭:发送时发生意外错误。”。您能帮我解决这个问题吗?WEB API URL配置为https,需要TLS 1.2。下面是您应该在计算机中检查的注册表项,如果不存在,请您创建它们。[HKEY_LOCAL_machine\SOFTWARE\Microsoft\.NETFramework\v2.0.50727]“SystemDefaultTlsVersions”=dword:0000000 1“SchUseStrongCrypto”=dword:00000001[HKEY\U LOCAL\U MACHINE\SOFTWARE\Microsoft\.NETFramework\v4.0.30319]“SystemDefaultTlsVersions”=dword:00000001“SchUseStrongCrypto”"=dword:0000000 1这类似于,我们已经创建了一个WebAPI项目,并将在Azure上上载,因此我们无法使用此注册表更改解决方案。在创建HttpClient System.Net.ServicePointManager.SecurityProtocol |=SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12之前,您可以尝试此语句吗?是的,谢谢您的帮助回复,现在错误已经改变了,