Walkthrough: Uploading a File from a Client to Microsoft Dynamics NAV Server

This walkthrough demonstrates how to use file functions to send a file from a client to the server, use stream functions to read the file, and use an XMLport to load values into the database. In Microsoft Dynamics NAV 2009, business logic runs on Microsoft Dynamics NAV Server instead of on the client. As a result, any business logic that reads data from a file needs to first move a copy of that file from the client to the server.

This walkthrough illustrates the following tasks:

  • Creating an XML file that contains two elements with values.

  • Creating a table in Microsoft Dynamics NAV to hold the data from the XML file.

  • Creating a codeunit that does the following:

    • Sends the file from a user-specified location to a location on the server

    • Reads the data

    • Writes the data to the table

    • Deletes the file from the server

  • Associating the codeunit with an action on a Role Center page.

  • Testing the codeunit.

Prerequisites

To complete this walkthrough, you will need:

  • Microsoft Dynamics NAV 2009 installed with a developer license

The first step is to create a simple XML file that contains the data that the codeunit will read into the table. This represents the file on a client computer.

To create the XML file

  1. Open Notepad.

  2. Enter the following code.

    <?xml version="1.0" encoding="UTF-8"?><head><valueI>10</valueI><valueII>11</valueII></head>
    
  3. Save the file in the root of the My Documents directory. Name the file simple.xml.

  4. Open the file in Internet Explorer to ensure that the XML is well formed.

The next step is to create the table that will hold the data that is read from the XML file.

To create the table

  1. In Object Designer, click Table, and then click New.

    Table Designer opens.

  2. Click Field Name in the first row and then enter IntA. Set Data Type to Integer.

  3. Click Field Name in the second row and then enter IntB. Set Data Type to Integer.

  4. Save the table, giving it a name of TestTable and an ID of 50010.

  5. Close Table Designer.

The next step is to create a codeunit that will prompt the user to specify the location of the XML file, load the XML file into a stream, and then write the values of those variables to the table using an XMLport.

To create the codeunit

  1. In Object Designer, click Codeunit, and then click New.

    C/AL Editor opens.

  2. Click the OnRun() function in the C/AL Editor.

  3. Click View and then click C/AL Locals.

    The C/AL Locals window opens.

  4. Click the Variables tab.

  5. Enter the following local variables.

    Name DataType Length

    TestFile

    File

    TestStream

    InStream

    NVOutStream

    OutStream

    ClientFileName

    Text

    1024

  6. Click the next empty line to ensure that the local variables are saved.

  7. Close the C/AL Locals window.

  8. Enter the following code in the C/AL Editor for the OnRun function.

    BEGIN
        IF ISSERVICETIER THEN BEGIN
            IF UPLOADINTOSTREAM(
                           'Select the simple.xml file',
                            'c:\temp',
                            'XML File *.xml| *.xml',
                             ClientFileName,
                             TestStream)
            THEN MESSAGE('File Successfully Uploaded')
            ELSE
                EXIT;
        END
        ELSE BEGIN
            TestFile.OPEN('C:\temp\simple.xml');
            TestFile.CREATEINSTREAM(TestStream);
        END;
        XMLPORT.IMPORT(50010,TestStream);
        IF NOT ISSERVICETIER THEN TestFile.CLOSE;
    END;
    
  9. Save the codeunit, giving it a name of UploadFile and an ID of 50010.

  10. Close the code editor.

The next step is to create an XMLport.

To create the XMLport

  1. In Object Designer, click XMLport, and then click New.

    XMLport Designer opens.

  2. Enter the following information.

    Node Name Node Type Source Type Data Source

    head

    Element

    Table

    <TestTable>(TestTable)

    valueI

    Element

    Field

    <TestTable>::IntA

    valueII

    Element

    Field

    <TestTable>::IntB

  3. Set the Indentation property for value I and value II to 1.

  4. Save the XMLport, giving it a name of UploadFile and an ID of 50010.

  5. Close XMLport Designer.

Now that you have the completed codeunit, you need to associate it with an action in the RoleTailored client so it can be tested. In this case, you will add a new action to the Sales Order Processor role center and name it Upload File to Server. When selected, this action will call the codeunit.

To associate the codeunit with an action

  1. In Object Designer, click Page, select page 9006 Order Processor Role Center, and then click Design.

    Page Designer opens.

  2. Click the first empty line on the page, click View, and then click Actions.

    Action Designer opens.

  3. Click the first empty line in Action Designer and in the Caption column for that line, enter Upload File to Server. Set the Type to Action.

  4. With the Upload File to Server line selected, press SHIFT+F4 to open the Properties window.

  5. Set the RunObject property value to c50010. When this action is selected on the role center, codeunit 50010 will run.

  6. Close the Properties window.

  7. Close Action Designer.

  8. Close Page Designer.

The final step is to test the codeunit to ensure that it correctly adds the values from the simple.xml file to the TestTable.

To test the codeunit

  1. Open the RoleTailored client.

  2. On the Sales Order Processor role center, click Actions, and then click UploadFileToServer.

    The Select the simple.xml file window opens.

  3. Browse to the location of simple.xml and then click Open.

  4. Click OK.

  5. Open the Classic client.

  6. In Object Designer, click Table, click 50010 Test Table, and then click Run.

    The TestTable-Table window opens with the values of 10 and 11 in the IntA and IntB columns, respectively.

Community Additions

ADD
Show: