Inkey Solutions
Inkey Solutions

Reputation: 117

D365 Business Central, we need to create records into Items using C# code

We need a requirement to create our own C# WebAPI project that will connect Business Central and inserts/updates data into Items.After doing a search, we found that this is possible using Business Central APIs like given in the link below.https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-develop-connect-apps  

Here, we are not able to understand,

If somebody has the C# code snippet/example please share with us, it will be really helpful for us.

OR Are there any SDKs available same as like CRM SDKs?

Upvotes: 0

Views: 1245

Answers (1)

Shivkant Soni
Shivkant Soni

Reputation: 61

How should we connect to Business Central?

If you are talking about connecting it to BC using code then you will need the Username and Web Access key of the Business Central user, which is different from the AAD user credentials.

How can we call items API and what the parameters are?

Please find the below sample code which might help you to understand how to call APIs

    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;

How can we see list of available APIs?

You will need to login into the Business Central instance with a user who has Administration role. After logging in Click on Services from the ribbon and then select Web Services. You should able to view all the api endpoints.

Hope this helps you in what you are trying to acheive.

Upvotes: 0

Related Questions