Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX (Archived)

AX 7 Visual Studio Extension, add method to selected class

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hello community,

I am trying to write a VS extension using the AX developer tools add-in project template.  The goal of the add-in is to add method(s) to the selected class.

Does anyone have example code, documentation or guidance on how to achieve this?

I have followed along with Peter's example from the tech conf recordings and am using the automation interfaces to understand members/methods of the selected class, but I do not understand how to create or modify metadata using those objects.

Best Regards,

*This post is locked for comments

  • Diego Mancassola Profile Picture
    Diego Mancassola 227 on at
    RE: AX 7 Visual Studio Extension, add method to selected class

    Thanks Martin, precious as always.

    That's works!

  • Martin Dráb Profile Picture
    Martin Dráb 230,354 Most Valuable Professional on at
    RE: AX 7 Visual Studio Extension, add method to selected class

    Try using DesignMetaModelService.Instance.CurrentMetaModelService instead (DesignMetaModelService class is defined in Microsoft.Dynamics.Framework.Tools.MetaModel.Core assembly).

  • Diego Mancassola Profile Picture
    Diego Mancassola 227 on at
    RE: AX 7 Visual Studio Extension, add method to selected class

    Hello, i'm trying to develop a new addin but i cannot reference to ServiceLocator

    Already try to add some references but without find the right reference.

    Thanks

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: AX 7 Visual Studio Extension, add method to selected class

    Martin,

    I found it at C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\4xwsot0h.pp4 on my AX VM

    Thanks

  • Martin Dráb Profile Picture
    Martin Dráb 230,354 Most Valuable Professional on at
    RE: AX 7 Visual Studio Extension, add method to selected class

    It's a installed as a part of the VS extension for AX (at c:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\buffodo3.ljy\ on my AX VM).

    Alternatively, it can be found in the bin subfolder of the packages folder (e.g. c:\AOSService\PackagesLocalDirectory\Bin\).

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: AX 7 Visual Studio Extension, add method to selected class

    Hi.  How to install Microsoft.Dynamics.AX.Metadata.dll ? Because I can't find Microsoft.Dynamics.AX.Metadata in my Reference Manager.

    Thanks

  • Martin Dráb Profile Picture
    Martin Dráb 230,354 Most Valuable Professional on at
    RE: AX 7 Visual Studio Extension, add method to selected class

    Yes, you need a reference to Microsoft.Dynamics.AX.Metadata. In general, you will probably need a plenty of references when developing add-ins, but which ones depends on what exactly you'll do.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: AX 7 Visual Studio Extension, add method to selected class

    Martin,

    Sorry for the delayed response.

    I needed to add a reference to Microsoft.Dynamics.AX.Metadata.dll in order to make use of AxMethod/AxReturnType class.

    Can you confirm the addition of this reference to the project was needed?

    Thanks again for your help.

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,354 Most Valuable Professional on at
    RE: AX 7 Visual Studio Extension, add method to selected class

    This solution isn't ideal, but I think it's a good start. It's a bit challenging without any documentation.

    /// <summary>
    /// Adds a new method (with a hard-coded name and empty body) to the selected class.
    /// </summary>
    /// <param name="c">The class to which the method will be added.</param>
    /// <remarks>
    /// The method is added to the underlying file, but not to the designer.
    /// Visual Studio will detect the change and asks the user if the file should be reloaded.
    /// Another approach is needed to update the designer directly.
    /// </remarks>
    public override void OnClick(AddinDesignerEventArgs e)
    {
        try
        {
            ClassItem c = e.SelectedElement as ClassItem;
    
            if (c != null)
            {
                AddMethodToFile(c);
            }
        }
        catch (Exception ex)
        {
            CoreUtility.HandleExceptionWithErrorMessage(ex);
        }
    }
    
    void AddMethodToFile(ClassItem c)
    {
        AxClass axClass = (AxClass)c.GetMetadataType();
    
        // Add the method to the class
        axClass.AddMethod(BuildMethod());
    
        // Prepare objects needed for saving
        var metaModelProviders = ServiceLocator.GetService(typeof(IMetaModelProviders)) as IMetaModelProviders;
        var metaModelService = metaModelProviders.CurrentMetaModelService;
        // Getting the model will likely have to be more sophisticated, such as getting the model of the project and checking
        // if the object has the same model.
        // But this should do for demonstration.
        
        ModelInfo model = DesignMetaModelService.Instance.CurrentMetadataProvider.Classes.GetModelInfo(axClass.Name).FirstOrDefault<ModelInfo>();
    
        // Update the file
        metaModelService.UpdateClass(axClass, new ModelSaveInfo(model));
    }
    
    // Create a new method with empty body
    private AxMethod BuildMethod()
    {
        AxMethod axMethod = new AxMethod()
        {
            Name = "myNewMethod",
            ReturnType = new AxMethodReturnType()
        };
        
        axMethod.Source = axMethod.GetDefaultMethodSource(4);
    
        return axMethod;
    }

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

Congratulations 2024 Spotlight Honorees!

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December!

Congratulations to our December super stars! 🥳

Get Started Blogging in the Community

Hosted or syndicated blogging is available! ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,622 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,354 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans