This is the guide step by step to perform operations ( select, insert, update and delete) with C# to Dynamics 365 Business Central

  • You need to publish one page or more, in the web services page of BC, to know the id of the page, press CTRL +ALT + F1 You can see the number of the page 30 in this case:

the page is  Item Card, then go to Web Services and publish it:You can see the URL of the services ODATA and SOAP, maybe you have to enable the services and check the firewall of the server to see if ports are allowed, in this case 7048 and 7047on the Shell You have to restart services:

on the explorer, You can see odata information.In my case I was to change the name of the computer with localhost to work , ( with the name of the computer not working , I don’t know exactly why but seems sql spn registration)

Before use Visual Studio , You need to download the $metadata from the url and save on a location on the computer to import then from Visual Studio

Inside a new project of Visual Studio You have to add odata connected service extension:

after restart the system you can configure a connected service:

select odata connected service:

Import the $metadata file downloaded before:

Select only the necessary pages and codeunits(Functions):

on the project create  a static class to make the connection:

internal static class OdataV4
{
public static NAV.NAV serveiodatav4;
public static void inicializa()
{
string url = "http://localhost:7048/BC230/ODataV4/";

NetworkCredential TokenCredential = CredentialCache.DefaultNetworkCredentials;

serveiodatav4 = new NAV.NAV(new Uri(url));
serveiodatav4.MergeOption = MergeOption.OverwriteChanges; // Objects are always loaded from persisted storage (Business Central)
serveiodatav4.Timeout = 15; // seconds
serveiodatav4.BuildingRequest += Nav_BuildingRequest2;
serveiodatav4.Credentials = TokenCredential;
//------------------------------------

}
private static void Nav_BuildingRequest2(object sender, Microsoft.OData.Client.BuildingRequestEventArgs e)
{
//e.RequestUri = new System.Uri(e.RequestUri + "?company='CRONUS%20España%20S.A.'");
e.Headers.Add("company", "TEST");

}

}

on the code you can see that is very easy to define the odata url and add a BuildingRequest to specify Company Name, before to make a query or call to a function.

For example basic read operation with option 1 ( linq filter):

private List<NAV.Item> items;
items = OdataV4.serveiodatav4.Item.GetAllPages().Where(x => x.No.Contains(txtitem.Text)).ToList();
dataGridView1.DataSource = items;

basic read operation with option 2 ( AddQueryOption filter):

private List<NAV.Item> items;
items = OdataV4.serveiodatav4.Item.AddQueryOption("$filter", "No eq '*" + txtitem.Text + "*'").GetAllPages().ToList();
dataGridView1.DataSource = items;

this two read operations are the same as speed and no difference of the results:

Basic Update operation:

</pre>
NAV.Item x = items.Where(x => x.No == txtcodeupdate.Text).First();
if (x != null)
{
x.Description = txtdescupdate.Text;
OdataV4.serveiodatav4.UpdateObject(x);
OdataV4.serveiodatav4.SaveChanges();

}
<pre>

Make sure that the object has the last version, because if you modify it on BC, you can not update, is recommended to read the object just before update it.

basic Insert operation ( I was try to do on the items table but is very difficult, when I was trying with another table is very easy):

 NAV.Recurso x = new NAV.Recurso();
x.No = txtcodeadd.Text;
x.Name = txtdescadd.Text;
OdataV4.serveiodatav4.AddToRecurso(x);
OdataV4.serveiodatav4.SaveChanges();

Basic delete operation:

NAV.Item x = items.Where(x => x.No == txtcodedelete.Text).First();
if (x != null)
{
OdataV4.serveiodatav4.DeleteObject(x);
OdataV4.serveiodatav4.SaveChanges();
}

 

As you can see is very easy to perform basic operations with Business Central with a few lines of code.

I hope that this post can help someone like me.


 

 

Facebooktwitterredditpinterestlinkedinmail