Philippsen's Blog

Everyday findings in my world of .net and related stuff

AX2012 AIF – CallContext

Posted by Torben M. Philippsen on October 8, 2014

Working with AIF on the Dynamics AX 2009 platform you had to create the SoapHeader manually. In that you had to specify the destination endpoint and the source endpoint user in order to being able to target a specific company using and maybe using a specific AX user. It could look like something similar to this:

 

public static class SoapHeader

    {

 

        /// <summary>

        /// Helper method – adds a SOAP Header defining the destination endpoint (local endpoint) in Dynamics AX

        /// </summary>

        /// <param name="nameOfEndpoint">The name of the local endpoint</param>

        public static void SetDestinationEndpoint(string _nameOfEndpoint)

        {

            OperationContext.Current.OutgoingMessageHeaders.Add(MessageHeader.CreateHeader("DestinationEndpoint", "http://schemas.microsoft.com/dynamics/2008/01/services&quot;, _nameOfEndpoint));

        }

 

        /// <summary>

        /// Helper method – adds a SOAP Header defining the source endpoint name and the source endpoint user to use

        /// </summary>

        /// <param name="sourceEndpointName">the name of the source endpoint</param>

        public static void SetSourceEndpointAndUser(string _sourceEndpointName, string _userName)

        {

            //string userName = HttpContext.Current.User.Identity.Name.ToString(); //returns the current user and domian – eg. egdk\tomph

            var addressHeader = AddressHeader.CreateAddressHeader("SourceEndpointUser", "http://schemas.microsoft.com/dynamics/2008/01/services&quot;, _userName);

            var addressBuilder = new EndpointAddressBuilder(

            new EndpointAddress(new Uri("urn:" + _sourceEndpointName), addressHeader));

            var endpointAddress = addressBuilder.ToEndpointAddress();

            OperationContext.Current.OutgoingMessageHeaders.From = endpointAddress;

        }

 

 

    }

            //call the webservice’s find method

            try

            {

                //SOAP header info

                using (new OperationContextScope(client.InnerChannel))

                {

                    //CREATE HEADER TO SET SOURCE ENDPOINT

       //this assumes that a endpoint (inside AX) with the selected (dataareaid in this case) name has
       been created for all companies

                    SoapHeader.SetDestinationEndpoint(ddlDataAraeId.SelectedValue);

                    //CREATE HEADER TO SET TARGET COMPANY

       //this assumes that a local endpoint (inside ax) with the name [ddlDataAraeId.SelectedValue] has
       been configured and is associated with a company that exists in dynamics ax

                    SoapHeader.SetSourceEndpointAndUser("Default", Helper.GetCurrentUser());

                    //submit the request and retrieve the respons

                }   

                response = client.find(qc);

                enumerEmplTable = response.EmplTable.GetEnumerator();

            }

 

 

Now in AX 2012 AIF you can simply specify the CallContext – which I may say is a h… of a lot easier…

 

            //create the AX call context in order to being able to define Ax company and submitting user

            CallContext axContext = new CallContext();

            axContext.Company = ddlDataAraeId.SelectedValue;

            axContext.LogonAsUser = Helper.GetCurrentUser();

 

            try

            {

                #if DEBUG

                CreateXmlMessageTextFileFromCreate(axdEGF_HRMWebRecruitment, Guid.NewGuid().ToString());

                #endif

                keys = client.create(axContext, axdEGF_HRMWebRecruitment);

                txtResult.Text = "SUCCESS: " + keys[0].KeyData[0].Field + " = " + keys[0].KeyData[0].Value;

            }   

Thank You MS for making my life just a little bit easier:-)

You may find it relevant to look at the this peace of documention on technet.

Advertisements

Sorry, the comment form is closed at this time.

 
%d bloggers like this: