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)
        serviceProvider.GetService(typeof(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());
            }
            else
            {
                contact["address1_name"] = "i already exist";
            }
            service.Update(contact);
        }
    }
}

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

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

On first open of the record:

clip_image002_thumb

 

 

 

On all future opens of the record:

clip_image0025_thumb

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=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Server was unable to process request.Detail:

    -2140991214

    Server was unable to process request.
    2015-04-13T10:12:09.3468529Z

    -2147187962

    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
    2015-04-13T10:12:09.3468529Z

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

Lascia un commento

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

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. 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...