How to Download the attachment of a note within a plugin in dynamics crm?

Hi,

My requirement was to register a plugin so that every time a file is attached to some entities, I download the file to a temp folder for performing some processing. Actually, in my case it was processing an Excel file as a more user-friendly data input for line items in opportunities and quotes, but that is anecdotic.

The 3 key points I would like to share here with you is how to:

  • Retrieve the note (annotation entity which is the one who stores the file)
  • Retrieve the file content
  • Save the file to a location in the web server

Below are some parts of my plugin .cs file:

 

//RETRIEVE THE NOTE

protected void ExecutePostNoteCreate(LocalPluginContext localContext)
{
    string tempFile = string.Empty;
 
    IPluginExecutionContext context = localContext.PluginExecutionContext;
    IOrganizationService organizationService = localContext.OrganizationService;         
    
    if (context.InputParameters.Contains("Target") &&
        context.InputParameters["Target"] is Entity)
    {
        Entity noteEntity = (Entity)context.InputParameters["Target"];
 
        Annotation note = noteEntity.ToEntity<Annotation>();
 
    .....

 

// RETRIEVE THE FILE CONTENT (DocumentBody property)

if (!string.IsNullOrEmpty(note.FileName))
{
   tempFile = SaveFile(note.FileName, note.DocumentBody);
}

 

// SAVE THE FILE

private static string SaveFile(string fileName, string noteBody)
{
    string outputFileName = @"C:\temp\" + fileName;
 
    if (!string.IsNullOrEmpty(noteBody))
    {
        // Download the attachment in the current execution folder.
        byte[] fileContent = Convert.FromBase64String(noteBody);
        System.IO.File.WriteAllBytes(outputFileName, fileContent);
    }
    else
    {
        throw new InvalidPluginExecutionException("File content is empty or cannot be retrieved");
    }
 
    return outputFileName;
}

Take note I’m using System.IO.File.WriteAllBytes. This is what did work for me. I’ve tried using WriteAllText and also FileStream but when trying to open the generated file, it was not in the right format.

 

Hope it helps,

PP [twitter: @pabloperalta]

1 Comment

  • Hello Pablo,

    A very nice post. I've been thinking for a while in a process similar to this one, to implement in my company.

    I've two question for you regarding this.

    1. Will this work online, and if yes, is it possible to use SharePoint has the location destination instead the file system, and if yes, can you point me a direction to start reading about it.

    2. Is this possible to get the attachments of a give email? And maybe delete the attachment after store it somewhere (SHP list, FileSystem).

    cheers, and keep up the good work.
    Mário

Comments have been disabled for this content.