Sunday, October 27, 2013

Microsoft Dynamics CRM 2013: Actions walkthrough for .Net and JavaScript developers

I believe that almost every developer for Dynamics CRM faced with scenario when he/she had to invoke some complex logic on a server side, pass parameters and receive a response back. Before CRM 2013 we used workarounds similar to this one. Once Dynamics CRM 2013 was released we have got solution that would not require ‘magic’.
Following posts describe how to create Actions in CRM side:
This is how my Custom Action look like:

Usage of Custom Actions in .Net:
1. Actions are available for Early binding. To get Early Binding classes you need to run CrmSvcUtil (you can find this utile in SDK package, bin folder) using following command line:
CrmSvcUtil.exe /url:http://crm:5555/contoso/XRMServices/2011/Organization.svc /out:Classes.cs /namespace:CustomActions /serviceContextName:CrmServiceContext /generateActions

My command sample can be used for OnPremise deployments. Detailed information regarding generating of Early Binding classes for all types of deployments you can find in this article.

Add generated Classes.cs to your project and start using it:

Sample of code that instantiates request, executes it and receives result:

slick_CustomActionsTestRequest request = new slick_CustomActionsTestRequest()
    BoolInArgument = true,
    DateTimeInArgument = DateTime.Now,
    DecimalInArgument = decimal.Zero

slick_CustomActionsTestResponse response = service.Execute(request) as slick_CustomActionsTestResponse;
//Processing of response

 2. If you don’t like or don’t want to use Early binding you case use OrganizationRequest/OrganizationResponse. Sample of code:

OrganizationRequest request = new OrganizationRequest("slick_CustomActionsTest");
request["BoolInArgument"] = true;
request["DateTimeInArgument"] = DateTime.Now;
request["DecimalInArgument"] = decimal.Zero;

OrganizationResponse response = service.Execute(request);

bool boolvalue = (bool)response.Results["BoolOutArgument"];

Usage of Actions in JavaScript:

1. Prepare Soap Xml request. The easiest way to do it is to use SoapLogger application that is the part of SDK package. Detailed information you can find here. Sample of code that captures request and response follows:

using (StreamWriter output = new StreamWriter("output.txt"))
    SoapLoggerOrganizationService slos = new SoapLoggerOrganizationService(serverConfig.OrganizationUri, service, output);

    OrganizationRequest r = new OrganizationRequest("slick_CustomActionsTest");
    r["BoolInArgument"] = true;
    r["DateTimeInArgument"] = DateTime.Now;
    r["DecimalInArgument"] = decimal.Zero;
    r["EntityCollectionInArgument"] = new EntityCollection()
        EntityName = "account"
    r["EntityInArgument"] = new Entity("account");
    r["EntityReferenceInArgument"] = new EntityReference("account", Guid.Empty);
    r["FloatInArgument"] = double.Parse("0");
    r["IntegerInArgument"] = 0;
    r["MoneyInArgument"] = new Money(decimal.Zero);
    r["PicklistInArgument"] = new OptionSetValue(1);
    r["StringInArgument"] = "";


In output.txt file you can find following:

<s:Envelope xmlns:s=''>
    <Execute xmlns='' 
      <request i:type='b:slick_CustomActionsTestRequest' 
        <a:Parameters xmlns:c=''>
            <c:value i:type='d:boolean' xmlns:d=''>true</c:value>
            <c:value i:type='d:dateTime' xmlns:d=''>2013-10-27T01:08:29.0439804+03:00</c:value>
            <c:value i:type='d:decimal' xmlns:d=''>0</c:value>
        <a:RequestId i:nil='true' />

this is the Soap Xml request you will use.

2. Use this Soap request with CustomAcionExecutor.js library I’ve developed. Sample of usage:

        requestXml: requestXml,
        async: true,
        successCallback: function(result){
        errorCallback: function (e) {

 In result you will get parsed response from server:

Source code of CustomActionExecutor.js:

No comments:

Post a Comment