AL, Business Central, Development Tips, File, How To, Information, InStream, Modern Development Tool, OutStream, Tip & Tricks, V2, Visual Studio Code, XMLports

File Handling using Stream – Business Central

Today in this post we will discuss about using Stream to Export or Import Files or in other words File handling using Streams.

First Example I am discussing about is how to Import Image to Item Picture.

First part of the code checks for if any existing Picture in the records Picture field, here we use Count to check, because Picture field is defined as MediaSet can hold multiple picture, so giving warning for Overwrite.

In Second part we are Uploading the Selected Image to the InStream.

Finally we clear the Picture field and load Picture from InStream, here file name will be used as description for the image.  And save the record post importing of the Image.

Below code sequence is self explanatory.

StreamImpImage

In Second example we are Exporting the image from the record, any/all images stored in Picture field of the Item record.

Again we are testing with Count if any Image stored, else flag the Error nothing to Export as record don’t have any picture stored.

As told above since it is MediasSet type so possibility of more than one Image may have been stored, so we setup for loop to access all of them.

In TenantMedia table all Images are stored with unique GUID, so we get them each by Item(index).

Calcfields used to load the content in memory and final checking if it HASVALUE.

We build the filename as Item_Image1.ext, we will look into GetImgFileExt function below.

Next we load the Image in InStream.

Finally we save the Image on disk by specified filename.

StreamExpImg

This below function we use to find right extension of the file from TenantMedia field Mime Type.

StreamGetFileExt

In last and below function we are using TempBlob Table and Stream to handle file.

We create the OutStream of BlobTable Blob Field, set it as destination for my Export XML Port.

Further creating the InStream we are transferring the content of Blob to the File.

StreamExportContact

Hope you will start using this technique in your future projects, when working with File Handling.

We will discuss on other concepts in our upcoming posts.

Not sure if this is my Last post for this year, so wishing you Happy New Year 2019 in advance.

Keep practicing and learning, see you again in my next post.

Till then bye, take good care of your self and remain safe.

 

Advertisement
Data, Development Tips, How To, Information, Tip & Tricks, Web Services, XMLports

Exposing & Consuming the Web service from & inside Navision – Part-2

Continuing from my previous post. Find the link to access the same in case you missed it.

Exposing & Consuming the Web service from & inside Navision – Part-1

 

Step 1: Create a XML Port to Export Data in XML Format using XMLPort and save at some specified Location.

Here we will be creating just for demo purpose simple XMLPort for Exporting Data from Navision Database in XML Format.

WebServiceUsage-2

We are using very simple structure for our XMLPort.

Make sure you set the properties accordingly as we are exporting the data in XML Format.

WebServiceUsage-3

Save the XMLPort at available id and suitable name.

 

Step 2: Create a Codeunit to call XML Port and retrieve data in XML Format in Text Variable.

Here we will create a Codeunit with few helpful functions which will call the XMLPort to Export the data and save in temp file.

Further retrieve the data from the file in XML Format.

Return the data retrieved above to the calling program/ function.

To start with let us start with creating some Global Variables.

WebServiceUsage-4

Next we will add one Function as below.

WebServiceUsage-5

Next we will add one more Function as below.

WebServiceUsage-6

Save the Codeunit at available Id and suitable Name.

 

Step 3: Expose/Publish the Codeunit as web service.

Open Web Services Page and add your Codeunit created above. Use your relevant ID on which you saved your Codeunit.

WebServiceUsage-7

 

Tick the Published to Expose the Codeunit as Web Service.

Rest in my Next post.

To Be Continued……

Data, Development Tips, How To, Information, Tip & Tricks, Web Services, XMLports

Exposing & Consuming the Web service from & inside Navision – Part-1

Introduction

Today in reply to one of my reader I am giving here steps how we can Export data using XMLPort and transfer data outside Navision using Web Services.

Lets discuss about the technology before I jump to the solution.

Few full forms which may help understanding while reading this post.

W3C – Web Services Architecture Working Group.

SOAP – Simple Object Access Protocol.

REST – Representational State Transfer.

WSDL – Web Service Description Language.

UDDI – Universal Description, Discovery & Integration.

AWS – Arbitrary Web Services.

XML – Extensible Markup Language.

HTTP – Hypertext Transfer Protocol.

 

What is Web Service?

W3C defines Web Service as : A software system designed to support interoperable Machine-to-Machine interaction over a network.

Web service can be defined as service offered by an electronic devices to another electronic devices, which communicate with each other over www.

W3C defines web services has an interface described in a machine-process able format WSDL.

Other systems I with the web service in a manner prescribed by its description using SOAP messages, typically conveyed using HTTP with an XML serialization in conjunction with other web-related standards.

Later W3C extended the definition:-

REST – compliant web services, in which the primary purpose of the service is to manipulate representations of web resources using a uniform set of stateless operations.

Arbitrary Web Services –  the services may expose an arbitrary set of operations.

WebServiceUsage-1-2

UDDI  defines which software system need to be contacted for which type of data.

Once the system is discovered to be contacted, the contact is established using SOAP.

Service provider system validate the request by referring the WSDL file, process the request and send the data using SOAP protocol.

 

Web Service is a method of communication that allows two software system to exchange the data over the internet.

  • Two systems may use different software which could be using different programming languages.
  • Almost all type of software interpret XML tags.
  • Web Services can be used to exchange data between two systems in form XML files.

WebServiceUsage-1-3

XML is used to tag the data.

SOAP is used to transfer the data.

WSDL is used to describe the services available.

UDDI lists what service are available.

Human to machine communication is utilized for machine-to-machine communication, used for transferring machine readable files such as XML & JSON formats.

Web services use SOAP over HTTP protocol, facilitating to use your existing low-cost internet for implementing Web Service.

Rules for communication between two different systems:

All the rules are defined in WSDL file.

  • How request to be send from one system to another.
  • What all parameters need to be send while requesting data.
  • What structure to be used for generating data (Normally data is exchanged in XML format) which is validated against .xsd file.
  • What errors to be generated in case of failure of rule communication set.

 

Web Services that uses Markup Language:

  • JSON-RPC
  • JSON-WSP
  • Web template
  • Web Service Description Language (WSDL)
  • XML Interface for Network Services (XINS)
  • Web Services Conversation Language (WSCL)
  • Web Services Flow Language (WSFL)
  • WS-Meta data Exchange
  • Representational State Transfer (REST)
  • Remote Procedure Call (RPC)
  • XML Remote Procedure Call (XML-RPC)

 

No more theories lets come to our point.

There are other ways too to achieve the same, but here I am specific to the requirement to which I am answering.

Requirement is the Data in XML format file will be transmitted using Web Service, We will be using Navision Exposed Web Service to Export data using XMLPort and then send the content by consuming another Web Service exposed in Dot Net which will retrieve the XML content and update in some other system.

Assumption is these two endpoints can be two separate databases in same domain or two different companies of same organization operating at two different location sharing information via Web Services.

For the demo purpose I will be using Navision to publish the service for exporting the data and retrieve the data using Web Service exposed in Dot Net.

Since I have not in depth knowledge of Dot Net codes, so I have asked my college to prepare the Web Service for me and I am simply using it to show how we can do it.

Here I will be getting this done through below 5 Steps.

WebServiceUsage-1

Next Post in this Series will contain:

Step 1: Create a XML Port to Export Data in XML Format using XMLPort and save at some specified Location.

Step 2: Create a Codeunit to call XML Port and retrieve data in XML Format in Text Variable.

Step 3: Expose/Publish the Codeunit as web service.

 

Next Post in this Series will contain:

Step 4: Create a DLL to use as Add-in to consume the Web Service itself in Navision.

 

And the Final Post in this Series will contain:

Step 5: Create a Codeunit to test Retrieve the data using above DLL and Send data in XML Format using another Web Service exposed in Dot Net.

 

Please follow my upcoming post for Walkthrough to get above 5 steps done.

To Be Continued……….

 

Corfu Navision 2016, Development Tips, How To, XMLports

Multiple namespace support in XML Ports – Microsoft Dynamics NAV 2016

Microsoft Dynamics NAV 2016 now enables developers to create XMLPorts which contain more than one namespace. Namespaces are used to distinguish fields and provide uniqueness.

The inclusion of namespaces enables parties to tag elements and resolve any ambiguity.

What is default Namespace?

This property specifies the default namespace for both import and export. The default value is unique for different XMLports.

For example, the following string specifies a namespace: urn:microsoft-dynamics-nav/xmlports/x100, where 100 is the ID of the XMLport.

What is Namespace?

You can use this property to declare one or more namespaces on the XMLport.

To add a namespace, you can choose the AssistEdit button or enter the namespaces directly in the field.

A namespace declaration consists of prefix and the namespace name, which has the format prefix=namespace. Separate multiple namespaces with a comma.

In the XML documents exported or imported by the XMLport, the namespaces declarations are only supported in the <root> element.

For example, if an XMLport has the namespace mynavprefix=mynavnamepace and the default namespace urn:nav:schema:all, then the root element will be as follows:

<Root xmlns:mynavprefix=”mynavnamespace” xmlns=”urn:nav:schema:all”>

To specify a default namespace, leave the Prefix field blank.

You can also specify the default namespace by specifying the namespace in the DefaultNamespace Property and setting the UseDefaultNamespace Property to Yes.

However, there can only be one default namespace. So if you want to specify a default namespace in the Namespace property, you must set the UseDefaultNamespace Property to No.

How to Define Namespace:
Namespace-1

Using Namespace:
Namespace-2

The final XMLport will be as below:
Namespace-3

Now save your XMLport and execute it.
Namespace-4

The output will be similar to below:
Namespace-5

Conclusion:

  • Multiple namespaces are defined in the root element of the XMLport and have their own property editor for defining them.
  • Developers can easily select the namespace via lookups on the element record.
  • XMLports are accessible both for developers and super users. Including the namespace prefix in the core editor lets you see key data while developing.

I will come up with more information in my upcoming posts.