title: Microsoft Dynamics CRM - Email Entity Retrieve Data - Plug In
body: <p>I'm trying to write a plugin for the email entity in Microsoft Dynamics CRM Online. Let's say its called "Sample_PlugIn".</p>
  
  <p>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.</p>
  
  <p>The Plugin does some other stuff as well (and it's all working), but this part of the code is the one making problems. <em>(My organisation service reference is called "service".)</em></p>
  
  <pre><code>try                
  {               
      Entity email = (Entity)context.InputParameters["Target"];
  
      EntityCollection fromCollection = (EntityCollection)email.Attributes["from"];
  
      if (fromCollection != null &amp;&amp; fromCollection.Entities.Count &gt; 0)
      {
          Entity sender = fromCollection[0]; 
          email["new_samplefield"] = (string)sender.Attributes["internalemailaddress"];
      }
  
      service.Update(email);
  }
  </code></pre>
  
  <p>Every time the plug-in is executed, I get this error: </p>
  
  <blockquote>
    <p>Unexpected exception from plug-in (Execute):
    Sample_PlugIn.Sample_PlugIn:
    System.Collections.Generic.KeyNotFoundException: The given key was not
    present in the dictionary.</p>
  </blockquote>
  
  <p>It'd be great if anyone could help me - thanks a lot!</p>