Welcome to MSDN Blogs Sign in | Join | Help

Creating an IBF Compliant Web Service for Axapta

Yesterday I was requested to create an Information Bridge Framework 1.5 (IBF) compliant Web service that exposes customer information from Axapta.

 

Initially I thought that this wouldn't be easy. However it merely took me approximately 2 hours to develop a fully functional IBF solution for Axapta. I still find this pretty impressive given that I had no experience at all with programmatically interacting with Axapta.

 

The first and only thing I had to learn about was Axapta’s Business Connector. Searching the Web got me to following two useful MSDN articles:

I was surprised by the simplicity of the - yet powerful - programming model provided by the Axapta Business Connector.

 

These two articles gave me enough information to create the IBF solution for Axapta. Allow me to share my code with you.

 

As a first step I created four classes in .NET using C#:

  • Customer
  • Customers
  • CustomerReferenceById
  • CustomersReferenceByCompanyName

Here you’ve the Customer class:

 

[XmlRoot("Customer", Namespace="Axapta2IBF")]

public class Customer

{

       private string _accNum;

       private string _name;

       private string _street;

       private string _state;

       private string _city;

       private string _zip;

       private string _country;

 

       [XmlElement]

       public string AccountNumber {get{return _accNum;} set{_accNum = value;}}

       [XmlElement]

       public string Name {get{return _name;} set{_name = value;}}

       [XmlElement]

       public string Street {get{return _street;} set{_street = value;}}

       [XmlElement]

       public string State {get{return _state;} set{_state = value;}}

       [XmlElement]

       public string City {get{return _city;} set{_city = value;}}

       [XmlElement]

       public string Zip {get{return _zip;} set{_zip = value;}}

       [XmlElement]

       public string Country {get{return _country;} set{_country = value;}}

}

 

Here you’ve the Customers class:

 

public class Customers : CollectionBase

{

       // Get Customer as a specific index

       public Customer this[int index]

       {

             get {return (Customer) List[index];}

             set {List[index] = value;}

       }

       public int Add(Customer item)

       {

             return List.Add(item);

       }

       public int IndexOf(Customer item)

       {

             return List.IndexOf(item);

       } 

       public void Insert(int index, Customer item)

       {

             List.Insert(index, item);

       }

       public void Remove(Customer item)

       {

             List.Remove(item);

       } 

       public bool Contains(Customer item)

       {

             return List.Contains(item);

       }

       public void CopyTo(Customer[] destination, int index)

       {

             List.CopyTo(destination, index);

       }

}

 

Here you’ve the CustomerReferenceById class:

 

[XmlRoot("CustomerReferenceById", Namespace="Axapta2IBF")]

public class CustomerReferenceById

{

       private string _accNum;

 

       [XmlAttribute]

       public string AccountNumber {get{return _accNum;} set{_accNum = value;}}

}

 

And finally, here you’ve the CustomersReferenceByName class:

 

[XmlRoot("CustomersReferenceByCompanyName", Namespace="Axapta2IBF")]

public class CustomerReferenceByCompanyName

{

       [XmlAttribute]

       public string companyName;

}

 

The second – and last – step is the actual implementation of the Web service methods.

 

To make things clean I first created a class that defines the interface that is implemented by the Web service:

 

public interface ICustomerInformationAccess

{

       Customer GetCustomerById(CustomerReferenceById customerId);

       Customers GetCustomersByName(CustomerReferenceByCompanyName customerName);

}

 

The Web service will implement this interface:

 

public class AxaptaWebService : System.Web.Services.WebService, ICustomerInformationAccess {…}

 

The implementation of the first method looks like this:

 

[WebMethod]

public Customer GetCustomerById(CustomerReferenceById accNum)

{

       AxaptaCOMConnector.Axapta axapta = new AxaptaCOMConnector.AxaptaClass();

       AxaptaCOMConnector.IAxaptaObject axaptaObj;

 

       axapta.Logon("Admin", "", "", "");

       axaptaObj = axapta.CreateObject("WebServiceCusInfo",null,null, null,null,null,null);

       axapta.Refresh();

 

       Customer cust = new Customer();

 

       cust.AccountNumber = accNum.AccountNumber;

       cust.Name = (string) axaptaObj.Call("retCustName", accNum.AccountNumber, null,null,null,null,null);

       cust.Street = (string) axaptaObj.Call("retCustStreet", accNum.AccountNumber, null,null,null,null,null);

       cust.City = (string) axaptaObj.Call("retCustCity", accNum.AccountNumber, null,null,null,null,null);

       cust.State = (string) axaptaObj.Call("retCustState", accNum.AccountNumber, null,null,null,null,null);

       cust.Zip = (string) axaptaObj.Call("retCustZip", accNum.AccountNumber, null,null,null,null,null);

       cust.Country = (string) axaptaObj.Call("retCustCountry", accNum.AccountNumber, null,null,null,null,null);

 

       axapta.Logoff();

 

       return cust;

}

 

In order to make this work you need to implement the X++ code as described in the first MSDN article mentioned above.

 

The implementation for the second method looks like this:

 

[WebMethod]

[return:XmlArray(ElementName="Customers", Namespace="Axapta2IBF")]

public Customers GetCustomersByName(CustomerReferenceByCompanyName name)

{

       Customers customers = new Customers();

       AxaptaCOMConnector.Axapta axapta = new AxaptaCOMConnector.AxaptaClass();

       axapta.Logon("Admin", "", "", "");

      

       AxaptaCOMConnector.IAxaptaObject axaptaQuery;

       axaptaQuery = axapta.CreateObject("Query", null, null, null, null, null, null);

                   

       int custTable = 77;

       AxaptaCOMConnector.IAxaptaObject axaptaDataSource;

       axaptaDataSource = (AxaptaCOMConnector.IAxaptaObject) axaptaQuery.Call("AddDataSource", custTable, null, null, null, null, null);

 

       AxaptaCOMConnector.IAxaptaObject axaptaQueryRun;

       axaptaQueryRun = axapta.CreateObject("QueryRun", axaptaQuery, null, null, null, null, null);

                   

       AxaptaCOMConnector.IAxaptaRecord axaptaRecord;

       while (axaptaQueryRun.Call("Next", null, null, null, null, null, null) != null)

       {

              axaptaRecord = (AxaptaCOMConnector.IAxaptaRecord) axaptaQueryRun.Call("GetNo", 1, null, null, null, null, null);

             if (axaptaRecord.Found == false) break;

             string custName = (string) axaptaRecord.get_field("Name");

             if (custName.ToLower().IndexOf(name.companyName.ToLower()) >= 0)

             {

                    Customer c = new Customer();

                    c.Name = custName;

                    c.AccountNumber = (string) axaptaRecord.get_field("AccountNum");

                    c.Street = (string) axaptaRecord.get_field("Street");

                    c.City = (string) axaptaRecord.get_field("City");

                    c.State = (string) axaptaRecord.get_field("State");

                    c.Zip = (string) axaptaRecord.get_field("ZipCode");

                    c.Country = (string) axaptaRecord.get_field("Country");

                    customers.Add(c);

             }           

       }

                   

       axapta.Logoff();

 

       return customers;

}

 

All the code above is pretty straight forward. To complete the IBF solution no more code is involved.

 

You only need to go through six IBF Wizards in Visual Studio .NET:

  1. Importing the Web service
  2. Creating a Customer entity and it’s default view
  3. Creating a Customers entity and it’s default view
  4. Creating a Region control library to display the Customer’s default view
  5. Creating a Reference List Region for the Customers’s entity default view
  6. Creating a Search page

In a next posting I’ll provide you with the actual screen shots of these 6 Wizards.

Published Thursday, April 21, 2005 12:20 PM by yvesk

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Creating an IBF Compliant Web Service for Axapta

Tuesday, October 11, 2005 10:32 PM by Anders Grønli
Hi
This is great stuff you have posted here. Very good. Keep up the good work :-)

# re: Creating an IBF Compliant Web Service for Axapta

Thursday, June 01, 2006 10:31 AM by Derek
Hi.
Thanks for posting your experience with this. I also had to develop a customer info WS. My current dillema is deployment of it to a production server when I can't build the Axapta Business Connector into the app. Also, we have three different enviornments (live, test, dev) and this has required me to partition the BC for each enviornment. Thus, I will need to install the app 3 times but configured differently.
I am curious as to your experience with deployment of the WS?
Best,
Derek

# COM, .NET, and RecIDs ! | keyongtech

Thursday, January 22, 2009 12:19 AM by COM, .NET, and RecIDs ! | keyongtech

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker