Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Unanswered

How to get labelID for all layers from label text though code?

(0) ShareShare
ReportReport
Posted on by 81

Hello,

I would like to get the Label ID from all Layers that contain text entered from form controls.

community.dynamics.com/.../1296437

I am implementing it with reference to the above link,
A compilation error has occurred as follow.

compileerror.jpg
Also, the sample code in this link only allows you to get the LabelID for the specified Layer.


Any sample code or any advice would be greatly appreciated.

  • Martin Dráb Profile Picture
    Martin Dráb 230,846 Most Valuable Professional on at
    RE: How to get labelID for all layers from label text though code?

    Your question suggests that you didn't add the reference, which explains why your code can't compile. The compiler can't load the assembly containing objects that you're using in your code.

    Open you project, right-click References node, choose Add Reference... and add AOSKernel.dll from the bin folder of your modelstore (e.g. k:\AosService\PackagesLocalDirectory\bin\).

  • akshi Profile Picture
    akshi 81 on at
    RE: How to get labelID for all layers from label text though code?

    The using statement is declared as follows. Declaring a Kernel gives an error again.

    using Microsoft.Dynamics.AX.Metadata.Kernel;
    using Microsoft.Dynamics.AX.Metadata.MetaModel;
    using Microsoft.Dynamics.AX.Metadata.Storage;
    using Microsoft.Dynamics.AX.Metadata.Storage.Runtime;
    using Microsoft.Dynamics.AX.Metadata.Core.MetaModel;
    using Microsoft.Dynamics.Ax.Xpp.MetadataSupport;
    using Microsoft.Dynamics.ApplicationPlatform.Environment;
    public static class TestGetLabelIdClass
    {
        /// 
        /// Class entry point. The system will call this method when a designated menu 
        /// is selected or when execution starts and this class is set as the startup class.
        /// 
        /// The specified arguments.
        public static void main(Args _args)
        {
            var environment = Microsoft.Dynamics.ApplicationPlatform.Environment.EnvironmentFactory.GetApplicationEnvironment();
            var runtimeConfiguration = new RuntimeProviderConfiguration(environment.Aos.PackageDirectory);
            var metadataProvider = new MetadataProviderFactory().CreateRuntimeProviderWithExtensions(runtimeConfiguration);
    
            //IMetaModelService service = new MetaModelServiceFactory().Create(Accessor::MetadataProvider);
            var labelFiles = metadataProvider.GetLabelFileNames();
    
            Set prefixes = new Set(Types::String);
            var enumerator = labelFiles.GetEnumerator();
            while (enumerator.MoveNext())
            {
                int positionOf_ = strFind(enumerator.Current, '_', 1, strLen(enumerator.Current));
                str prefix = subStr(enumerator.Current, 1, positionOf_ - 1);
                prefixes.add(prefix);
            }
        }
    
    }

    Could you tell me how to add a reference?

    Thanks.

  • Martin Dráb Profile Picture
    Martin Dráb 230,846 Most Valuable Professional on at
    RE: How to get labelID for all layers from label text though code?

    I see several bugs:

    1. 'using' statements needs to be defined in the class, not inside a method together with your code.
    2. You're calling GetApplicationEnvironment() as if it was an instance method, not a static one.
    3. There it seems to be no GetLabelFileNames() method.

    I think you gave us wrong information about which thread you tried to follow. I believe you meant   .  Didn't you forget to add a reference to AOSKernel.dll?

  • akshi Profile Picture
    akshi 81 on at
    RE: How to get labelID for all layers from label text though code?

    If you have any helpful blogs or information about how to get the AOT label file list, please share it.

  • akshi Profile Picture
    akshi 81 on at
    RE: How to get labelID for all layers from label text though code?

    Hello Martin,

    Thanks for your info.

    I fixed it as below, but the reference error still occurs.

    Can you share how to add reference?

    using Microsoft.Dynamics.AX.Metadata.MetaModel;
    using Microsoft.Dynamics.AX.Metadata.Storage;
    using Microsoft.Dynamics.AX.Metadata.Storage.Runtime;
    using Microsoft.Dynamics.AX.Metadata.Core.MetaModel;
    
    var environment = Microsoft.Dynamics.ApplicationPlatform.Environment.EnvironmentFactory.GetApplicationEnvironment();
            var runtimeConfiguration = new RuntimeProviderConfiguration(environment.Aos.PackageDirectory);
            var metadataProvider = new MetadataProviderFactory().CreateRuntimeProviderWithExtensions(runtimeConfiguration);
    
            //IMetaModelService service = new MetaModelServiceFactory().Create(Accessor::MetadataProvider);
            var labelFiles = metadataProvider.GetLabelFileNames();
    
            Set prefixes = new Set(Types::String);
            var enumerator = labelFiles.GetEnumerator();
            while (enumerator.MoveNext())
            {
                int positionOf_ = strFind(enumerator.Current, '_', 1, strLen(enumerator.Current));
                str prefix = subStr(enumerator.Current, 1, positionOf_ - 1);
                prefixes.add(prefix);
            }

    Referror.jpg

    Do it make a project with .Net and refer to it from the X project?

  • Martin Dráb Profile Picture
    Martin Dráb 230,846 Most Valuable Professional on at
    RE: How to get labelID for all layers from label text though code?

    The first error says that you're referring to Microsoft.Dynamics.AX.Metadata.Kernel for some reason, but you haven't added a reference to an assembly containing this namespace.

    Regarding Accessor, maybe you can get rid of it completely. I don't know why you're using your current approach, but what about switching to new MetadataProviderFactory().CreateRuntimeProviderWithExtensions()?

  • akshi Profile Picture
    akshi 81 on at
    RE: How to get labelID for all layers from label text though code?

    Hi

    I created it with reference to the above link to get the label file. As a result, the above error occurs.

    using Microsoft.Dynamics.AX.Metadata.Kernel;
    using Microsoft.Dynamics.AX.Metadata.Service;
    Public static class TestGetLabelIdClass
    {
        public static TestLabelIdTmpTable getLabelIdInfo(Str4095 _labelTxt)
        {
            TestLabelIdTmpTable testLabelIdTmpTable;
         
            IMetaModelService service = new MetaModelServiceFactory().Create(Accessor::MetadataProvider);
            var labelFiles = service.GetLabelFileNames();
    
            Set prefixes = new Set(Types::String);
            var enumerator = labelFiles.GetEnumerator();
            while (enumerator.MoveNext())
            {
                int positionOf_ = strFind(enumerator.Current, '_', 1, strLen(enumerator.Current));
                str prefix = subStr(enumerator.Current, 1, positionOf_ - 1);
                prefixes.add(prefix);
            }
    
            return testLabelIdTmpTable;
        }
    
    }

    First, I am trying to get all label file list.

  • André Arnaud de Calavon Profile Picture
    André Arnaud de Cal... 291,971 Super User 2025 Season 1 on at
    RE: How to get labelID for all layers from label text though code?

    Hi Akshi,

    You might have forgotten a reference, like suggested by the error. Instead asking coding, maybe it would be better if you share the class you created. You can use the rich formatting and then Insert  > Code to paste the code and share what you have done so far.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,971 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,846 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans