Exporting Annotation (Note) Attachment

A requirement I had recently was to move data between CRM environments, some of this data was files loaded into CRM as an attachment to an annotation (note). I’ve included a snippet below which has been cut down to bare bones. Its basically a function that uses fetch xml to retrieve the annotations, decode the file data and save to disk. So each annotation record has three attributes which are important here;

  • filename – name of the file (note: different from the subject of the annotation)
  • documentbody – Base64 encoded string which represents the document – hence I decode it before writing to disk
  • mimetype – I don’t actually use it for anything in the snippet but its required for reimporting – its an internet mime type and describes the type of file, e.g. image/jpeg

If you are going to use this you will probably want to change the fetch to; get more records (count) and have some filtering, i used a link-entity, e.g. get attachments regarding contacts

public void ExportDocuments(IOrganizationService service, String filePath)
     String fetch = @"<fetch mapping='logical' count='100' version='1.0'>
          <entity name='annotation'>
               <attribute name='filename' />
               <attribute name='documentbody' />
               <attribute name='mimetype' />

     foreach (Entity e in service.RetrieveMultiple(new FetchExpression(fetch)))
          if (!String.IsNullOrWhiteSpace(e.Attributes["documentbody"].ToString()))
               byte[] data = Convert.FromBase64String(e.Attributes["documentbody"].ToString());

               File.WriteAllBytes(filePath + e.Attributes["filename"].ToString(), data);

2 thoughts on “Exporting Annotation (Note) Attachment

  1. Pingback: Importing Annotation (Note) Attachment « woodsworkblog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s