web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Email Entity Retrieve Data - Plug In - C#

(0) ShareShare
ReportReport
Posted on by

Hello,

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!

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Try to use following code:

    try                

    {              

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

       EntityCollection fromCollection = email.GetAttributeValue<EntityCollection>("from");

       if (fromCollection != null && fromCollection.Entities.Count > 0)

       {

           Entity sender = fromCollection[0];

           email["new_samplefield"] = sender.GetAttributeValue<string>("internalemailaddress");

       }

       service.Update(email);

    }

    Anyway I don't think that this code will work the way you want. Can you please explain your scenario?

  • ashlega Profile Picture
    34,477 on at

    Hi,

     "from" is a partylist attribute - there are more examples here:

    http://www.magnetismsolutions.com/blog/paulnieuwelaar/2015/09/07/working-with-dynamics-crm-activity-party-lists-in-c--plugins

     You might need to replace

     email["new_samplefield"] = (string)sender.Attributes["internalemailaddress"];

     With

     email["new_samplefield"] = (string)sender.Attributes["addressused"];

    PS. If you move that plugin step to the pre-create, you may not need to call service.Update - setting "new_samplefield" attribute directly on the target should work

  • Verified answer
    Community Member Profile Picture
    on at

    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);

    }

  • Community Member Profile Picture
    on at

    Hi Nathalie! I know a plugin that can help log calls automatically in MS Dynamics. That app is Tenfold, a CTI and sales acceleration platform that offers features like call logging and call monitoring. You can check out Tenfold page if you'd like to know more. www.tenfold.com/.../dynamics

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans