CRM2011 – Retrieve Plugin

2007_microsoft_dynamics_crmToday I need to execute a method every time open a Contact 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:

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

On first open of the record:





On all future opens of the record:



4 thoughts on “CRM2011 – Retrieve Plugin

    • Hi Nilesh,
      you can debug the retrieve plugin with remote debugger… you need to register plugin assembly and pdb (put in GAC) files on server.. Register plugin in database mode and after use remote debugger….

  1. Hi All, I have user permission at Business unit level to read records. in my update plugin I am assign record to different business unit. record is successfully assigned to new business unit. perhaps,below error I am getting

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Server was unable to process request.Detail:


    Server was unable to process request.


    SecLib::AccessCheckEx failed. Returned hr = -2147187962, ObjectID: 6eba400d-c1e1-e411-af83-0050569d2111, OwnerId: 09b1a18b-ce2d-e411-92db-0050569d2111, OwnerIdType: 9 and CallingUser: e3a107b6-9d70-e411-8d50-0050569d2111. ObjectTypeCode: 10022, objectBusinessUnitId: 08b1a18b-ce2d-e411-92db-0050569d2111, AccessRights: ReadAccess

    After update plugin execution will retreive plugin will fire???


Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:


Stai commentando usando il tuo account Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...