
Plugins
Generate Early bound Class using CrmSvcUtil.Exe in CRM
CrmSvcUtil.exe is a command-line code generation tool for use with Microsoft Dynamics CRM. This tool generates Early bound class in C# with Custom entities and attributes. Provides Intellisense Support.
Follow the below steps to create early bound class,
Step 1: Download Latest CRM SDK.
Step 2: Open Run and type cmd.
Step 3: In SDK\bin, we can see crmsvcutil.exe. Now provide the path “CRM\SDK\bin” in command prompt.
Example: I have downloaded the CRM SDK on my desktop. So, path in command prompt will be like below,
Step 4: Now we have to execute crmsvcutil.exe from command prompt with the following command,
CrmSvcUtil.exe /url:<Organization Service> out:<Class Name>.cs /username:"<User Id>" /password:"<Password>" /namespace:<Namespace> /serviceContextName:<Context Name>
Parameter | Description |
Organization Service | Provide Organization Service. Goto Microsoft Dynamics CRM ->Settings -> Customizations -> Organization Service. |
Class Name | Provide Output Class Name. Ex: D:\OutputFile.cs |
User Id | Provide Online 365 User Id or On-Premise domain\userid . |
Password | Provide Password. |
Namespace | Provide Namespace name. |
Context Name | Provide Context Name. |
Example:
CrmSvcUtil.exe /url:https://OrgName.api.crm5.dynamics.com/XRMServices/2011/Organization.svc /out:C:\Users\arunpotti\Desktop\EarlyBound.cs /username:"arunpotti@OrgName.onmicrosoft.com" /password:"password" /namespace:earlybound /serviceContextName:ServiceContext
Step 5: Paste the command line in command prompt and click on enter to generate class file.
Please provide your valuable comments on this article.
FetchXML Formatter Tool
It is a Light weight windows application and this tool will be helpful when you are extensively working with FetchXML in Javascript / C# code to get desired result.
Functionality:
It will take FetchXML from Advance Find as an input and convert that into desired format, which can be used into Javascript / C# for further coding.
Pros:
- No Need to spend time for FetchXML formatting
- It is easy to use.
- Works for both Javascript and C# code
Example:
Let us take a simple example of the below FetchXML,
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false"> <entity name="contact"> <attribute name="fullname" /> <attribute name="telephone1" /> <attribute name="emailaddress1" /> <attribute name="contactid" /> <order attribute="fullname" descending="false" /> <filter type="and"> <condition attribute="ownerid" operator="eq-userid" /> <condition attribute="statecode" operator="eq" value="0" /> </filter> </entity> </fetch>
Provide this as an input to FetchXML formatter Tool, 1. Check Javascript radio button and click on Format to see the below output,
"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+ " <entity name='contact'>"+ " <attribute name='fullname' />"+ " <attribute name='telephone1' />"+ " <attribute name='emailaddress1' />"+ " <attribute name='contactid' />"+ " <order attribute='fullname' descending='false' />"+ " <filter type='and'>"+ " <condition attribute='ownerid' operator='eq-userid' />"+ " <condition attribute='statecode' operator='eq' value='0' />"+ " </filter>"+ " </entity>"+ "</fetch>"
2. Check C# radio button and click on Format to see the below output,
@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'> <entity name='contact'> <attribute name='fullname' /> <attribute name='telephone1' /> <attribute name='emailaddress1' /> <attribute name='contactid' /> <order attribute='fullname' descending='false' /> <filter type='and'> <condition attribute='ownerid' operator='eq-userid' /> <condition attribute='statecode' operator='eq' value='0' /> </filter> </entity> </fetch>"
Click Here to download the FetchXML Formatter Tool Exe file
Screen Shots:
Retrieve records using Fetch XML & Java Script in CRM 2011/13
Task: Retrieve Arun Potti Business Phone on onload of Contact record in Contact Entity using FetchXML & Javascript
Solution:
Step 1: Goto http://xrmsvctoolkit.codeplex.com/, and download the Zip folder. Unzip XrmServiceToolkit the folder.
Step 2: Open the XrmServiceToolkit folder, you can find the below Javascript files.
Goto Microsoft Dynamics CRM –> Settings –> Customization –> Webresources.
Create jquery, json2 and XrmServiceToolkit javascript webresources. While creating web resources browse for the respective files and provide the path of XRMServiceToolkit.
Step 3: Add all 3 files to the contact entity,
Step 4: Goto Microsoft Dynamics CRM –> Sales –> Contacts, click on Advance find button. Create new criteria as shown below by clicking on New button,
Step 5: Click on Download Fetch XML button, to get FetchXML code. You can see the below XML,
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false"> <entity name="contact"> <attribute name="fullname" /> <attribute name="telephone1" /> <attribute name="contactid" /> <order attribute="fullname" descending="false" /> <filter type="and"> <condition attribute="fullname" operator="eq" value="Arun Potti" /> </filter> </entity> </fetch>
Step 6: We have to change the format of the above FetchXML to use in Javascript.
Use FetchXML Formatter Tool to modify the format.
To Know more about FetchXML Formatter Tool click Here
After modifying its looks like the below,
"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+ " <entity name='contact'>"+ " <attribute name='fullname' />"+ " <attribute name='telephone1' />"+ " <attribute name='contactid' />"+ " <order attribute='fullname' descending='false' />"+ " <filter type='and'>"+ " <condition attribute='fullname' operator='eq' value='Arun Potti' />"+ " </filter>"+ " </entity>"+ "</fetch>"
Step 7: Now create new_contactJscript Webresource and paste the below code,
function contactOnload() { var contactFetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+ " <entity name='contact'>"+ " <attribute name='fullname' />"+ " <attribute name='telephone1' />"+ " <attribute name='contactid' />"+ " <order attribute='fullname' descending='false' />"+ " <filter type='and'>"+ " <condition attribute='fullname' operator='eq' value='Arun Potti' />"+ " </filter>"+ " </entity>"+ "</fetch>"; var contactRecords = XrmServiceToolkit.Soap.Fetch(contactFetchXML); if (contactRecords.length > 0) { if (contactRecords[0].attributes.telephone1 != undefined) alert(contactRecords[0].attributes.telephone1.value); } }
Step 8: Add the new_contactJscript Webresource to Form Libraries on contact Form, and add the contactOnload function to Onload Event,
Step 9: Click on Ok. Save & Publish the contact Entity.
Step 10: Open the existing contact record to see the below pop up,
Sample Plugin using Developer Toolkit
Will see a simple example, to develop and deploy plugin in CRM Online 2013 using SDK Developer Tool Kit in Visual Studio 2012.
Task: On Post Create of Account, Task should create.
Solution:
If you do not have Developer Tool Kit on your Visual Studio, Click Here to know about Installation process.
Step 1: Open Visual Studio 2012. Click on New Project. Goto Visual C# -> Dynamics CRM -> Dynamics CRM 2013 Package. Provide Name “CrmPackage”.
This package is helpful for Deploying Plugin from Visual Studio.
Step 2: Please follow the below steps,
- Provide Discovery details of your organization. (I am using CRM Online 2013)
Get this details going to CRM Web Client,
Microsoft Dynamics CRM -> Settings -> Customizations -> Developer Resources -> Discovery Service. - Select Protocol to HTTPS and click on Connect.
- Provide User name & Password and click on Log on.
- After successful login to CRM, select Organization and Solution Name. (I have Created a Separate Solution called Plugins for demo)
Step 3: Right Click on Solution ‘CrmPackage’ (1 project) and Select Dynamics CRM 2013 Plug-in Library. Provide Name as “PostAccountCreate” and click ok.
Step 4: On the Left Hand Side, we can see CRM Explorer. Goto Microsoft -> Entities -> Account -> Create Plug-in.
Step 5: Select Message, Run in Context, Pipeline Stage and Execution Mode as mentioned below. Click OK.
Step 6: You may see the below alert, click on Yes to All
Step 7: Have to provide Strong Key to the Plugin. For that Go to Solution Explorer on the right hand side, Right Click On “PostAccountCreate” and click on Properties .
Step 8: Follow the below steps for Strong Key set up.
- Go to Signing and Select Sign the assembly.
- Choose a strong name key file: to <New…>.
- Provide Key file name: as “PostAccountCreate” and uncheck the Protect my key file with a password.
- Select Signature Algorithm.
Step 9: Goto Solution Explorer, double click on “PostAccountCreate.cs” file. You can see the below code,
Step 10: Copy and paste the below code in method ExecutePostAccountCreate after //TODO: Implement your custom Plug-in business logic.
// TODO: Implement your custom Plug-in business logic. IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; ITracingService tracingService = localContext.TracingService; if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { Entity entity = (Entity)context.InputParameters["Target"]; if (entity.LogicalName == "account") { try { Entity followup = new Entity("task"); followup["subject"] = "Send e-mail to the new customer."; followup["description"] = "Follow up with the customer. Check if there are any new issues that need resolution."; followup["scheduledstart"] = DateTime.Now.AddDays(7); followup["scheduledend"] = DateTime.Now.AddDays(7); followup["category"] = context.PrimaryEntityName; // Refer to the account in the task activity. if (context.OutputParameters.Contains("id")) { Guid regardingobjectid = new Guid(context.OutputParameters["id"].ToString()); string regardingobjectidType = "account"; followup["regardingobjectid"] = new EntityReference(regardingobjectidType, regardingobjectid); } // Create the task in Microsoft Dynamics CRM. tracingService.Trace("FollowupPlugin: Creating the task activity."); service.Create(followup); } catch (FaultException ex) { throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex); } } }
Step 11: Finally the ExecutePostAccountCreate method looks like the below,
Step 12: Save All. Click on F6 to build the solution.
Step 13: Goto Solution Explorer, and right Click on Solution ‘CrmPackage’ (2 projects). Click on Deploy Solution to deploy into Online CRM 2013.
Step 14: After Successful Deployment, you can check it, whether plugin registered on Account or not, by going to Plugin Registration Tool or Go to CRM Explorer on left hand side of visual studio, you can find the below
Step 15: Create Account record in CRM Online. After Account creation, Task is created, you can check it in under Social Pane -> Activities,
Good Luck :):):)
Overview of Plugins
What is a Plugin?
A plug-in is custom business logic (code) that you can integrate with Microsoft Dynamics CRM Online or OnPremise to modify or augment the standard behavior of the platform.
Event execution pipeline
The Microsoft Dynamics CRM event processing subsystem executes plug-ins based on a message pipeline execution model. A user action in the Microsoft Dynamics CRM Web application or an SDK method call by a plug-in or other application results in a message being sent to the organization Web service. The message contains business entity information and core operation information. The message is passed through the event execution pipeline where it can be read or modified by the platform core operation and any registered plug-ins.
Note: While there are several Web services hosted by the Microsoft Dynamics CRM platform, only events triggered by the organization and OData endpoints can cause plug-ins to execute.
Architecture and related components
The following figure illustrates the overall architecture of the Microsoft Dynamics CRM platform with respect to both synchronous and asynchronous event processing.
Points to Remember:
- The event execution pipeline processes events either synchronously or asynchronously.
- The platform core operation and any plug-ins registered for synchronous execution are executed immediately.
- Synchronous plug-ins that are registered for the event are executed in a well-defined order.
- Plug-ins registered for asynchronous execution are queued by the Asynchronous Queue Agent and executed at a later time by the asynchronous service.
- Regardless of whether a plug-in executes synchronously or asynchronously, there is a 2 minute time limit imposed on the execution of a (message) request. If the execution of your plug-in logic exceeds the time limit, a System.TimeoutException is thrown. If a plug-in needs more processing time than the 2 minute time limit, consider using a workflow or other background process to accomplish the intended task.
Pipeline Stages:
Event Pipeline Stages are 4, in which we can register plugin in 3 Stages.
Images
Pre Image: A pre-image is a snapshot of the entity’s attributes before the core operation.
Availability of Pre Images for the Plugin stages,
Post Image: A post-image is a snapshot of the entity’s attribute after the core operation.
Availability of Post Images for the Plugin stages,