Writing an automation for Navision in C#

Writing a automation for Navision is pretty simple, when you first know how to do it 🙂

First of all, I assume that you already know about C#.

So here are the basic steps for creating an automation:


1. Create a new Class Library project.

  • Chose File -> New -> Project and then chose Class Library and save it as myAutomation (or use you own name)

2. Before making any changes – setup the Properties

  • Chose Project -> myAutomation Properties
  • Select tab Application and then the button Assembly Information
    • In the Assembly Information window, mark “Make assembly COM-Visible” and chose Ok
  • Select tab Build
    • Mark “Register for COM interop”
  • Select tab Signing
    • Mark “Sign the assembly” and create a key files (or chose an existing one)

Now everything is setup and you can close the property setup.

3. Add the namespace for InteropServices

  • This is done by adding using System.Runtime.InteropServices

You should now have a code similar to the following:

using System;
using System.Text;
using System.Runtime.InteropServices;

namespace myAutomation
{
   public class myAutomation
   {
   }
}

4. Declare the interface

First you have to decide if the class should be visible or not. In this case we want it to be visible, so that we can use it with the automation.

[ComVisible(true)]

Next you can choose to set the Guid and ProgId.

[Guid("41E646B3-F65C-4d8e-8539-499FA56C7076")]
[ProgId("myAutomation")]

Then decide the Class Interface Type. You can choose between None and Autodual.

[ClassInterface(ClassInterfaceType.AutoDual)]

For now we will be using Autodual, because None requires the use of Interface and class’es – where as Autodual only uses class.

You should now have a code similar to the following:

using System;
using System.Text;
using System.Runtime.InteropServices;

namespace myAutomation
{
   [ComVisible(true)]
   [Guid("41E646B3-F65C-4d8e-8539-499FA56C7076")]
   [ClassInterface(ClassInterfaceType.AutoDual)]
   [ProgId("myAutomation")]
   public class myAutomation
   {
   }
}

Now you can add all the functions that you need.

Notice, you can hide functions and classes from the automation by using Private or [ComVisible(false)]

Here is a code example for an automation with visible and invisible methods.

using System;
using System.Text;
using System.Runtime.InteropServices;

namespace myAutomation
{
   [ComVisible(true)]
   [Guid("41E646B3-F65C-4d8e-8539-499FA56C7076")]
   [ClassInterface(ClassInterfaceType.AutoDual)]
   [ProgId("myAutomation")]
   public class myAutomation
   {
       public Int16 Add(Int16 X, Int16 Y)
       {
           Int16 Result = (Int16)(X + Y);
           return Result;
       }

       //Is hidden in the Automation because of Private
       private Int16 PrivateAdd(Int16 X, Int16 Y)
       {
           Int16 Result = (Int16)(X + Y);
           return Result;
       }

       //Is hidden in the Automation because of ComVisible
       [ComVisible(false)]
       public Int16 InvisibleAdd(Int16 X, Int16 Y)
       {
           Int16 Result = (Int16)(X + Y);
           return Result;
       }
   }
}

Functions are in the automation presented as Methods, where as C# properties (public set; get;) are presented as properties.

Please note, that I am using Visual Studio C# 2008 (you can download the Express version from Microsoft here.)

10 Comments

  1. Your article is great! And it’s exactly what I need to do.
    The problem is that my class myAutomation doesn’t appear in Navision.
    (VS2008 cannot register the assembly, access denied).
    Do you have any idea?

  2. You can not registrate the automation directly through VS2008. Here you have to use regasm and gacutil. Regasm is a part of your framework, where as gacutil is a part of Microsoft SDK.

    Ex. on a dos script for registring the automation:

    set regasm=%windir%Microsoft.NETFrameworkv2.0.50727regasm.exe
    set gacutil=C:program filesMicrosoft SDKsWindowsv6.0abingacutil.exe

    %regasm% “myAutomation.dll” /tlb:myAutomation.tlb
    %gacutil% /i “myAutomation.dll”

    and an example for unregistring the automation:

    @echo off
    set regasm=%windir%Microsoft.NETFrameworkv2.0.50727regasm.exe
    set gacutil=C:program filesMicrosoft SDKsWindowsv6.0abingacutil.exe

    REM Unregister types
    %regasm% /unregister /tlb:myAutomation.tlb “myAutomation.dll”
    %gacutil% /u “myAutomation”

  3. I wonder if there is a .Net API to un/register and un/GAC assemblies. This could be used with a stealth file-system service that intercepts file system calls to registered assemblies and pre-empts these with an unregister and un-GAC in case of a delete, for example – and perhaps does a full reregistration in case of a rename. Oh Microsoft, where art thou.

  4. Hi thanks for your solution, I’ve tried your solution but when I compile in VS I have an error.. Can you tell me how set the GUID in point4? For my, I have copy and paste the guid from the button Assembly information from the properties of the project..
    Thanks for your reponse

  5. Hello, it helped alot. But i have a question, maybe you can help. How to send IXMLDomNode to Automation. Now i writen a funcion that takes IXMLDomNode and set’s it text and returns it:
    public IXMLDOMNode XML3(String text, IXMLDOMNode nodes)
    {
    nodes.text = text;
    return nodes;

    }

    But when i try to send a node from navision i get this error:
    Could noty invoke the memeber XML3.
    I try to send : DOMNode := XMLDoc.createNode(1,’NewNode’,”);
    Any suggestions?
    I can send and get string, it works great with strings and int.

  6. Hello,
    Please help, I’ve question for it. If I have DataTable as the return value, how can I retrieve it on Navision ? or is there any other way to have table as automation output value ?

    Many Thank,
    Ferry

  7. What’s Happening i’m new to this, I stumbled upon
    this I’ve found It

    absolutely helpful and it has helped me out loads. I hope to
    give a

    contribution & aid other users like its helped me.
    Great

    job.

Leave a Reply

Your email address will not be published.


*