Tag Archives: dynamics crm plugin plugins javascript client script

Call plugin from Javascript in Microsoft Dynamics CRM.

Wanted to share with you all a very interesting way to call plugins from you client side code in Microsoft Dynamics CRM to perform complex operations.Recently in our project  we had a requirement to determine duplicates for an entity based on some very complex business rules with integration from external system. Surely we can call the write some code in a plugin and register it in the pre-validation of Create/ update message of the entity. However our requirement was bit more complex. Not only we had to inform the user of the duplicates but also shown the user, the list of all the duplicates in a webresource popup from where the user can select one of the duplicates. And on top of that all this should be synchronous.


So. What can we do? Can’t we leverage the power of server side plugins to write some complex business logic. yes we can. Following is the approach we took.


  • 1. We created a entity in CRM which would act as the dummy entity in our purpose. We created a field called ‘new_data’ with type of string which would store the data that we need to pass to server side to process the logic of duplicate detection.
  • Next we create a fetchxml for this dummy entity. Lets say we need to pass account name to the server side to determine the duplicate. So we created a fetchxml in the below format where we are passing the value of account name as a condition for the new_data field.

var fetchString = ‘<fetch version=”1.0″ output-format=”xml-platform” mapping=”logical” distinct=”false”>’ +
                                  ‘<entity name=”new_dummy”>’ +
                                    ‘<attribute name=”new_dummyid” />’ +
                                    ‘<filter type=”and”>’ +
                                      ‘<condition attribute=”statecode” operator=”eq” value=”0″ />’ +
                                      ‘<condition attribute=”new_data” operator=”eq” value=”‘ + accountname+ ‘” />’ +
                                    ‘</filter>’ +
                                  ‘</entity>’ +


  • Register plugin classes in the pre and post of the RetrieveMultiple message for the new_dummy entity.
  • Now we execute this fetch from the client side. To check out how can execute a fetch call from client side in Microsoft Dynamics CRM, please refer to my following blog post – http://debajmecrm.com/2014/08/01/how-to-execute-retrievemultiple-request-from-javascript/
  • As soon as you fire this fetch, the plugins registered on the pre and post would be triggerd. The following is the code that I have written in the pre-retrivemultiple of this dummy entity and interecepting the query and taking out the account name from the condition,

var organizationServiceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            var pluginContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            var tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            var orgService = organizationServiceFactory.CreateOrganizationService(pluginContext.UserId);

            if (pluginContext.InputParameters[“Query”] is FetchExpression
                && pluginContext.MessageName.Equals(“RetrieveMultiple”, StringComparison.OrdinalIgnoreCase)
                && pluginContext.PrimaryEntityName.Equals(“new_dummy”, StringComparison.OrdinalIgnoreCase)
                && pluginContext.InputParameters.ContainsKey(“Query”))
                var fetchExpression = (FetchExpression)pluginContext.InputParameters[“Query”];
                // Convert the FetchXml to QueryExpression.
                FetchXmlToQueryExpressionRequest req = new FetchXmlToQueryExpressionRequest();
                req.FetchXml = fetchExpression.Query;
                FetchXmlToQueryExpressionResponse resp = (FetchXmlToQueryExpressionResponse)orgService.Execute(req);
                var queryExpression = resp.Query;


                foreach (var condition in queryExpression.Criteria.Conditions)
                    if (condition.AttributeName.Equals(“new_data”))

                       // perform what you need to do here. You can get the value passed in the new_data field using condition.values[0]




  • Wondering how would you pass the data back to the caller client side code. Well that’s fairly easy. In the post of retrievemultiple of the dummy entity, populate the BusinessEntityCollection property of the Entity. Below is the code to do the same. And in the responsexml in your client side you can view the entire data. We parsed the response xml to get the list of duplicates and show it to the user in the webresource.

var organizationServiceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            var pluginContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            var tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            var orgService = organizationServiceFactory.CreateOrganizationService(pluginContext.UserId);

                var entityCollection = new EntityCollection();

                var entity = new Entity(“de_dummy”);
                entity[“new_dummy1”] = // populate whatever you want to return.


                pluginContext.OutputParameters[“BusinessEntityCollection”] = entityCollection;

With this approach you can virtually accomplish anything. One more advantage of this approach is you can run these plugins under higher privileged users and perform operation with that user.

Hope this helps!