Search code examples
c#pluginsdynamics-crmdynamics-crm-onlinedynamics-crm-2016

Microsoft Dynamics CRM - Email Entity Retrieve Data - Plug In


I'm trying to write a plugin for the email entity in Microsoft Dynamics CRM Online. Let's say its called "Sample_PlugIn".

I want the plugin to retrieve the sender of the email, and write his/her email address into a field (new_samplefield) of the email.

The Plugin does some other stuff as well (and it's all working), but this part of the code is the one making problems. (My organisation service reference is called "service".)

try                
{               
    Entity email = (Entity)context.InputParameters["Target"];

    EntityCollection fromCollection = (EntityCollection)email.Attributes["from"];

    if (fromCollection != null && fromCollection.Entities.Count > 0)
    {
        Entity sender = fromCollection[0]; 
        email["new_samplefield"] = (string)sender.Attributes["internalemailaddress"];
    }

    service.Update(email);
}

Every time the plug-in is executed, I get this error:

Unexpected exception from plug-in (Execute): Sample_PlugIn.Sample_PlugIn: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.

It'd be great if anyone could help me - thanks a lot!


Solution

  • Just for the sake of completeness, this is the code that worked for me in the end. In this case, the sender is of entity systemuser. (I have registered this plug-in on "post-operation" and "Update".)

    try
    {
        Entity email = (Entity)context.InputParameters["Target"];
    
        //  Post Entity Image (since the plug-in is registered on "Update")
        Entity postImage = context.PostEntityImages["Image"];
    
        EntityCollection fromCollection = postImage.GetAttributeValue<EntityCollection>("from");
    
        if (fromCollection != null && fromCollection.Entities.Count > 0)
        {
            Entity sender = fromCollection[0];
            EntityReference partyId = sender.GetAttributeValue<EntityReference>("partyid");
    
            string entityType = partyId.LogicalName.ToString();
    
            if (entityType == "systemuser")
                {
                    //  Create query using querybyattribute
                    QueryByAttribute queryToSender = new QueryByAttribute("systemuser");
                    queryToSender.ColumnSet = new ColumnSet("systemuserid", "internalemailaddress");
    
                    //  Attribute to query
                    queryToSender.Attributes.AddRange("systemuserid");
    
                    //  Value of queried attribute to return
                    queryToSender.Values.AddRange(partyId.Id);
    
                    EntityCollection retrievedFromSystemuser = service.RetrieveMultiple(queryToSender);
    
                    foreach (Entity systemuserE in retrievedFromSystemuser.Entities)
                        {
                            email["new_samplefield"] = (string)systemuserE.Attributes["internalemailaddress"];                          
                        }
                }                     
            }
    
        service.Update(email);
    }