Search code examples
javascriptc#dynamics-crmdom-eventsdynamics-crm-2016

How to retrieve name of Javascript events attached to form in CRM 2016 server side


I am trying to retrieve All the Javascript events/libraries attached to the form of particular entity from the server side.

I am able to retrieve the all the forms of that particular entity by using query expression

 QueryExpression q = new QueryExpression("systemform");
            q.ColumnSet = new ColumnSet() { AllColumns = true };
            q.Criteria.AddCondition(new ConditionExpression("objecttypecode", ConditionOperator.Equal, "account"));
            EntityCollection ec = serviceProxy.RetrieveMultiple(q);

I just need to know the Javascript library attached to OnLoad or OnSave Events in CRM form.


Solution

  • Querying the formxml attribute on the form will give you what you are looking for. For e.g. to get all the attribute, event and function names on contact form:

    var attributeEventsDetails =
                    XDocument.Parse(xrmServiceContext.SystemFormSet.FirstOrDefault(form => form.Name == "contact").FormXml)
                        .Descendants("event")
                        .Select(descendants =>
                            new
                            {
                                AttributeName = descendants.Attribute("attribute"),
                                EventName = descendants.Attribute("name"),
                                FunctionName =
                                    descendants.Descendants()
                                        .FirstOrDefault(childDesc => childDesc.Name == "Handler")
                                        .Attribute("functionName")
                            });