Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

Read a text file in 'notes'

Posted on by 1,695

CRM 2016 On-Premises

I have a custom entity with a text file uploaded into each record 'notes' . 

I'm trying to grab the 'file' from annotation, open it and read it in a custom workflow with something like File Stream. I'd like to read the file directly from CRM rather than  write it to disc 

These text files are uploaded to CRM  and contain multiple data records that I need to process into different entities, hence the need to be be able to open each 'attachment' read and process it through a workflow.

  • Suggested answer
    Pawar Pravin  Profile Picture
    Pawar Pravin 5,227 on at
    RE: Read a text file in 'notes'

    FYI,

    //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();
    
     
    
        .....
    
     
    
    // 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;
    
    }

  • Suggested answer
    Pete_N Profile Picture
    Pete_N 1,695 on at
    RE: Read a text file in 'notes'

    Thank you

  • Suggested answer
    Adrian Begovich Profile Picture
    Adrian Begovich 21,009 Super User 2024 Season 2 on at
    RE: Read a text file in 'notes'

    Hi Pete N,

    You can obtain the contents of an annotation entity by using C#. This article explains how to achieve this.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans