CRM 2011–Retrieve Plugin

So you want to write a plugin that executes every time a user opens a record.

Usually this would be frowned upon because this code could easily present a performance issues, especially if the data you’re gathering is hosted on an external resource.

In the off-change you do need to do this, here is the solution:


First, create a new Plugin and sign the assembly with a key:

public void Execute(IServiceProvider serviceProvider)
    // Obtain the execution context from the service provider.
    Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)

    if (context.Depth == 1)
        IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

        // Obtain the target entity from the input parmameters.
        EntityReference entity = (EntityReference)context.InputParameters["Target"];

        ColumnSet cols = new ColumnSet(
                             new String[] { "lastname", "firstname", "address1_name" });

        var contact = service.Retrieve("contact", entity.Id, cols);

        if (contact != null)
            if (contact.Attributes.Contains("address1_name") == false)
                Random rndgen = new Random();
                contact.Attributes.Add("address1_name", "first time value: " + rndgen.Next().ToString());
                contact["address1_name"] = "i already exist";


Next, hop over to the Plugin Registration Tool (included with the CRM SDK) and register the plugin as follows:



What you’ll end up with in this example is:

On first open of the record:


On all future opens of the record:




This has not been tested with CRM 2011 Online.  Use at your own risk, your mileage may vary. Smile


// Dave

This entry was posted in Development and tagged . Bookmark the permalink.

12 Responses to CRM 2011–Retrieve Plugin

  1. Holly says:

    Hi Dave,

    Thanks for posting this code. I wasn’t trying to execute a plug-in when a record was opened. Instead, I was having trouble with the syntax to retrieve attribute values when a record is updated so I could pass the values to a web service to create a folder in SharePoint. I followed your code example and solved my problem!

    Thanks again,

  2. Mark Pitts says:

    Hello Dave,

    What version of CRM 2011 did you test this with?

    In a pre-operation stage the “Target” is not defined. The following results in an exception.



  3. Dave Corun says:

    CRM 2011 Update Rollup 6 when the blog post was created. You should have a target. Are you able to set breakpoints and determine what is being sent into the context of your plugin?

  4. Mark Pitts says:


    Yes, I stepped into the code – I’m attached to the sandbox worker process.

    localContext.PluginExecutionContext.InputParameters is not available. The only message that works is Post-Retrieve. I can get the ‘Retrieve’ attributes using:


    However, since my goal is to change what gets returned as part of the ‘Retrieve’ request, using the ‘Post Operation’ is too late in the life-cycle.

    I cannot get this to work using ‘Pre-Retrieve’.
    The trace log returns the following:

    —> Microsoft.Crm.CrmException: Unexpected exception from plug-in (Execute):
    Element ‘’
    contains data from a type that maps to the name ‘Microsoft.Crm.Sdk:Moniker’.
    The deserializer has no knowledge of any type that maps to this name.
    Consider changing the implementation of the ResolveName method on your DataContractResolver to return
    a non-null value for name ‘Moniker’ and namespace ‘Microsoft.Crm.Sdk’.


  5. Dave says:

    That is strange, what Update Rollup are you on?

  6. Mark Pitts says:

    Rollup 8.

    The plugin context object doesn’t contain very much for the ‘Retrieve’ message on ‘Pre-Operation’.

  7. Dave Corun says:

    And you have this marked as synchronous, pre-operation, and you have a Primary Entity defined?

  8. Mark Pitts says:

    I’m performing a SOAP ‘Retrieve’ request from javascript as follows:

    var xml = “” +
    authenticationHeader +
    “” +
    “” +
    “new_counter” +
    “” + counterid + “” +
    “” +
    “” +
    “new_entityname” +
    “new_nextnumber” +
    “new_format” +
    “” +
    “” +

    //call function to create Soap Request to ms crm webservice
    xmlHttpRequest =new ActiveXObject(“Msxml2.XMLHTTP”);
    xmlHttpRequest.Open(“POST”, “/mscrmservices/2007/CrmService.asmx”, false);
    xmlHttpRequest.setRequestHeader(“SOAPAction”, “”);
    xmlHttpRequest.setRequestHeader(“Content-Type”, “text/xml; charset=utf-8”);
    xmlHttpRequest.setRequestHeader(“Content-Length”, xml.length);

  9. Dave Corun says:

    Try verifying that the plugin is firing by creating a new record using the out of the box CRM Web UI? Also, are you sure that you’ve registered the plugin correctly? Not having a target leads me to believe that you haven’t associated the plugin with an entity.

  10. Mark Pitts says:

    I came across this post. I still have not found a solution.

  11. meenakshi says:

    does it effect if we use post operation in retrive plugin

  12. Naveed Anwar says:

    Thanks. This code has helped me a lot.

Leave a Reply

Your email address will not be published. Required fields are marked *