Generate Javascript soap xml calls from C# .Net code

In CRM customizations while writing java scripts we sometimes need to accomplish requirements involving SOAP XML calls like fetching data, setting state of an entity or calling a workflow etc.

With this blog, I would like to showcase the way of writing effective SOAP XML calls easily for all possible requests that are available with the “Execute()” method of organization service.

All of us would have downloaded the CRM 2011/2013 SDK. Now, the SDK comes with some tools and one of them is the SOAP LOGGER tool. The tool can be found at:

Installed SDK Path\sdk\samplecode\cs\client\soaplogger

First, we need to create our own project in Visual Studio and include the reference to the project “soaplogger.sln” from the above location to our newly created project:

1

Now, the way we write code for creating an organization service proxy and “Execute” a request, we need to write code using the soap logger. For example, the below code is for executing a workflow.

public class Helper
    {
        /// <summary>
        /// Execute Function to execute the request and
        /// create the respective Javascript equivalent
        /// </summary>
        public void ExecuteFunction()
        {
            using (StreamWriter output = new StreamWriter(“output.txt”))
            {
                OrganizationServiceProxy serviceProxy = CreateOrganizationServiceProxy();

                var uri = new Uri(“https://xrmadvice.api.crm.dynamics.com/XRMServices/2011/Organization.svc”);
                SoapLoggerOrganizationService slos = new SoapLoggerOrganizationService(uri, serviceProxy, output);

                /*Code over here is to execute the workflow. It can be changed
                 for Entity Create/Update/SetState etc.*/
                try
                {
                    ExecuteWorkflowRequest request = new ExecuteWorkflowRequest()
                    {
                        WorkflowId = new Guid(“4017B077-B13D-45A0-B222-2D9B7CF086CF”),
                        EntityId = new Guid(“01987DAC-1A06-E411-93F2-005056AB5C1B”)
                    };

                    ExecuteWorkflowResponse response =
                        (ExecuteWorkflowResponse)slos.Execute(request);
                }
                catch (Exception ex)
                {
                }
            }
        }

        /// <summary>
        /// Create Organization Proxy Service
        /// </summary>
        /// <returns></returns>
        private OrganizationServiceProxy CreateOrganizationServiceProxy()
        {
            var clientCredentials = new ClientCredentials();
            clientCredentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
            clientCredentials.UserName.UserName = “bohnnie@xrmadvice.onmicrosoft.com”;
            clientCredentials.UserName.Password = “**********”;
            var uri = new Uri(“https://xrmadvice.api.crm.dynamics.com/XRMServices/2011/Organization.svc”);
            Uri homeRealmUri = null;
            var serviceProxy = new OrganizationServiceProxy(uri, homeRealmUri, clientCredentials, null);
            return serviceProxy;
        }

}

2

In the above code we need to specify the name of the text file where the javascript output of the equivalent code would be the output. In my example i have given the file name as “output.txt”.

After this, execute the code and ensure that it executes successfully!

Now navigate to the respective “Debug” folder and the file “output.txt” would contain the respect java script equivalent for the C# .Net code.

3

All that we need to do now is copy paste the java script code from the “OutPut in JavaScript” section and our SOAP Call is done.

4

The above code need not be changed while switching across environments because it is generic however there are few considerations that I have listed below.

**Few considerations here while using the java script code:

5

1. The code currently uses “ActiveXObject” for creating the HTTP Request. This works only in IE, so better is to use XMLHttpRequest() so that it is compatible with all the browsers.

var xmlHttpRequest = new XMLHttpRequest();

2. Also while calling the xmlHttpRequest.Open() call, make sure to pass the complete Organization Service URL as sometimes the relative path might hinder with the authentication. So, we change this as:

xmlHttpRequest.open(“POST”, Xrm.Page.context.getClientUrl() + “/XRMServices/2011/Organization.svc/web”, false);

So, by appending “Xrm.Page.context.getClientUrl() “ resolves this issue.

As shown above with the SOAP Logger tool you can create any SOAP XML call by comfortaby writing C# .Net code and not worrying about java script!

Leave a comment