title: Copying annotations leads to a corrupted attachment
body: <p>I'm implementing a plugin (POST Quote Create, Synchronous, Sandbox) to make it so that Notes are copied to the new record when a quote is revised.</p>
  
  <p>My plugin boils down to this (snippet):</p>
  
  <pre><code>var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
  var Service = serviceFactory.CreateOrganizationService(context.UserId);
  
  var notesQuery = new QueryExpression("annotation");
  notesQuery.ColumnSet = new ColumnSet(true);
  notesQuery.Criteria = new FilterExpression
  {
      FilterOperator = LogicalOperator.And,
      Conditions =
      {
          new ConditionExpression("objecttypecode", ConditionOperator.Equal, "quote"),
          new ConditionExpression("objectid", ConditionOperator.Equal, revisedQuoteId)
      }
  };
  var notes = Service.RetrieveMultiple(notesQuery).Entities;
  foreach (var n in notes)
  {
      var newNote = new Entity("annotation");
  
      newNote.Attributes.Add("ownerid", n.GetAttributeValue&lt;EntityReference&gt;("ownerid"));
  
      newNote.Attributes.Add("objectid", new EntityReference("quote", sourceEntity.Id));
      newNote.Attributes.Add("objecttypecode", "quote");
  
      newNote.Attributes.Add("subject", n.GetAttributeValue&lt;string&gt;("subject"));
      newNote.Attributes.Add("notetext", n.GetAttributeValue&lt;string&gt;("notetext"));
  
      newNote.Attributes.Add("isdocument", n.GetAttributeValue&lt;bool&gt;("isdocument"));
  
      if (n.GetAttributeValue&lt;bool&gt;("isdocument"))
      {
          newNote.Attributes.Add("filesize", n.GetAttributeValue&lt;int&gt;("filesize"));
          newNote.Attributes.Add("documentbody", n.GetAttributeValue&lt;string&gt;("documentbody"));
          newNote.Attributes.Add("filename", n.GetAttributeValue&lt;string&gt;("filename"));
          newNote.Attributes.Add("mimetype", n.GetAttributeValue&lt;string&gt;("mimetype"));
      }
  
      Service.Create(newNote);
  }
  </code></pre>
  
  <p>Basically, I copy everything over, including an eventual attachment. Everything seems fine, the new revision shows fields, detail records and notes properly... everything <em>but the attachment</em> of the notes.</p>
  
  <p>If I have a single note, with an attached <code>test.txt</code> which content is:</p>
  
  <blockquote>
    <p>Test attachment</p>
  </blockquote>
  
  <p>The <code>OrganizationData</code> service reads as follows:</p>
  
  <pre><code>&lt;d:FileName&gt;test.txt&lt;/d:FileName&gt;
  &lt;d:FileSize m:type="Edm.Int32"&gt;39&lt;/d:FileSize&gt;        
  &lt;d:DocumentBody&gt;H4sIAAAMaVMA/wtJLS5RSCwpSUzOyE3NK+HlAgCLmj1zEQAAAA==&lt;/d:DocumentBody&gt;
  </code></pre>
  
  <p>Its "clone" has the correct subject and text, and also shows a <code>test.txt</code> attached which content is</p>
  
  <blockquote>
    <p>‹  iS ÿI-.QH,)ILÎÈMÍ+áå ‹š=s   </p>
  </blockquote>
  
  <p><code>mimetype</code> <strike>and <code>filesize</code></strike> (while checking odata, I noticed that <code>filesize</code> is <em>not</em> actually correct!) appear to be correct (aka: the same as the original note I'm trying to copy), but OData seems to confirm something's off (it's different!):</p>
  
  <pre><code>&lt;d:FileName&gt;test.txt&lt;/d:FileName&gt;
  &lt;d:FileSize m:type="Edm.Int32"&gt;60&lt;/d:FileSize
  &lt;d:DocumentBody&gt;H4sIAED6aVMA/5Pv5mBg4MkMZvjP7amrF+iho+npc+6E71nth0+ZGLpn2RYLMjAwAABXqCwTJQAAAA==&lt;/d:DocumentBody&gt;
  </code></pre>
  
  <p>The <code>test.txt</code> file was created from a command prompt (<code>COPY CON test.txt</code>, type, <code>CTRL+Z</code>).</p>
  
  <p>I tried to change the file, and created a <code>test.pdf</code> through PDFCreator: AcroRead in turn whines and says the document is corrupted (so it seems like the issue is mimetype-agnostic).</p>
  
  <p>I also tried re-implementing the same code through early binding (via the CRMSVCUTIL-generated classes) but it yields the exact same result (garbage instead of the attachment contents).</p>
  
  <p>I attempted to hand-craft the <code>documentbody</code> like this:</p>
  
  <pre><code>// "VGVzdCBhdHRhY2htZW50" is Base64 for "Test attachment"
  newNote.Attributes.Add("documentbody", "VGVzdCBhdHRhY2htZW50");
  </code></pre>
  
  <p><strong>and the created file is correct</strong>.</p>
  
  <p>I can't figure out what's going on: as far as I know, <code>documentbody</code> is supposed to be a Base64-encoded string which (again, as far as I know) shouldn't be any different when copied around. What am I missing ?</p>
  
  <p>For reference, CRM is updated to <strong>UR13</strong> but I repro'd it on a <strong>UR16</strong> environment.</p>