web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

AX 7 Visual Studio Extension, add method to selected class

(0) ShareShare
ReportReport
Posted on by

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

I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    238,751 Most Valuable Professional on at

    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;
    }
  • Community Member Profile Picture
    on at

    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.

  • Martin Dráb Profile Picture
    238,751 Most Valuable Professional on at

    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
    on at

    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
    238,751 Most Valuable Professional on at

    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
    on at

    Martin,

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

    Thanks

  • Diego Mancassola Profile Picture
    673 on at

    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

  • Martin Dráb Profile Picture
    238,751 Most Valuable Professional on at

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

  • Diego Mancassola Profile Picture
    673 on at

    Thanks Martin, precious as always.

    That's works!

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Sagar Suman Profile Picture

Sagar Suman 2 Super User 2026 Season 1

#1
Alexey Lekanov Profile Picture

Alexey Lekanov 2

#1
Pratik Bhosle Profile Picture

Pratik Bhosle 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans