CRM plugins RRS feed

  • Question

  • I am new in mscrm 2011

    Give sample screanios  for plugins or logic


    Thursday, January 19, 2012 7:35 PM


All replies

  • If you download the SDK, you can find many good code samples in the \sdk\samplecode\cs\plug-ins folder. Below is the code for one of them (not mine - just to be clear):


    namespace Microsoft.Crm.Sdk.Samples
     public class AccountNumberPlugin: IPlugin
      /// <summary>
            /// A plugin that auto generates an account number when an
            /// account is created.
      /// </summary>
            /// <remarks>Register this plug-in on the Create message, account entity,
            /// and pre-event stage.
            /// </remarks>
            public void Execute(IServiceProvider serviceProvider)
                // Obtain the execution context from the service provider.
                Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)

                // The InputParameters collection contains all the data passed in the message request.
       if (context.InputParameters.Contains("Target") &&
        context.InputParameters["Target"] is Entity)
                    // Obtain the target entity from the input parmameters.
        Entity entity = (Entity)context.InputParameters["Target"];

                    // Verify that the target entity represents an account.
                    // If not, this plug-in was not registered correctly.
                    if (entity.LogicalName == "account")
                        // An accountnumber attribute should not already exist because
                        // it is system generated.
         if (entity.Attributes.Contains("accountnumber") == false)
                            // Create a new accountnumber attribute, set its value, and add
                            // the attribute to the entity's attribute collection.
          Random rndgen = new Random();
                            entity.Attributes.Add("accountnumber", rndgen.Next().ToString());
          // Throw an error, because account numbers must be system generated.
                            // Throwing an InvalidPluginExecutionException will cause the error message
                            // to be displayed in a dialog of the Web application.
                            throw new InvalidPluginExecutionException("The account number can only be set by the system.");

    Cordially, W.G.Ryan - www.williamgryan.mobi
    Thursday, January 19, 2012 8:34 PM
  • Hi

     Plugin requires bit of understanding before looking at the code.

     there are plenty of structure example on net aswell on CRM Sdk

     my recommendation would be going thru the tutorial

     I m enclosing few links which will help you to understand better

    If you really want to get deep in Plugin the David Yack book "Rapid Application Developer 4.0"  is the utlimate guide





     if the response answered your question, please take a minute and mark the response as an answer.

    Thursday, January 19, 2012 10:57 PM

    Plugins are can be run when an entity is updated.  


    you use plugins to run code which directly will make changes to the data on the CRM 2011 database using the SDK.  

    You can specify when plugins run (e.g. before validation, after the record has been created).

    An example could be you want to calculate the tax for an order.

    When the user saves the order you could check all the products and then calculate the tax for each product or calculate the tax for the whole order and then save the value to the order form.

    When the order refreshed the user would then see the tax value.

    Ben Hosking
    Check out my CRM Blog
    Linked-In Profile
    Follow Me on Twitter!
    Friday, January 20, 2012 12:20 PM
  • Hi
    Did this help?
    Please make sure to mark as answer to the response that helped you get through. This will help others with similar problem identify the answer and also close this thread as resolved.
    Saturday, January 21, 2012 3:34 PM