AIF Transformer: Import
CSV files using AIF Transformers
How to build a .Net
Transformer using XSD?
In this post I am going to show you how to
create a .Net transform assembly that can be called from Application
Integration Framework (AIF) in AX 2012 R3. In this scenario, the component
takes the contents of a comma-separated values (CSV) file that contains Bills
of materials service (BOM) data and converts it into XML.
This
walkthrough illustrates the following tasks:
·
Create
the .NET assembly
·
Create
a program to test the assembly
·
Import
.dll to AX
·
Configure
Transformer to AIF inbound service.
The .NET assembly contains the class that gets
called from the AIF pipeline when processing a request. This class must
implement the “ITransform” interface
which is located in the assembly file
Microsoft.Dynamics.IntegrationFramework.dll. This interface contains a single
method called Transform that takes three parameters: an input stream, an output
stream and a configuration string.
·
Input
stream – the request message passed in as a stream.
·
Output
stream – the request message after it has been transformed passed out of the
method as a stream.
·
Configuration
string – a string of data that the developer can specify to be used within the
Transform method. This parameter enables you to pass data that is related to
the transformation but which is not part of the request stream into the class
To add a reference to the ITransform assembly
1.
Open
Visual Studio and create a new project by selecting File > New > Project.
2.
In
the Project types tree, select Visual C# > Windows and click the Class
Library template.
3.
Type
a project name such as AssemblyTransformTest and click OK.
4.
In
Solution Explorer, right-click References and select Add Reference.
5.
On
the Add Reference form, click the Browse tab and browse to assembly in the
client\bin directory. For example, C:\Program Files\Microsoft Dynamics
AX\60\Client\Bin\ Microsoft.Dynamics.IntegrationFramework.
6.
Click
OK.
To create the transform assembly
1.
Open
Class1.cs, and then add the following code.
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
System.Text;
using
System.IO;
using
System.Xml;
using
Microsoft.Dynamics.IntegrationFramework.Transform;
namespace
MkTransformerBOMCSV2XML
{
public class Class1 : ITransform
{
public void Transform(System.IO.Stream input, System.IO.Stream output, string configuration)
{
// Code will be implemented
in next steps.
}
}
}
2.
Creating
Class from XSD.
Ø Create two folders in above created
project lets name them as “BOMXSD” and “SharedXSD”.
Ø C:\Users\mgudidevuni\Documents\Visual
Studio 2013\Projects\MkTransformerBOMCSV2XML\BOMXSD
Ø C:\Users\mgudidevuni\Documents\Visual
Studio 2013\Projects\MkTransformerBOMCSV2XML\SharedXSD
Ø Save the XSD files (BillsOfMaterials_BOMInterface.xsd
and SharedTypes.xsd) to above said folders.
Ø We can get the above XSDs from
(System administration/SetUp/Services and AIF/Inboun.
Ø Open the Visual studio command
prompt and navigate to the directory where the XSD files were saved.
Ø xsd BomBillsOfServices.xsd
SharedTypes.xsd /classes /namespace:XSDTrans.BOMXSD
Ø The output from the above command,
is the VS class BOM_SharedTypes.cs
Ø Copy this file BOM_SharedTypes.cs
and place this file in the folder BOMXS.
Ø Now we need the schema which has the Envelope defined. This is defined in a file called Message.xsd which can be found at the following location
C:\ProgramFiles\MicrosoftDynamicsAX\60\Server\bin\Application\Share\Include\
> Copy Message.xsd to the above folder "BOMXSD"
Ø xsd Message.xsd /classes
/namespace:LooneyTrans.SharedXSD
Ø The above command will create the
code for the Envelope.
Ø Place the output file Message.cs in the folder SharedXSD of the project.
We need to add
this envelope which sets the Action for the document. The code should now look
like this
C:\Users\mgudidevuni\Documents\Visual
Studio 2013\Projects\MkTransformerBOMCSV2XML
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
using
System.Text;
using
System.IO;
using
System.Xml;
using
Microsoft.Dynamics.IntegrationFramework.Transform;
using XSDTrans.BOMXSD;
using XSDTrans.SharedXSD;
namespace
AVABOMCSV2XML
{
public class Class1 : ITransform
{
public void Transform(System.IO.Stream input, System.IO.Stream output, string configuration)
{
StreamReader sreader = new StreamReader(input);
string[] dataArray;
string[] fieldArray;
bool Version;
int i;
// Get the element names from
the headings
string
lineIn = sreader.ReadLine();
fieldArray = lineIn.Split(',');
//BOM collection
List<AxdEntity_BOMVersion> LbomVersion = new List<AxdEntity_BOMVersion>();
List<AxdEntity_BOMTable> LbomTable = new List<AxdEntity_BOMTable>();
List<AxdEntity_BOM> LbomItem = new List<AxdEntity_BOM>();
lineIn = sreader.ReadLine();
Version = true;
while (lineIn != null)
{
// Loop through each
line of data in the file.
dataArray = lineIn.Split(',');
if (Version)
{
//Write BOMVersion
elements
AxdEntity_BOMVersion BOMVersion = new AxdEntity_BOMVersion();
BOMVersion.BOMId = dataArray[0];
BOMVersion.Name = dataArray[1];
BOMVersion.ItemId = dataArray[2];
BOMVersion.FromQty = Int32.Parse(dataArray[3]);
BOMVersion.FromQtySpecified = true;
BOMVersion.InventDimId = "403-000031";
BOMVersion.Active = AxdExtType_BOMVersionActive.Yes;
BOMVersion.ActiveSpecified = true;
LbomVersion.Add(BOMVersion);
//Write BOMTable
elements
AxdEntity_BOMTable BOMTable = new AxdEntity_BOMTable();
BOMTable.BOMId = dataArray[0];
BOMTable.Name = dataArray[1];
BOMTable.SiteId = dataArray[4];
LbomTable.Add(BOMTable);
Version = false;
lineIn = sreader.ReadLine();
continue;
}
AxdEntity_BOM BOMItem = new AxdEntity_BOM();
BOMItem.BOMId = dataArray[0];
BOMItem.ItemId = dataArray[5];
BOMItem.BOMQty = Int32.Parse(dataArray[6]);
BOMItem.BOMQtySpecified = true;
BOMItem.UnitId = dataArray[7]; ;
BOMItem.InventDimId = "403-000031";
LbomItem.Add(BOMItem);
lineIn = sreader.ReadLine();
}
sreader.Close();
AxdBillsOfMaterials bom = new AxdBillsOfMaterials();
bom.BOMVersion = LbomVersion.ToArray();
bom.BOMVersion[0].BOMTable = LbomTable.ToArray();
bom.BOMVersion[0].BOMTable[0].BOM = LbomItem.ToArray();
//Serialize item data to xml
string
StringWriter stringWriterItems = new StringWriter();
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(AxdBillsOfMaterials));
serializer.Serialize(stringWriterItems, bom);
//Put it inside the envelope
EnvelopeType envelope = new EnvelopeType();
HeaderType header = new HeaderType()
{
MessageId = Guid.NewGuid().ToString("B"),
Action = @"http://schemas.microsoft.com/dynamics/2008/01/services/BillsofMaterialsService/create"
};
envelope.Header = header;
BodyType bodyType = new BodyType();
//Load item data xml string
into an XML Document
XmlDocument xmlDocumentItems = new XmlDocument();
xmlDocumentItems.LoadXml(stringWriterItems.ToString());
//Set the body to the Item
XML Document
bodyType.MessageParts
= xmlDocumentItems.DocumentElement;
envelope.Body = bodyType;
//serilalize to outputstream
System.Xml.Serialization.XmlSerializer mainSerializer = new System.Xml.Serialization.XmlSerializer(typeof(EnvelopeType));
mainSerializer.Serialize(output, envelope);
}
}
}
Now we are
all set to go, compile the project and load the resulting DLL into AX.
To import the .NET assembly
1.
Copy the assembly file (DLL) to the server bin directory. This
directory varies depending on your installation, but it may be named something
like C:\Program
Files\Microsoft Dynamics AX\60\Server\MicrosoftDynamicsAX\Bin.
2.
Copy the assembly file (DLL) to the client bin directory. This
directory varies depending on your installation, but it may be named something
like C:\Program
Files\Microsoft Dynamics AX\60\Client\Bin.
3.
Open Microsoft Dynamics AX and type CTRL+SHIFT+W to open a new
development workspace.
4.
Select Tools > Application
Integration Framework > Manage transforms.
5.
In the Manage transforms form,
type CTRL+N to add a new transform.
6.
In the Name field,
enter a unique name of up to 30 characters.
7.
In the Description field,
enter a description of the transform of up to 1,000 characters.
8.
In the Type field,
select .NET
Assembly.
9.
Click Load, navigate to the assembly that you copied to the
client bin directory, and then click Open.
10.
In the Class field,
select the class in the assembly that contains the transform you want to
import.
11.
Type CTRL+S to save the transform.
To use
the transform, you first must add the transform file to the list of available
transforms by using the Manage
transforms form.
1.
Click System
administration > Setup > Services and Application
Integration Framework > Inbound ports.
2.
Deactivate the integration port named BOMService, if it is still active. (You created this integration
port in the previous walkthrough.)
3.
Click Inbound
transforms. Then, on the Action Pane, click Manage transforms.
4.
Add the Library that we created and give it a Name and Description.
5.
In the Type field,
select .NET Assembly.
6.
Click Load, navigate to the assembly that you copied
to the client bin directory, and then click Open.
7.
In the Class field,
select the class in the assembly that contains the transform you want to
import.
8.
Type CTRL+S to save the transform.
This is for my reference only.
for more info Refer to below link:
Have a gr8 day...
Gudidevuni, Mallikarjun
!!!!