The sample below creates a new customer using the eConnect .NET assembly and in-memory serialization.
To run the following code on your machine:
- Install the latest version of the eConnect 11 SDK.
- Create a new Console Application in Microsoft Visual Studio.
Add references to these dynamic link libraries which are located by default in C:\Program Files (x86)\Microsoft Dynamics\eConnect 11.0\API\. (Ignore the x86 if you are using a 32-bit system.)
- Microsoft.Dynamics.GP.eConnect.dll
- Microsoft.Dynamics.GP.eConnect.Serialization.dll
Replace the Program.cs class in the project for the new class below.
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using Microsoft.Dynamics.GP.eConnect;
using Microsoft.Dynamics.GP.eConnect.Serialization;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Beginning integration test.");
using (eConnectMethods eConnectMethods = new eConnectMethods())
{
try
{
Console.WriteLine("Creating a new customer.");
// Modify the connection string for your environment.
string connectionString = @"data source=localhost; initial catalog=TWO; integrated security=SSPI";
// Create the customer.
taUpdateCreateCustomerRcd customer = new taUpdateCreateCustomerRcd();
customer.CUSTNMBR = "TEST001";
customer.CUSTNAME = "Test Customer";
// Assign the customer to a new master customer type.
RMCustomerMasterType customerMasterType = new RMCustomerMasterType();
customerMasterType.taUpdateCreateCustomerRcd = customer;
// Assign the master customer type to a new collection of master customer types.
RMCustomerMasterType[] customerMasterTypes = { customerMasterType };
// Serialize the master vendor type in memory.
eConnectType eConnectType = new eConnectType();
MemoryStream memoryStream = new MemoryStream();
XmlSerializer xmlSerializer = new XmlSerializer(eConnectType.GetType());
// Assign the master customer types to the eConnectType.
eConnectType.RMCustomerMasterType = customerMasterTypes;
// Serialize the eConnectType.
xmlSerializer.Serialize(memoryStream, eConnectType);
// Reset the position of the memory stream to the start.
memoryStream.Position = 0;
// Create an XmlDocument from the serialized eConnectType in memory.
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(memoryStream);
memoryStream.Close();
// Call eConnect to process the XmlDocument.
eConnectMethods.CreateEntity(connectionString, xmlDocument.OuterXml);
Console.WriteLine("Successfully created customer {0}.", customer.CUSTNMBR);
}
catch (Exception ex)
{
Console.WriteLine("Exception occured: " + ex.Message);
}
finally
{
eConnectMethods.Dispose();
}
}
Console.WriteLine("Integration test complete." +
Environment.NewLine +
Environment.NewLine);
Console.WriteLine("Press <Enter> to continue...");
Console.ReadLine();
}
}
1 comment:
Thanks for this informative post. Is there eConnect code to link a customer to a vendor as well?
Post a Comment