Showing posts with label AIF. Show all posts
Showing posts with label AIF. Show all posts

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

Tuesday, December 11, 2012

AX 2012 AX 2009 Create AX Customer through AIF Customer Document Service AX2009 AX2012

AX Customer Document service,

 This is the piece of code to create the AX 2009 customer through AIF customer document service.

It creates Customer in customer and address for the same.


AxdCustomer axdCustomer = new AxdCustomer();
            CustomerServiceClient CstService = new CustomerServiceClient();

            AxdEntity_CustTable tabCustomer = new AxdEntity_CustTable();           
            AxdEntity_AddressRelationship addressRelation = new AxdEntity_AddressRelationship();
            AxdEntity_AddressRelationshipMap addRelationshipMap = new AxdEntity_AddressRelationshipMap();
            AxdEntity_CustAddress customerAddress = new AxdEntity_CustAddress();

            CstService.ClientCredentials.Windows.ClientCredential.UserName = "domainusername";
            CstService.ClientCredentials.Windows.ClientCredential.Password = "password";
            CstService.ClientCredentials.Windows.ClientCredential.Domain = "domainname";

            axdCustomer.SenderId = "company/entity name";
            axdCustomer.CustTable = new AxdEntity_CustTable[] { tabCustomer };

            tabCustomer.AccountNum = "TEST11";
            tabCustomer.Currency = "USD";
            tabCustomer.CustGroup = "DOMESTIC";
            tabCustomer.LanguageId = "En-us";
            tabCustomer.PartyType = AxdEnum_DirPartyType.Organization;           
            tabCustomer.CashDisc = "1/2% 10 Days";
            //tabCustomer.Name = "TEST11";
            //tabCustomer.NameAlias = "TEST11";
            tabCustomer.PaymTermId = "Net 30";
            tabCustomer.TaxGroup = "TAX-POD";
                       
            tabCustomer.AddressRelationship = new AxdEntity_AddressRelationship[] { addressRelation };
            addressRelation.IsPrimary = AxdExtType_DirIsPrimaryAddress.Yes;           
            addressRelation.IsPrimarySpecified = true;
            addressRelation.PartyId = "TEST11";
            addressRelation.Shared = AxdExtType_DirIsSharedAddress.Yes;
            addressRelation.SharedSpecified = true;

            addressRelation.AddressRelationshipMap = new AxdEntity_AddressRelationshipMap[] { addRelationshipMap };

            addRelationshipMap.CustAddress = new AxdEntity_CustAddress[] { customerAddress };
            customerAddress.type = AxdEnum_AddressType.Invoice;
            customerAddress.typeSpecified = true;
            customerAddress.ZipCode = "YORK";
            customerAddress.Phone = "6369";           
                     
            try
            {
                EntityKey[] key = CstService.create(axdCustomer);
            }
            catch (Exception e1)
            {
                Console.WriteLine("{0} Exception caught.", e1);
            }



Try and Enjoy AXingg,,

Thanks,
K. Arunsubramaniam