Import product category hierarchy from XML file in AX2012

Problem.

There is a need to import product hierarchy categories into Microsoft Dynamics AX2012 from file due to the large amount of data. It will also be easier to insert/update categories at all three enviroments (dev, test and production).

Tool.

Categories from file” import tool is already there and it is available in Microsoft Dynamics AX2012 R3. It runs ecoResCategoryImportCommonClass class, which already implements some validations and business logic. All it needs is an XML file with some mandatory data about inserted category and parent category.

Step by step.

It would be hard to prepare XML file by creating section by section manually, so we have to find an easier way to create our data file. The best method is to prepare Microsoft Excel file with XML maps, which can be sent to users to insert categories and categories’ details to import. To do this, we have to create a sample XML, which will be our source document in MS Excel.

1. Get XML Schema (XDS)

EcoResCategoryImportCommonClass class validates our XML file with XDS for EcoResCategoryDoc class (which extends AifDocument class), so we can save XML Schema to prepare XML import file. We can do this by running this job:

What we get after running this code is an XML Schema file with mandatory and non-mandatory fields, which our import file has to contain. Required fileds are category Code, category Name, category Parent (we need to deliver parent code to import categories) and fixed Class attribute set to „Entity”.

XML Schema for EcoResCategoryDoc is available HERE.

2. Create sample XML file

Now, we can create sample XML file and use it as a map in Microsoft Excel. If we prepare XML with mandatory tags, we will get something like this:

3. Create Microsoft Excel file for data preparation

To create *.xls file, which will contain data to import and will be easy to conver to XML format, we will use XML maps. After opening new Microsoft Excel document, let’s create headings for our attribute and tags, so we will have class, Code, Name and Parent.

Next step is to load XML map. We can do this in Developer tab, XML section by clicking Source button. Then, choose our sample XML and after it is loaded, simply drag and drop XML tags (from sidebar) on our columns. Our XML is now mapped.

If we want to add more categories, we add more rows in our table. Simple as that. If we are finished, we can export our data to XML file by click Export button (in Developer tab, XML section).

MS Excel IconMicrosoft Excel file with XML maps is available HERE.

4. Import hierarchy categories from XML file

Our XML file is ready, so we can try to import it into Microsoft Dynamics AX2012. We can go to Product information management > Setup > Categories > Category hierarchies and if our parent categories are already created, click Categories from file.

In dialog box, we do not select any inbound port, because our XML file is already transformed to AIF format and ready to go, so we select file to import and click OK. That’s all, we should now have all categories from XML in DAX2012.

Hierarchy categories import
Hierarchy categories import

If you think that there is a better way to solve this problem, please send me an email through „Contact” form or leave a comment, I will keep this post updated!

Leave a Reply