Create webservice in NAV2018 to use in external app, return JSON with item information

Yannick1Yannick1 Member Posts: 5
Hi All,

I need to create a webservice in NAV 2018 to be used in an external app. The webservice is called for a specific item number, the webservice should return a JSON with some relevant information of this specific item.

I have a hard time finding a good example which is covering this issue, can anybody provide me a simple example?

Thanks in advance,
Yannick

Answers

  • foo_barfoo_bar Member Posts: 91
    Expose an odata endpoint of type page (try with page 30 for testing, then create your own page)
    https://docs.microsoft.com/en-us/dynamics-nav/how-to--publish-a-web-service

    to filter to a specific record, see https://docs.microsoft.com/en-us/dynamics-nav/using-filter-expressions-in-odata-uris
  • ShaiHuludShaiHulud Member Posts: 228
    If you don't want to use ODATA, you can create a codeunit with a function that takes 2 parameters: Item No. and return text (marked with "var", of type BigText). When you prepare the return JSON, you just write it into the return text variable. Whoever calls your function, should call it with Item No. and blank string. After the call the string will be filled with JSON data.
  • denpardenpar Member Posts: 80
    edited 2020-07-27
    @ShaiHulud That sounds like a good solution... do you have a example (yourself of url) how to do that?
    If your work becomes a passion...
    www.pardaan.com
  • foo_barfoo_bar Member Posts: 91
    edited 2020-07-27
    @ShaiHulud, how do you see his external app call this function..?
    @denpar, why not use a page webservice?
  • ShaiHuludShaiHulud Member Posts: 228
    @denpar @foo_bar
    1. Create a codeunit and publish it as a webservice (simply add it to the Web Services table). You will get SOAP URL (SOAP needs to be enabled on the server instance)
    2. Inside the codeunit, create a function that you want accessible to the outside. In my case I made "GetAttributes". It has one regular parameter (LanguageCode_P) and one parameter for return (JSONReturn_P) marked with "var"
      env1kj4476jt.png
    3. The function does its thing, and at the end, writes the JSON into the JSONReturn_P (JSONReturn_P.ADDTEXT(AttributesJSON_L.ToString());)
    4. Follow the Microsoft's tutorial's "Using the Web Service" section to set up a Visual Studio project that could use the webservice
    5. Call the webservice with something like:
      JSONResult = "";
      Console.WriteLine("Calling GetAttributes function");
      ws.GetAttributes("ENU", ref JSONResult);
      Console.WriteLine("Contents of the JSON result:");
      Console.WriteLine(JSONResult);
      
  • denpardenpar Member Posts: 80
    edited 2020-07-27
    @ShaiHulud Thanks for the detailed information, really appreciate it!

    Is it possible to test it in Postman? I tried something like this....

    Made this codeunit and published it as a webservice
    mdy1hfp28pii.png

    How can I check if it's working in Postman (correct return value)?
    If your work becomes a passion...
    www.pardaan.com
  • ShaiHuludShaiHulud Member Posts: 228
    edited 2020-07-27
    @denpar
    In Postman you need to set up your call in the following way:
    1. Method: POST
    2. Add header called "SoapAction" with value "urn:microsoft-dynamics-schemas/codeunit/YOURCODEUNITNAME" - quotes have to be there, "YOURCODEUNITNAME" is the name you gave your published webservice
    3. Add body of type "Raw" subtype "XML"
    4. Write your body:
      <?xml version="1.0" encoding="utf-8"?>
      <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
        <soap:Body>
          <get:GetItemInfo xmlns:get="SAME_AS_SOAPACTION">
            <get:jsonBT></get:jsonBT>
            <get:ItemNo></get:ItemNo>
          </get:GetItemInfo>
        </soap:Body>
      </soap:Envelope>
      

    You should then see your JSON in the response body below
  • foo_barfoo_bar Member Posts: 91
    @ShaiHulud, can you tell me why you advice to use SOAP for this?
  • denpardenpar Member Posts: 80
    @ShaiHulud Thx!!!
    If your work becomes a passion...
    www.pardaan.com
  • waitwait Member Posts: 53
    Maybe I'm missing something but why don't you use the API functionality introduced in 2018?

    There is even a Item Entity and it returns JSON

    Take a look at this https://community.dynamics.com/business/b/andreysnavblog/posts/how-to-obtain-data-from-nav-standard-apis

    The url for the item entity is
    {{http}}://{{url}}/{{service}}//API/beta/companies({{companyId}})/items

    Then you just use filters to get the items you want
  • ShaiHuludShaiHulud Member Posts: 228
    edited 2020-07-28
    foo_bar wrote: »
    @ShaiHulud, can you tell me why you advice to use SOAP for this?

    I didn't say I advise it, or that it's the best, last and only solution. I offered it as an alternative and was asked for more details.
  • denpardenpar Member Posts: 80
    @wait My question is not complete... I want to return multiple levels (item -> item variant) in the JSON, I don't think that's possible with API page
    If your work becomes a passion...
    www.pardaan.com
  • waitwait Member Posts: 53
    denpar wrote: »
    @wait My question is not complete... I want to return multiple levels (item -> item variant) in the JSON, I don't think that's possible with API page

    Yes you can, that is what the API does very well and easily, you are limited to three levels though, you can return Page, SubPage and then you can also return Complex Types

    Good example would be Sales Invoice Entity (Page 5475) and Sales Invoice Line Entity (Page 5476) and The Sales Invoice Line Entity has field LineObjectDetailsJSON which is complex type.

    To get the sales invoices and sales invoice lines your url would be something like the following

    {{http}}://{{url}}/{{service}}//API/beta/companies({{companyId}})/salesInvoices?$expand=salesInvoiceLines

    Here's tutorial I did a while back on complex type, https://forum.mibuso.com/discussion/72539/nav-2018-api-complex-types#latest

  • denpardenpar Member Posts: 80
    @wait Thx!
    If your work becomes a passion...
    www.pardaan.com
Sign In or Register to comment.