Click here to Skip to main content
14,494,237 members
Rate this:
Please Sign up or sign in to vote.
See more:
LS,

In D365BC I created a simple table, card page and list page. You can create a list of workers entering: first name, last name and function.
I turned the card page into a web service and now there is a ODATA V3, ODATA V4 and SOAP web service available.

In C# VS2019 I created a program to see if I can delete a record using these web services.

When I use the SOAP webservice, there is no problem. It deletes the selected record.

But when I use the ODATA web service I get error 404 not found.

What I have tried:

When I use this code for the the SOAP webservice, there is no problem. It deletes the selected record.

private void button4_Click(object sender, EventArgs e)
{//delete
    string _wsURL = "https://api.businesscentral.dynamics.com/v2.0/SomeFunkyGUID/Sandbox/WS/CRONUS%20NL/Page/WorkersWS";
    string _userName = "UserName";
    string _wsKey = "Password";

    //Create an instance of the D365BC SOAP WS
    BasicHttpBinding _binding = new BasicHttpBinding();

    //Set https usage
    _binding.Security.Mode = BasicHttpSecurityMode.Transport;
    _binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;

    WorkersWS_PortClient _ws = new WorkersWS_PortClient(_binding, new EndpointAddress(_wsURL));
    _ws.ClientCredentials.UserName.UserName = _userName;
    _ws.ClientCredentials.UserName.Password = _wsKey;

    try
    {
        _ws.Delete(rtbE_Tag.Text); //this textbox holds this: W/ABC12345 nr which is like a primary key
    }
    catch (Exception _ex)
    {

    }
}


But now I do almost the same, except that I now use the ODATA web service (PS there is no difference if I use the ODATA v3 or v4 web service, they both give error 404 not found):
private async void button6_Click(object sender, EventArgs e)
{
    try
    {
        string _url = "https://api.businesscentral.dynamics.com/v2.0/SomeFunkyGuid/Sandbox/ODataV4/Company('CRONUS%20NL')/WorkersWS";//List Page
        string _userName = "UserName";
        string _wsKey = "Password";
        byte[] _authenticationParameter = Encoding.UTF8.GetBytes(_userName + ":" + _wsKey);
        _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(_authenticationParameter));
        HttpResponseMessage response = await _client.DeleteAsync(_url + "/" + rtbE_Tag.Text);//this textbox holds this: W/ABC12345 nr which is like a primary key
        response.EnsureSuccessStatusCode();
    }
    catch (Exception ex)
    {

    }
}


When I use the code to try using the ODATA web service the code breaks at: response.EnsureSuccessStatusCode();

The error is: Statuscode no success, 404 (not found)

Perhaps I am passing the record that is to be deleted in a wrong way?

I hope someone can help me.


Kind regards,


Clemens Linders
Posted
Updated 26-Mar-20 2:12am
Rate this:
Please Sign up or sign in to vote.

Solution 1

The 404 message tells you that the page was not found, so you need to check the URL. If it is correct then you will need to debug the server code to see why it raises that error.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

HI Richard,

Thanks for your input. But I received the solution from Gert.

For deletion I do not have to pass the E-tag, but I can pass the primary key. Although using the Soap web service I do pass the E-tag.

It works when I changed my code into:

HttpResponseMessage response = await _client.DeleteAsync(_url + "(No='1002')") ;

1002 is the value of the primary key field No.
In D365bc there is a point after the name field No

For other D365bc users I want to show that you do not use that point in C#.

Kind regards,


Clemens Linders
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100