Export (0) Print
Expand All

Walkthrough: Creating and Using a Client Control Add-in

This walkthrough demonstrates how to create a Microsoft Dynamics NAV control add-in and use it on a Microsoft Dynamics NAV page. A client control add-in enables you to add custom functionality to the Microsoft Dynamics NAV Windows client and the Microsoft Dynamics NAV Web client by creating a control add-in that can run on both client platforms.

Dn182584.Important(en-us,NAV.71).gifImportant
This walkthrough addresses the Microsoft Dynamics NAV implementation of extensibility. This makes it possible to write control add-ins for all display targets. For information about how to write control add-ins specifically for Microsoft Dynamics NAV Windows client, see Extending the Windows Client Using Control Add-ins.

In a typical business scenario, developers create control add-ins using Microsoft Visual Studio Express, Visual Studio 2008, Visual Studio 2010, or Visual Studio 2012. Implementers of Microsoft Dynamics NAV solutions then use the control add-ins on Microsoft Dynamics NAV client pages, such as the Windows client or the Web client.

To complete this walkthrough, you will need:

  • Microsoft Dynamics NAV with a developer license. For more information, see System Requirements for Microsoft Dynamics NAV 2013 R2.

  • CRONUS International Ltd. demonstration database.

  • Microsoft Visual Studio Express, Microsoft Visual Studio 2008, Microsoft Visual Studio 2010, or Microsoft Visual Studio 2012.

  • Microsoft .NET Strong Name Utility (sn.exe). This is included with Windows SDKs.

  • Experience using Visual Studio.

Simon is a software developer working for CRONUS International Ltd. He has been told that the users of the Microsoft Dynamics NAV Web client want to see Bing Maps displayed on the Web client. He wants to use the client extensibility framework to test how to do this on a separate page first.

Microsoft Dynamics NAV includes the Microsoft.Dynamics.Framework.UI.Extensibility.dll assembly that defines the model for creating Microsoft Dynamics NAV control add-ins. The Microsoft Dynamics NAV API provides the binding mechanism between the Microsoft Dynamics NAV control add-in and the Microsoft Dynamics NAV framework.

  1. In Visual Studio, on the File menu, choose New, and then choose Project.

  2. Under Installed Templates, choose Visual C#, and then choose Class Library.

  3. In the Solution Name text box, enter the name of your solution. For example, you can enter BingMapsControlAddIn and then choose the OK button.

  4. You will add a reference to the following assembly: Microsoft.Dynamics.Framework.UI.Extensibility.dll

  5. In Solution Explorer, right-click your project, and then choose Add Reference.

  6. In the Add Reference window, on the Browse tab, navigate to the location of the Microsoft.Dynamics.Framework.UI.Extensibility.dll assembly on your computer, and then choose the OK button. By default, the path of the assembly is C:\Program Files (x86)\Microsoft Dynamics NAV\71RoleTailored Client.

  7. Open the Class1.cs file and add the following using directive.

    using Microsoft.Dynamics.Framework.UI.Extensibility;
    
  8. In the ClassLibrary1 namespace, add the following code to declare a new interface named BingMapsControlAddIn.

    namespace ClassLibrary1
    {
        [ControlAddInExport("BingMapsControl")]
        public interface IBingMapsControlAddIn
        {
            [ApplicationVisible]
            event ApplicationEventHandler ControlAddInReady;
            [ApplicationVisible]
            event ApplicationEventHandler MapLoaded;
            [ApplicationVisible]
            void LoadMap(double latitude, double longitude);
            [ApplicationVisible]
            void ShowMiniMap(bool show);
            [ApplicationVisible]
            void ShowPushpin(string title, string imageName);
        }
    }
    
    
  9. You will use the name BingMapsControl later in the walkthrough when you register the control add-in in Microsoft Dynamics NAV.

The assembly must now be signed to be used with Microsoft Dynamics NAV. The next steps will discuss how to sign the assembly.

  1. In Visual Studio, on the Project menu, choose BingMapsControlAddIn properties.

  2. In the Properties window, choose Signing, and then select the Sign the assembly check box.

  3. In the Choose a strong name key file drop-down list, select New.

  4. In the Key file name text box, enter BingMapsControlAddIn, and then clear the Protect my key file with a password check box.

  5. Choose the OK button.

  6. In Solution Explorer, notice the BingMapsControlAddIn.snk file that is added in Solution Explorer.

  7. On the Build menu, choose Build <Your Solution> to build the project. Verify that the build succeeds.

After you build the control add-in, you copy the output assembly file to the computer that is running the Microsoft Dynamics NAV Development Environment.

To copy the control add-in assembly to the Microsoft Dynamics client

  1. On the computer, locate and copy the control add-in assembly file (.dll) file in the control add-in project’s output folder.

  2. By default, this folder is C:\Documents\MyDocuments\Visual Studio\Projects\[Your Addin Project]\[Your Class Library]\bin\Debug.

  3. On the computer that is running the development environment, paste the assembly in the Add-ins folder.

    By default, this folder is C:\Program Files (x86)\Microsoft Dynamics NAV\71\RoleTailored Client\Add-ins.

After you create an interface in Visual Studio that exposes a number of properties for the BingMapsControlAddIn, you must create a manifest file. A manifest file is written in XML and contains information such as where to look for resource files, references to external JavaScripts, and the size of the control add-in. For more information, see Manifest Overview. In the next steps, you will create a manifest file that loads a BingMaps control and you will register this manifest in the Client Add-in page.

To create the manifest file

  1. Copy this sample manifest and paste it into any text editor.

    <?xml version="1.0" encoding="utf-8"?>
    <Manifest>
        <Resources>
            <Script>Script.js</Script>
        </Resources>
        <ScriptUrls>
            <ScriptUrl>http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js</ScriptUrl>
            <ScriptUrl>http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.3</ScriptUrl>
        </ScriptUrls>
        
        <Script>
            <![CDATA[
            $(document).ready(function() 
            { 
                InitializeMap('controlAddIn');
                Microsoft.Dynamics.NAV.InvokeExtensibilityMethod('ControlAddInReady', null);
            });
            ]]>
        </Script>
        
        <RequestedHeight>300</RequestedHeight>
        <RequestedWidth>700</RequestedWidth>
        <VerticalStretch>false</VerticalStretch>
        <HorizontalStretch>false</HorizontalStretch>
    </Manifest>
    
    
  2. Save the manifest to a file that is in same directory that the assembly is saved in, the Add-ins directory.

    Name the manifest Manifest and make sure to add the .xml extension to the file, so that the file name will now be Manifest.xml.

The next step is to create a JavaScript file containing code that calls C/AL in Microsoft Dynamics NAV.

Now you must create a JavaScript file to hold all of the code that calls C/AL in Microsoft Dynamics NAV.

To create the JavaScript file

  1. Copy this sample script and paste it into any text editor.

    var map = null;
            function InitializeMap(controlId) {
                map = new VEMap(controlId);
                map.HideScalebar();
                map.onLoadMap = function () {
                    Microsoft.Dynamics.NAV.InvokeExtensibilityMethod ('MapLoaded', null);
                };
            }
            function LoadMap(latitude, longiture) {
                var mapOptions = new VEMapOptions();
                mapOptions.DashboardColor = "black";
                mapOptions.EnableSearchLogo = false;
                map.LoadMap(
                    new VELatLong(latitude, longiture), // Center
                    1,                                  // Zoom level 1-19
                    VEMapStyle.Birdseye,                // Map style
                    false,                              // Fixed map
                    VEMapMode.Mode2D,                   // Map mode
                    true,                               // Map mode switch
                    0,                                  // Tile buffer
                    mapOptions                          // Options
                );
            }        
            function ShowMiniMap(show) {
                if (show)
                    map.ShowMiniMap();
                else
                    map.HideMiniMap();
            }
            function ShowPushpin(title, imageName) {
                map.Clear(); 
                if (title != '') {
                    var point = map.GetCenter();
                    var pushpin = map.AddPushpin(point);
                    pushpin.SetTitle(title);
                }
            }
    
  2. Save and name the script Script and make sure to add the .js extension to the file, so that the file name will now be Script.js.

The next step is to create a .zip file containing the manifest and resource files and register this file with the control add-in in Microsoft Dynamics NAV.

Before registering the control add-in in Microsoft Dynamics NAV, you must create one single file containing the manifest and any resource files. This single file is a .zip file and it will be registered in the Client Add-in page. The .zip file must contain a certain structure for it to be recognized by the Client Add-in page. In the next steps, you will create the right structure and a .zip file.

To create a resource .zip file

  1. On your computer, in a folder of your own choice, create the following folder structure: Image, Script, and StyleSheet.

  2. Locate the Manifest.xml file that you created in the previous steps, and copy this to the same folder structure. Then locate the Script.js file that you created in the previous steps, and copy this to the Script folder. Your folder should now look like this:

    Folder structure before creating the .zip file
  3. Place images, scripts, and stylesheets in the right folders, but in this walkthrough we will leave the rest of these folders empty.

  4. In the Windows Explorer mark all of the folders and the manifest file and right-click, and then choose Send to…, and then choose Compressed (zipped) folder.

  5. Name the .zip file BingMapsControlAddIn.

To register a control add-in, you include it in the Control Add-in page in Microsoft Dynamics NAV. To include a control add-in in the page, you must provide the following information:

  • Control Add-in name.

    The control add-in name is determined by the Microsoft.Dynamics.Framework.UI.Extensibility.ControlAddInExport attribute value of control add-in class definition that you specified when you created the control add-in. The name in this walkthrough is BingMapsControl.

  • Public key token.

    This is a 16-character key that is given to the assembly when it is signed and built in Visual Studio. You can determine the public key token by running the Microsoft .NET Strong name Utility (sn.exe) on the assembly. You must run the utility from the Visual Studio command prompt. The sn.exe utility is available with Visual Studio.

  • Resource.

    On the Control Add-in page, the Resource field is a BLOB data type and the content of the field cannot be viewed from the development environment.

    When this field is filled in, Microsoft Dynamics NAV identifies the registered control add-in as a type of control add-in that works on all display targets.

To determine the public key token for the control add-in

  1. On the Windows taskbar, choose Start, choose All Programs, choose Microsoft Visual Studio 2012, choose Visual Studio Tools, and then choose Visual Studio Command Prompt (2012) to open the command prompt.

  2. At a command prompt, change to the directory that contains the assembly that you copied. For example, C:\Program Files (x86)\Microsoft Dynamics NAV\71\RoleTailored Client\Add-ins.

  3. Type the following command: sn –T <assembly>

    Replace <assembly> with the assembly name, such as BingMapsControlAddIn.dll.

  4. Press Enter and note the public token key that is displayed.

To include the control add-in in the Control Add-in page

  1. Open Microsoft Dynamics NAV.

  2. In the Search box, enter Control Add-ins and then choose the related link.

  3. In the Control Add-ins window, choose New, and then in the Control Add-in Name column, enter the control add-in name. In the Public Key Token column, enter the public key token that you obtained earlier.

With the Control Add-in window still open, you will now import the .zip file for the BingMapsControlAddIn add-in.

To include the .zip file in the Control Add-in page

  1. Choose Import.

  2. In the Import Control Add-in Resource window, locate the BingMapsControlAddIn.zip file that you saved earlier, and then choose Open.

  3. Choose the OK button to close the Control Add-in page.

You have set up the prerequisites for using a control add-in from a page. Now you need a way to display the BingMapsControlAddIn control. In this section, you will create a new page called Bing Maps that contains two fields to control the coordinates of the map and one field that contains the map control. This involves the following tasks:

  • Creating a new page called Bing Maps.

  • Setting variables and properties.

  • Adding C/AL triggers.

To create the Bing Maps page

  1. In the Microsoft Dynamics NAV Development Environment, in Object Designer, choose Page, and then choose New.

  2. In the New Page dialog, choose Create blank page and then choose the OK button.

  3. In Page Designer, on the first line, in the Name column, enter Control1, and go to the next line.

  4. In the Type column, choose Group, and in the Caption column, enter Coordinates.

  5. On a new line, in the Type column, choose Field. In the Name column, enter LatitudeControl, and in the Caption column, enter Latitude.

  6. On a new line, in the Type column, choose Field. In the Name column, enter LongitudeControl, and in the Caption column, enter Longitude.

  7. On a new line, choose Group, and in the Caption column, enter Map.

  8. On a new line, under the Map group, for the Type column set to Field, in the Name column, enter Bing Maps Control.

    Your page design should now look like this.

    The page design for the Bing Maps control
  9. Save and compile the page. Name the page Bing Maps.

Now you have created a page skeleton. The next step is to add code and call the control add-in from a control on the page.

To add variables and properties

  1. In the Microsoft Dynamics NAV Development Environment, in Object Designer, choose Page, and then choose the Bing Maps page.

  2. On the Tools menu, choose View, and then C/AL Globals.

  3. On the Variables tab, enter Latitude in the Name column, and set the DataType to Decimal.

  4. Create a new variable. Enter Longitude in the Name column, and set the DataType to Decimal.

  5. Create a new variable. Enter MiniMap in the Name column, and set the DataType to Boolean.

  6. Close the C/AL Globals window.

  7. In Page Designer, select the BingMapsControl field, and then on the View menu, choose Properties.

  8. In the Properties window, locate the ControlAddIn property and choose the Up Arrow. Select the BingMapsControl control add-in from the Client Add-in window.

  9. Choose the OK button to close the Client Add-In window. The public key token is inserted into the Value field. Close the Properties window.

To add C/AL triggers

  1. With the Bing Maps page open in Object Designer, on the View menu, choose C/AL Code.

  2. In the Page Bing Maps – C/AL Editor window, locate the BingMapControl::ControlAddInReady() trigger, and add the following line of code.

    CurrPage.BingMapControl.LoadMap(Latitude, Longitude);
    
  3. In the Page Bing Maps – C/AL Editor window, locate the BingMapControl::MapLoaded() trigger, and add the following line of code.

    CurrPage.BingMapControl.ShowMiniMap(MiniMap);
    
  4. Finally, locate the OnInit() trigger, and add the following line of code.

    Latitude := 40.689467;
    Longitude :=  74.044444;
    
  5. Save and compile the Bing Maps page.

After you have saved and compiled the Bing Maps page, you can run the page directly from the development environment to verify that it works on the Microsoft Dynamics NAV Web client. For more information, see Opening a Page in the Microsoft Dynamics NAV Web Client by Using a URL.

Now you have created a Client Control Add-in that runs on both Microsoft Dynamics NAV Windows client and Microsoft Dynamics NAV Web client. A next step could be to implement a control add-in on an existing page or on the Role Center to display a Bing Map of where customers are located.

Show:
© 2014 Microsoft