Friday, March 8, 2013

AX 2009 Access different company through AIF Doc Service AX2009

Hi,

 Normally base AIF webservice connects to the default company of the specified user. If you want to pass data to different Entities in AX we need to create Endpoints (AIF-> Endpoints) for the respective company and need to assign the user to access the same. And create the action policies for the required service.
Those services uses the wsHttpBinding binding.
Add below lines in AIF web config file.

<bindings>
      <wsHttpBinding>
        <binding name = "wsHttpWindowsAuthAif" />
      </wsHttpBinding>
    </bindings>

and change the respective service parameter
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="wsHttpWindowsAuthAif"

And sample code to create the customer, u can change the Endpoint id for respective company


using System;
using SysColl = System.Collections;
using SysSvcmod = System.ServiceModel;
using SysSvcmodChan = System.ServiceModel.Channels;
using SysXml = System.Xml;

namespace AifWebSvcFromCSharp22
{
    class ProgramClass
    {
        AXCustServ.CustomerServiceClient m_createCustomerServiceClient;
        static void Main(string[] args)
        {
            ProgramClass programClass;
            AXCustServ.CustomerServiceClient createCustomerServiceClient;
            try
            {
                createCustomerServiceClient = new AXCustServ.CustomerServiceClient();
                createCustomerServiceClient.ClientCredentials.Windows.ClientCredential.UserName = "username";
                createCustomerServiceClient.ClientCredentials.Windows.ClientCredential.Password = "pwd";
                createCustomerServiceClient.ClientCredentials.Windows.ClientCredential.Domain = "domainname";

                programClass = new ProgramClass(createCustomerServiceClient);
                programClass.SendMessage();
            }
            catch (SysSvcmod.FaultException<AifWebSvcFromCSharp22.AXCustServ.AifFault> aifFaultExcepn)
            {
                Console.Out.WriteLine(aifFaultExcepn.Code);
                Console.Out.WriteLine(aifFaultExcepn.ToString());
            }
            catch (Exception excepn)
            {
                Console.Out.WriteLine(excepn.ToString());
            }
        }

        protected ProgramClass(AXCustServ.CustomerServiceClient customerCreateServiceClient)
        {
            this.m_createCustomerServiceClient = customerCreateServiceClient;
        }


        protected void SendMessage()
        {
            SysSvcmod.OperationContextScope operContextScope;

            try
            {
                operContextScope = new SysSvcmod.OperationContextScope(m_createCustomerServiceClient.InnerChannel);
                using (operContextScope)
                {
                    this.PrepareHeader();
                    this.createCustomer();
                }
            }
            catch (Exception excepn)
            {
                Console.Out.WriteLine(excepn.ToString());
            }
        }


        protected void PrepareHeader()
        {
            Guid guidMesgId = Guid.NewGuid();
            Console.Out.WriteLine("MessageId = " + guidMesgId.ToString());
            this.SetHeader_MessageId(guidMesgId);
            this.SetHeader_DestinationEndpoint("Endpointname");//Specify the endpoint name here and below
            this.SetHeader_SourceEndpointUser(new Uri("urn:EndPointname"), "domain\username"); //specify the endpoint username
        }

        protected void SetHeader_MessageId(Guid guidMessageId)
        {
            System.Xml.UniqueId guidUniqueId1 = new System.Xml.UniqueId();
            SysSvcmod.OperationContext.Current.OutgoingMessageHeaders.MessageId = new System.Xml.UniqueId();
        }

        protected void SetHeader_SourceEndpointUser(Uri uriEndpoint, string sEndpointUser)
        {
            SysSvcmodChan.AddressHeader addressHeader;
            SysSvcmod.EndpointAddressBuilder epAddressBuilder;
            SysSvcmod.EndpointAddress epAddress1, epAddress2;

            addressHeader = SysSvcmodChan.AddressHeader.CreateAddressHeader("SourceEndpointUser"
                                                            , "http://schemas.microsoft.com/dynamics/2008/01/services"
                                                            , sEndpointUser);
            epAddress1 = new SysSvcmod.EndpointAddress(uriEndpoint, addressHeader);
            epAddressBuilder = new SysSvcmod.EndpointAddressBuilder(epAddress1);
            epAddress2 = epAddressBuilder.ToEndpointAddress();
            SysSvcmod.OperationContext.Current.OutgoingMessageHeaders.From = epAddress2;
        }

        protected void SetHeader_DestinationEndpoint(string sDestinationEndpointName)
        {
            SysSvcmodChan.MessageHeader messageHeader;

            messageHeader = SysSvcmodChan.MessageHeader.CreateHeader("DestinationEndpoint"
                    , "http://schemas.microsoft.com/dynamics/2008/01/services", sDestinationEndpointName);
            SysSvcmod.OperationContext.Current.OutgoingMessageHeaders.Add(messageHeader);
        }

        protected void createCustomer()
        {
            AXCustServ.AxdCustomer axdCustomer = new AXCustServ.AxdCustomer();
            AXCustServ.AxdEntity_CustTable tabCustomer = new AXCustServ.AxdEntity_CustTable();
                             
            tabCustomer.Currency = "USD";
            tabCustomer.CustGroup = 1";          
            tabCustomer.LanguageId = "En-us";
            tabCustomer.PartyType = AXCustServ.AxdEnum_DirPartyType.Organization;
            tabCustomer.AccountNum = "CUST_001";
            tabCustomer.CashDisc = "";          
            tabCustomer.Name = "Vendor Name";
            tabCustomer.NameAlias = "";
            tabCustomer.PaymTermId = "30";
            tabCustomer.Phone = "111";

            AXCustServ.AxdEntity_AddressRelationship addressRelation = new AXCustServ.AxdEntity_AddressRelationship();
            addressRelation.PartyId = "CUST_001";
            addressRelation.IsPrimary = AXCustServ.AxdExtType_DirIsPrimaryAddress.Yes;

            tabCustomer.AddressRelationship = new AXCustServ.AxdEntity_AddressRelationship[] { addressRelation };


            AXCustServ.AxdEntity_AddressRelationshipMap addRelationshipMap = new AXCustServ.AxdEntity_AddressRelationshipMap();
            AXCustServ.AxdEntity_CustAddress customerAddress = new AXCustServ.AxdEntity_CustAddress();

            customerAddress.type = AXCustServ.AxdEnum_AddressType.Invoice;
            customerAddress.ZipCode = "00346";          
            customerAddress.Phone = "6369";        


            addRelationshipMap.CustAddress = new AXCustServ.AxdEntity_CustAddress[] { customerAddress };
            addressRelation.AddressRelationshipMap = new AXCustServ.AxdEntity_AddressRelationshipMap[] { addRelationshipMap };
         
            axdCustomer.CustTable = new AXCustServ.AxdEntity_CustTable[] { tabCustomer };

            try
            {
                AXCustServ.EntityKey[] key = m_createCustomerServiceClient.create(axdCustomer);
            }
            catch (Exception e1)
            {
                Console.WriteLine("{0} Exception caught.", e1);
            }
        }
    }
}


I think i covered all :-)


Thanks,
K. Arunsubramaniam

4 comments:

  1. Interesting - the code sample above is C# is it not? Why did you choose this instead of X++?

    ReplyDelete
  2. Normally AIF is used for integration and that is done with AX & external application, so i have given in C#.

    ReplyDelete
  3. Hi Blogger, would have great opportunity for AX, .NET developer with depth experience in Web Services integration and .NET Web App development (Enteprise Portal). Kindly provide your contact email to explore more details. Greetings, RF

    ReplyDelete