Writing an automation for Navision in C#

code-dynamics

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.)

You can leave a response, or trackback from your own site.

10 Responses to “Writing an automation for Navision in C#”

  1. mvcFan says:

    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. iby says:

    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.NET\Framework\v2.0.50727\regasm.exe
    set gacutil=C:\program files\Microsoft SDKs\Windows\v6.0a\bin\gacutil.exe

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

    and an example for unregistring the automation:

    @echo off
    set regasm=%windir%\Microsoft.NET\Framework\v2.0.50727\regasm.exe
    set gacutil=C:\program files\Microsoft SDKs\Windows\v6.0a\bin\gacutil.exe

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

  3. salman says:

    Thank you for the Example.

  4. Bent says:

    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.

  5. Fabrice Bourqui says:

    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

  6. User says:

    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.

  7. Ferry says:

    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

  8. Meera says:

    hi ! can u let me know how the codings should be in VB.Net?

  9. Samu Thangadurai says:

    Very nice.. It is Possible to automate the reg/unreg automated while Install/UnInstall

  10. Hubert says:

    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


− six = 2