如何使用Odata Dynamics NAV 2017网络服务删除记录

时间:2017-12-03 11:48:30

标签: web-services odata dynamics-nav dynamics-nav-2016

我开发了一个连接到Microsoft Dynamics NAV 2017 OData Web服务的php应用程序,我可以读取(GET),并且创建(POST)没有问题,但是对于删除我收到错误405,微软说它是可以删除:

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

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

我检查Dynamics NAV中具有正确属性InsertAllowed,ModifyAllowed或DeleteAllowed的页面是否设置为是,并且我有权删除

尝试与邮递员接受相同的错误:

Delete Error Odata debug with postman

有人能帮助我吗?感谢

1 个答案:

答案 0 :(得分:2)

最后我找到了解决方案!! ,我写自己帮助另一个遇到同样问题的人:

您只需在请求网址上添加标识符,在我的情况下是客户表格的标识符('/ Customer(No ='。$ identifier。')'

这是PHP中的示例代码,其中包含Dynamics NAV的guzzle和table客户:

 $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());

更新( PATCH )我必须首先阅读reccord的 etag @ odata.etag ),然后添加更新的标题( If-Match 值):

 $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());