But they know their NAV, too, and after NAV TechDays 2011, which have just ended in Antwerp, and two days of top NAV content, I can only say – great job, Luc and the team, and please make it a tradition.
If you attended my presentation about .NET interoperability, then there are a couple of demos I couldn’t deliver due to time constraints, and I promised to blog it. So, here we go.
It’s about streams. You already know that in NAV there are two data types, InStream and OutStream, that allow you to stream data in and out of generic sources or destinations. They are a fantastic tool, because they require you to know nothing about the type of source or destination, and you can store and retrieve data without having to care if the data comes from Internet, or a BLOB field, or is it written to a file, or transported over an XMLport. Stream makes it abstract and allows you to simply handle the data, and make the object itself care about the specifics.
In .NET, streams a bit more complex, or comprehensive if you wish, than in C/AL, and there are specific types of streams which do not exist in C/AL. For example, System.IO.FileStream and System.IO.MemoryStream are two stream types which handle specifically file data, or in-memory data. Luckily, they both inherit from System.IO.Stream, and System.IO.Stream is fully and bi-directionally mappable to InStream and OutStream of C/AL.
If you don’t see a galaxy of opportunities here, then just follow this demo. Let’s make a totally useless, but cool and simple, demo with streams. Let’s stream data from a blog directly into NAV.
1. Create the table and UI in NAV
A chore first, fun later. To store any kind of data in NAV, you need a table, so let’s create one:
No triggers or anything funny. Just what you see above. Save it, and create a page of ListPart type:
For the last field, set the ExtendedDataType to URL. Just in case, make the page non editable.
Finally, design the page 9006 Order Processor Role Center and add page 50001 Blog Posts as a Part control of SubType Page to the top of the second Group. It’s simple, so no screenshots here. Life’s tough.
If everything was done correctly, this will be your role center:
Notice the Blog Posts in the top right. It’s empty, and at this point it’s pretty much by design. Patience, we are almost there.
2. Review the blog stream and create an XMLport
Navigate to http://NavigateIntoSuccess.com/feed/ and take a look at the source. Obviously, a blog’s feed is a simple XML file. So, a million dollar question (only I don’t have that million, yet): how do you import an XML into NAV?
You’ve got it right: the XMLport. A nice little gizmo which lets you model NAV data as XML and then stream it in and out. Apart from them being able to natively snakker XML, there is one particular feature that makes them extremely suitable for this demo. They handle streams, not files.
So, create this XMLport:
Must have taken all 56 seconds of your life, eh?
3. Read the feed
There is nothing simpler than this. Create a new Codeunit, save it as 50001 Blog Post Management.
Create a new function called ImportRss, have it receive a parameter called Url of type Text. Declare a new local variable of type DotNet, call it XRss, and set its subtype to ‘System.Xml.Linq, Version=184.108.40.206, Culture=neutral, PublicKeyToken=b77a5c561934e089’.System.Xml.Linq.XDocument
The body of the function should look like this:
XRss := XRss.Load(Url);
That’s right. That thing will have your feed loaded. If you don’t believe, give it a try. Add the following line below:
Yes, I know – I’ve violated my own hardcoding rules by hardcoding the URL of my blog – but that was on purpose. If I have you make setup for this, I bet you a glass of Duvel and a box of Godiva that you would put some other feed’s URL. As if you can’t change this one, but I know you won’t, will you?
Go. Restart your RTC and watch! I’d put a screenshot here, but it’d be very large and equally as useless.
But now there is a problem. The feed you have in front of you doesn’t exactly match the blueprint laid out by this XMLport, does it? There are plenty more elements in the feed, that NAV or database couldn’t care less about, so there are two choices here: either specify all that unneeded extra info right here in the XMLport (which will take like half an hour of your time, at best), or make a quick .NET function to strip that extra info away.
Since this post is about .NET, and not about stuffing up XMLports, you’ve got it again: we take the .NET path.
4. Create a .NET object to transform the feed
It’s far simpler than it sounds. Start Visual Studio, create a new C# project of type Class Library (or use the Microsoft Dynamics NAV .NET Interoperability Assembly template I give you, for free, at the end of the post in the Downloads section), call it XmlHelper, and create the following class:
The core of it is this simple statement:
return new XDocument(
from x in xrss.Element("rss").
select new XElement("item",
new XElement("link", x.Element("link").Value),
new XElement("title", x.Element("title").Value)
Since this is about .NET, and most of NAV folks aren’t too .NET savvy, let me explain how it works. It creates a new XDocument instance (a class which handles XML data in a nice and simple way), and makes it have the same structure as our feed:
XRss := XRss.Load(Url);
XRssSimplified := XmlHelper.SimplifyRss(XRss);
There. You can now have your role center nicely adorned with the latest feed from my blog (or any other, if you want to cheat ), and stay up to date with, well, whatever it is to stay up to date, right from your role center.
And last, but not least, here is the ZIP file with all the demos.
I hope this little walkthrough helps you understand the concept of streaming using .NET interop better.