With the redesign of the X++ compiler (that I go over in depth inĀ this post) to allow for interfacing with C#/.NET in D365FO I’ve tried every chance I get to code my complex business logic in .NET instead of X++. Mostly because, as a .NET developer by trade, I feel more comfortable in that language but also because I think personally that .NET gives you more flexibility than X++ does. But with this decision comes a number of challenges, first and foremost there are some processes that become very difficult without some standard X++ classes and methods.
One of those tasks is trying to get labels for different objects from within a C#/.NET project. Here are the steps I had to take to be able to accomplish this:
First I had to reference the necessary X++ libraries to be able to actually get to the classes/methods necessary, here are the libraries you will need to reference.
- Microsoft.Dynamics.AX.Metadata
- Microsoft.Dynamics.AX.Metadata.Core
- Microsoft.Dynamics.AX.Server.Core
- Microsoft.Dynamics.AX.Xpp.AxShared
- Microsoft.Dynamics.AX.Xpp.Support
Now we can start looking at how to get labels for different object types.
In the first example below we are looking specifically at tables, you can see we now have access to the X++ Dictionary which is a very useful object especially if you want to query objects in the system. In this case we use this Dictionary object to get an AxTable object and that allows us to get the label tied to that object.
In the second example, we are looking at menu items. In this case we are specifically looking at menu item displays but all menu item types act in a very similar way. We are able to get back a menu item object using the MetadataSupport class which is an important class when dealing with objects and their metadata. From there we are able to get the label for this, but as anyone who deals with AX/D365 knows this label is actually the label Id and is associated to the actual label. To get the actual label we now need to use another critical library the LabelHelp class, using this we are able to convert this label Id to the actual label itself.
In the final example, we are looking at data entities which are fairly similar to the menu items process we used in the previous example. We are again able to use the MetadataSupport class to get back a data entity object and then use the LabelHelper class to get the label of the object.
I have also provided a text copy of the code above.
public static void GetMetadataLabels() { Microsoft.Dynamics.Ax.Xpp.Dictionary dict = new Microsoft.Dynamics.Ax.Xpp.Dictionary(); string tableAOTName = "VendTable"; string tableLabel = dict.Tableobject(dict.Tablename2id(tableAOTName)).Label(); //tableLabel = Vendors string menuItemAOTName = "VendTableListPage"; string menuItemLabel = Microsoft.Dynamics.Ax.Xpp.LabelHelper.GetLabel( Microsoft.Dynamics.Ax.Xpp.MetadataSupport.GetMenuItemDisplay(menuItemAOTName).Label); //menuItemLabel = All vendors string dataEntityAOTName = "VendVendorEntity"; string dataEntityLabel = Microsoft.Dynamics.Ax.Xpp.LabelHelper.GetLabel( Microsoft.Dynamics.Ax.Xpp.MetadataSupport.GetDataEntityView(dataEntityAOTName).Label); //dataEntityLabel = Vendors }
While there are certain things that still have to be done in X++, I hope this shows that you can do business logic within C#/.NET for D365FO.
Hi Alex, tried to use your code on D365FO version 8.1 Update 20, could not make it to work. I’ve added all references from your screenshot, still it could not find LabelHelper class at Microsoft.Dynamics.Ax.Xpp namespace.
Any ideas why?
Artem,
I just tested this on an 8.0 and 8.1 PU20 box and was able to successfully build using the same code/DLLs in both. Intellisense was still showing the LabelHelper namespace as well.
If you provide what errors you are getting, might be able to help diagnose/troubleshoot the issue.
Thank you for the reply, you could see my error here:
https://preview.ibb.co/fsLK0A/Screenshot-13-Nov-2018-11-09-41.jpg
I found what was the issue. I was missing one of the references: Microsoft.Dynamics.AX.Xpp.AxShared
Hi Alex, how can I get a list of all label files for all languages?
Vladimir,
You can use labelId2String method which takes in a language parameter, here’s an example: https://community.dynamics.com/ax/b/axdaily/posts/getting-label-text-in-different-languages
can you help me to get list of objects against label id in X++ (similar to AOT find reference)
You can use the same MetadataSupport library in X++ like you can in C#, here is an example: http://dev.goshoom.net/2016/11/new-metadata-api/
Hello,
How can I get all label id for all layers from label text?
Eiso,
You should be able to use the ‘GetAllLabels’ method for this: