How to Delete records using Odata Dynamics NAV 2017 web services

为君一笑 提交于 2019-12-08 00:08:31

问题


I was developed a php application that connects to Microsoft Dynamics NAV 2017 OData Web Services, I can read (GET), and create (POST) with no problems,but for delete I receive the error 405, Microsoft say that it is possible to delete :

https://msdn.microsoft.com/es-es/library/dd355398(v=nav.90).aspx

https://msdn.microsoft.com/en-us/library/dn182582(v=nav.90).aspx

I check the page in Dynamics NAV that have a correct property InsertAllowed, ModifyAllowed, or DeleteAllowed, is set to Yes, and I have permissions to delete

After try with postman recevie the same error:

Can someone help me? Thanks


回答1:


Finally I found the solution!! , I write myself to help another who is with the same problem:

You only have to add the identifier on the request url, in my case the identifier of the customer table ('/Customer(No='.$identifier.')')

This is a sample code in PHP with guzzle and table customer of Dynamics NAV:

 $client = new GuzzleHttpClient();
 $uri=env('HTTP_URIBASE', '');
 $apiRequest = $client->request('DELETE', $uri.'/Customer(No='.$identifier.')',[
        'auth' => [env('HTTP_USERNAME', 'usuari'),env('HTTP_PASSWORD', ''), 'ntlm' ],
        'headers' => ['Content-Type' => 'application/json', 
                       'Accept' => 'application/json']
  ]);
  $content = json_decode($apiRequest->getBody()->getContents());

for updates (PATCH) I have to first read the etag of the reccord (@odata.etag), and add on the headers (If-Match value) for update:

 $client = new GuzzleHttpClient();
 $uri=env('HTTP_URIBASE', '');
 // get the recordset of the customer
 $apiRequest = $client->request('GET', $uri.'/Customer(No='.$identifier.')',[
            'auth' => [env('HTTP_USERNAME', 'usuari'),env('HTTP_PASSWORD', ''), 'ntlm' ]     
            ]);
 $content = json_decode($apiRequest->getBody()->getContents());
 $etag= $content->{'@odata.etag'};

 // update description of the customer
 $apiRequest = $client->request('PATCH', $uri.'/Customer(No='.$identifier.')',[
        'auth' => [env('HTTP_USERNAME', 'usuari'),env('HTTP_PASSWORD', ''), 'ntlm' ],
        'headers' => ['Content-Type' => 'application/json', 
                       'Accept' => 'application/json',
                       'If-Match' =>$etag ],
        'body'    => '{"Name":"'.$missatge.'"}' 
         ]);
 $content = json_decode($apiRequest->getBody()->getContents());


来源:https://stackoverflow.com/questions/47618045/how-to-delete-records-using-odata-dynamics-nav-2017-web-services

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!