Showing posts with label Metadata in Operations 365. Show all posts
Showing posts with label Metadata in Operations 365. Show all posts

Monday, 25 December 2017

Get Metadata in Operations 365

Introduction:
In Dynamics AX 2012, to get the tables, fields, forms, menu-items, you can query UtilElements table. However, starting Operations 365, this support is deprecated, even though the table is there, but it is empty. Instead of it, Microsoft has introduced a new class library, Microsoft.Dynamics.Ax.Xpp.MetadataSupport, which helps you to get Metadata objects.


This post will help you in getting all fields as well as table relations of SalesTable

Code Snippet:

public static void main(Args _args)
{
    //get SalesTable table name
    str tableName = tableStr(SalesTable);
    
    //Call the MetadataSupport method, GetTable to get table details
    AxTable axTable = Microsoft.Dynamics.Ax.Xpp.MetadataSupport::GetTable(tableName);  
        
    if(axTable != null)
    {                 
        info("****Fields****");
        
        //get list of fields and use an enumerator to loop through all fields      
        var axTableFieldList = axTable.Fields;        
        System.Collections.IEnumerator axTableFieldsEnumerator = axTableFieldList.GetEnumerator();

        while (axTableFieldsEnumerator.moveNext())
        {
            AxTableField axTableField = axTableFieldsEnumerator.get_Current();

            info(axTableField.Name);
        }
                
        info("****Relations****");
        
        //get all table relations as Key value pair
        var keyValueRelations  = Microsoft.Dynamics.Ax.Xpp.MetadataSupport::GetTableRelations(tableName);       
        var relationsEnumr = keyValueRelations.GetEnumerator();

        while (relationsEnumr.MoveNext())
        {
            //the relations are stored in Constraints object
            AxTableRelation tableRelation =  relationsEnumr.Current;
            var constraints = tableRelation.Constraints;
            var constraintsEnmr = constraints.GetEnumerator();

            while (constraintsEnmr.MoveNext())
            {
                AxTableRelationConstraintField constraintField =  constraintsEnmr.Current;
                
                info(tableRelation.RelatedTable);
                info(constraintField.Field);
            }            
        }
    }
}