Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Walkthrough: Using the Metadata Service to Get Table Field Labels [AX 2012]

Updated: July 21, 2011

Applies To: Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012

The metadata service is one of the system services exposed by Microsoft Dynamics AX and that adheres to the Windows Communication Foundation (WCF) protocols and standards. This service enables you to retrieve information about AOT objects such as tables, services, extended data types (EDTs), enums, and so on. In this walkthrough, you will call the metadata service from a C# program and return field names and field labels for all the fields in a particular table as specified by the table ID.

This walkthrough illustrates the following tasks:

  • Add a reference to the metadata service.

  • Call the service and retrieve the metadata.

To complete this walkthrough, you will need:

  • Microsoft Dynamics AX 2012

  • Visual Studio 2010

In order to call the metadata service, you must first create a project in Visual Studio and add a reference to the service.

To add a reference to the metadata service

  1. Open Visual Studio and create a new project by selecting File > New > Project.

  2. In the Installed Templates tree, select Visual C# > Windows and then click the Console Application template.

  3. Type a project name such as MetadataSvcGetFieldLabels and then click OK.

  4. In Solution Explorer, right-click References and select Add Service Reference.

  5. In the Address Field, enter the URL of the service. For example, http://servername:8101/DynamicsAx/Services/MetadataService.

  6. Click Go. In the Services tree, you should see the AxMetadataService.

  7. Type MetadataServiceReference in the Namespace field and then click OK.

Now we will write the code that calls the metadata service and returns the metadata for a single query.

To call the metadata service

  1. In the Program.cs file, add the following using statements.

    using System;
    using MetadataSvcGetFieldLabels.MetadataServiceReference;
    
  2. In the Program.cs file Main method add the code to call the service. The contents of the Program.cs file should resemble the following:

    using System;
    using MetadataSvcGetFieldLabels.MetadataServiceReference;
    
    
    namespace MetadataSvcGetFieldLabels
    {
        class Program
        {
            static void Main(string[] args)
            {
                AxMetadataServiceClient client = new AxMetadataServiceClient();
    
                try
                {
                    // The table ID of the SalesTable.
                    int[] tableIDs = new int[1];
                    tableIDs[0] = 366;
    
                    // Gets the table metadata.
                    TableMetadata[] tableMetadata = client.GetTableMetadataById(tableIDs);
                    TableMetadata salesTable = tableMetadata[0];
    
                    foreach (DataFieldMetadata fieldMetadata in salesTable.Fields)
                    {
                        // Print the field name and the label for the field to the console.
                        System.Console.WriteLine("Field named: {0}, Field label: {1}", fieldMetadata.Name, 
                               client.GetLabelMetadataForLanguageById("EN-US", new string[] { fieldMetadata.LabelId })[0].LabelString);
                    }
                    Console.ReadKey(true);
    
                    client.Close();
                }
                catch
                {
                    client.Abort();
                    throw;
                }
            }
         }
     }
    
  3. Run the program. The program loops through the fields in the SalesTable table and writes the name of each field and the field label to the console window. The output resembles the following:

    Field named: SalesId, Field label: Sales order
    Field named: SalesName, Field label: Name
    Field named: Reservation, Field label: Reservation
    Field named: CustAccount, Field label: Customer account
    Field named: InvoiceAccount, Field label: Invoice account
    …
    

    Notice that the code calls the GetLabelMetadataForLanguageById method and takes a parameter that specifies a language. This method can be used for localization code because it enables you to return a label in a specific language. You can retrieve the language values by calling the GetLanguages method.

    TipTip

    If you run the code and receive a message size exception, you may need to modify settings in the app.config file. For more information, see Message Size Error in Troubleshooting System Services

NoteNote

If you run this code sample and the program returns field labels such as @SYS1234 instead of the actual field label, it may be because the user calling the service is not a valid user in Microsoft Dynamics AX. Try adding the user under which the service is being called as a Microsoft Dynamics AX user and run the program again.

The TableMetadata class contains all the methods and properties for a single table. If you are working with other objects such as queries, you can access the data through the corresponding metadata object, for example, the QueryMetadata class.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.