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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

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

Cannot delete record using ODATA web service

(0) ShareShare
ReportReport
Posted on by 70

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 availabale.

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.

        private void button4_Click(object sender, EventArgs e)

        {//delete

            string _wsURL = "">api.businesscentral.dynamics.com/.../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):

        private async void button6_Click(object sender, EventArgs e)

        {

            try

            {

                string _url = "">api.businesscentral.dynamics.com/.../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

  • Suggested answer
    gert@lynge.org Profile Picture
    18 on at

    Hi

    I don't think the eTag can be used instead of the primary key. It is more like a version of the record and ensures that it has not been modified by other since you read it.

    At least I'm usually sending the primary key as parameters to the DELETE call and then include the eTag in a If-Modify header check.

    If you are able to read PowerShell, I have working examples of the OData calls on an old blog here (unfortunately the blog is in Danish, but the powershell code is english): https://scblog.lynge.org/?p=1105

  • Clemens Linders Profile Picture
    70 on at

    Hi Gert,

    Thanks for your reply.

    Trying to pass the primary record is one of the first things I tried.

    If I change the code to: HttpResponseMessage response = await _client.DeleteAsync(_url + "/1002");  this unfortunately makes no difference the error remains 404 not found.

    You say that for deletion you do not pass the E-Tag but the primary record. Yet when I look at your sample I think you pass the E-tag???

    function Remove-NAVODataRecord()

    {

       [CmdletBinding()]

       param (

           [parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)]

               [String]$NAVUri,

           [parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)]

               [String]$NAVWebService,

           [parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)]

               [String]$NAVSearchParm,

           [parameter(ValueFromPipelineByPropertyName=$true)]

               [PSCredential]$NAVCredentials )

       PROCESS

       {

           try {

               $ETag = (Get-NAVODataRecord -NAVUri $NAVUri -NAVWebService $NAVWebService -NAVSearchParm $NAVSearchParm -NAVCredentials $NAVCredentials).ETag

               if(-not [string]::IsNullOrEmpty($ETag)) {

                   if([string]::IsNullOrEmpty($NAVCredentials)) {

                       $NAVResult = Invoke-RestMethod -Uri "$NAVUri$NavWebService($NavSearchParm)?`$format=json" -Method Delete -UseDefaultCredentials `

                                                      -ContentType "application/json" -Headers $(New-NAVODataRestMethodHeader -ETag $ETag)

                   }

                   else {

                       $NAVResult = Invoke-RestMethod -Uri "$NAVUri$NavWebService($NavSearchParm)?`$format=json" -Method Delete -Credential $NAVCredentials `

                                                      -ContentType "application/json" -Headers $(New-NAVODataRestMethodHeader -ETag $ETag)

                   }

                   return $true

               }

               else {

                   return $false

               }

           }

           Catch {

               write-host ($_ |Convertfrom-json|select -ExpandProperty "odata.error").message.value -ForegroundColor Red

               return $false

           }

       }

    }

    I still hope you have a solution for me.

    Kind regards,

    Clemens Linders

  • Verified answer
    gert@lynge.org Profile Picture
    18 on at

    Hi Clemens

    I think the correct syntax for the URL is ...<webservicename>(<field>=<value>)

    Like ...Customer(No='11111') in my example

    I don't use eTag in the URL - it is only used as a "If-Match" header.

  • Clemens Linders Profile Picture
    70 on at

    Hi Gert,

    I changed my code into:

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

    And now the record where No. (Primary key) = 1002 gets deleted.

    Thanks.

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 570 Super User 2026 Season 2

#2
YUN ZHU Profile Picture

YUN ZHU 390 Super User 2026 Season 2

#3
Teagen Boll Profile Picture

Teagen Boll 220 Super User 2026 Season 2

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans