web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

What is the structure in C# to add records to a BC Cloud table via the OData v4 web service?

(0) ShareShare
ReportReport
Posted on by 99

I have created an application in Azure and I have given it read and write permissions in BC, in BC I have published the page as a web service. So far I only have the C# code to display the records. The requirement is to monitor a specific folder in Windows and whenever a text file is added, the content of that text file needs to be loaded into the BC table.

Thanks in advance.

I have the same question (0)
  • Suggested answer
    YUN ZHU Profile Picture
    95,597 Super User 2025 Season 2 on at

    Hi, Here are two examples that hopefully give you some hints.

    https://demiliani.com/2020/03/30/dynamics-365-business-central-saas-save-a-file-to-an-sftp-server/

    https://github.com/microsoft/AL/issues/5617

    Thanks.

    ZHU

  • isabtogumon Profile Picture
    99 on at

    Hi, yzhums 

    I appreciate your answer, however, apparently I did not explain what to do with the text file. So I put it in context a little better.

    Once it has been identified which file(s) have been added to the folder, the application must access that file(s) to read them and then add the content to the BC table.

    Currently I have this code, which already detects the new files in the folder and accesses them, I have also communicated to the BC object

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO;
    using System.Net.Http;
    using System.Web;
    using Newtonsoft.Json.Linq;
    using System.Xml.Linq;
    
    namespace MonitorChanges
    {
    
        public partial class Form1 : Form
        {
            string Path = @"D:\OneDrive - ...", Ruta;
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                fileSystemWatcher1.Path = Path;
                GetFiles();
            }
    
            public void GetFiles() 
            {
                string[] lst = Directory.GetFiles(Path);
                textBox1.Text = "";
                foreach(var sFile in lst) 
                {
                    textBox1.Text  = sFile   Environment.NewLine;
                    Ruta = sFile;
                }
             }
    
            private void fileSystemWatcher1_Changed(object sender, FileSystemEventArgs e)
            {
    
            }
    
            private async void fileSystemWatcher1_Created(object sender, FileSystemEventArgs e)
            {
                GetFiles();
                await GetToken();
            }
         
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
    
            }
    
            static async Task GetToken()
            {
    
                string ClientId = "2825017b...";
                string ClientSecret = "cGO8Q~...";
                string TenantId = "36393c6b...";
    
                string URL = "https://login.microsoftonline.com/"   TenantId   "/oauth2/v2.0/token";
    
                HttpClient client = new HttpClient();
                var content = new StringContent("grant_type = client_credentials"  
                                                "&scope=https://api.businesscentral.dynamics.com/.default"  
                                                "&client_id="   HttpUtility.UrlEncode(ClientId)  
                                                "&client_secret="   HttpUtility.UrlEncode(ClientSecret));
    
                content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded");
                var response = await client.PostAsync(URL, content);
                if (response.IsSuccessStatusCode)
                {
                    JObject Result = JObject.Parse(await response.Content.ReadAsStringAsync());
                    string BearerToken = Result["access_token"].ToString();
    
                    string URL2 = "https://api.businesscentral.dynamics.com/v2.0/36393c6b.../Production/ODataV4/Company('CRONUS MX')/VistaClientes";
                    HttpClient testclient = new HttpClient();
                    testclient.DefaultRequestHeaders.Add("Authorization", "Bearer "   BearerToken);
                    var result = await testclient.GetAsync(URL2);
                    if (result.IsSuccessStatusCode)
                    {
                        MessageBox.Show(await result.Content.ReadAsStringAsync());
                        List records = new List();
                        List rows = File.ReadAllLines(@"D:\OneDrive...\Test.txt").ToList();
                        foreach (var row in rows)
                        {
                            string[] entries = row.Split(',');
    
                            Record newRecord = new Record();
    
                            newRecord.Name = entries[0];
                            newRecord.Description = entries[1];
                            newRecord.Document = entries[2];
    
                            records.Add(newRecord);
                        }
                        foreach (var record in records)
                        {
                            Console.WriteLine($"{record.Name} {record.Description}: {record.Document}");
                        }
                    }
                    else
                    {
                        MessageBox.Show(result.StatusCode.ToString());
                    }
                }
                else
                {
                    MessageBox.Show(response.StatusCode.ToString());
                }       
            }
        }
    }
    

    Regards

  • Suggested answer
    DAnny3211 Profile Picture
    11,397 on at

    hi

    i did not understand what is not working

    you can read the files I understand with C# and you can also point to BC's web services.

    what can't you do? enter the values in BC?

    I don't know if this will help you

    DAniele

  • isabtogumon Profile Picture
    99 on at

    Hi, I have little experience with C#, so I ask for some tip or example to insert the values in BC

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,785

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 1,007 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 948 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans