How to Use a .NET Project Within a D365FO Solution

 



step1 : create a x++ project


step2 : Select Class Library (.NET Framework)



step3: Enter a Project Name and Location, then select .NET Framework 4.7.2 – the reason for this is that if we want to reference any Microsoft DLLs we need to be sure our solution matches the project type and version that the DLLs are built against (which in the case of Microsoft DLLs is .NET Framework 4.7.2)



step4 : Next we need to create a reference between our X++ project and our .NET project, right click on the References and ‘Add Reference’


step5 : Next select your .NET project from the Projects tab


step6 : Now you will see your .NET project is referenced by your X++ project


So Now I Can Reference Microsoft DLLs from the .NET Class?

1) Right click on the References tab on your .NET project -> Add Reference

2) Click on Browse -> Then the Browse button

3) Navigate to the PackagesLocalDirectory folder (on my development onebox this is located at K:\AosService\PackagesLocalDirectory) then look for the bin folder. This folder contains the built DLLs that are included with your D365FO instance.


In the example scenario I came up with below you can see I’ve added the Microsoft.Dynamics.AX.Xpp.Support library to my project and then added a ‘using’ statement for Microsoft.Dynamics.Ax.Xpp. This allows me to use the MetadataSupport class in my project to get a listing of all menu item display names and then perform a LINQ statement to find menu items that contain a search term, I then use the JsonConvert class from Newtonsoft.Json nuget package to serialize the object before returning it.


code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Dynamics.Ax.Xpp;
using Newtonsoft.Json;

namespace FPTestsNET
{
    public static class FPTestNETClass
    {
        public static string MenuItemDisplaySearch(string search)
        {
            var menuItemNames = MetadataSupport.GetMenuItemDisplayNames();
            var menuItemSearchObjects = menuItemNames.Where(mi => mi.ToUpper().Contains(search.ToUpper()));
            return JsonConvert.SerializeObject(menuItemSearchObjects);

        }
    }
}

One thing to note here is that by default X++ projects are built against .NET version 4.6, so we need to update this to 4.7.2. To do this we need to find the .rnrproj file which is the X++ project file, open it in a text editor and change the TargetFrameworkVersion from v4.6 -> v4.7.2:



And How Do I Call This .NET Method From X++?

Since we are referencing our .NET project in our X++ solution we can add a using statement to our X++ class to the FpTestNet project and then reference the .NET class and method within our X++ code. Note here that you have to follow the syntax of: <.NetClass>::<.NetMethod>. When we execute the code you can see the correct menu item displays are returned in a JSON format that we can then utilize within our X++ project or return to a service operation endpoint to an external solution.




Comments

Popular posts from this blog

how to post trade agreement journals automatically using x++ code

How to Create a wizard in x++ d365

x++ code to submit and approve and reject the invent movement workflow in d365 F&O