Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

C# sdk downloading note attachment from a record

(0) ShareShare
ReportReport
Posted on by

Hello all!

I'm trying to write a console app using c# to download pdf files from note section of a record in Entity A. After downloading the files, I would like to delete those files from CRM as well. 

Can anyone explain how to query those files? some example code is much appreciated!

Thank you!

*This post is locked for comments

  • Verified answer
    Mahendar Pal Profile Picture
    45,095 on at
    RE: C# sdk downloading note attachment from a record

    Ok, here _annotationId is the notes record whose attachment you want to download. so you can retrieve this using retrievemultiple method for example let's say you want to retrieve annotation record based on it's name, so you can use something like below:

    ConditionExpression condition1 = new ConditionExpression();

    condition1.AttributeName = "subject ";

    condition1.Operator = ConditionOperator.Equal;

    condition1.Values.Add("Sample Attachment");            

    FilterExpression filter1 = new FilterExpression();

    filter1.Conditions.Add(condition1);

    QueryExpression query = new QueryExpression("annotation");

    query.ColumnSet.AddColumns("annotationid");

    query.Criteria.AddFilter(filter1);

    EntityCollection result1 = _service.RetrieveMultiple(query);

    _annotationId=result.Entities[0].Id;

  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,188 Moderator on at
    RE: C# sdk downloading note attachment from a record

    You can use the following sample code to loop through the annotations, download the files and delete them.

    I would probably run this in separate processes. Once to download and once to delete, so that you can verify the files before deleting them...

    Check this out:

    private EntityCollection ProcessAttachments()

    {

    ColumnSet columns = new ColumnSet("annotationid", "filename", "documentbody");

    QueryExpression query = new QueryExpression

    {

    ColumnSet = columns,

    EntityName = "annotation"

    };

    try

    {

    EntityCollection results = service.RetrieveMultiple(query);

    foreach (Entity annotation in results.Entities)

    {

      string fileName = annotation["FileName"].ToString();

      FileStream stream = new FileStream(fileName, FileMode.OpenOrCreate);

      byte[] fileContent = Convert.FromBase64String(annotation["documentbody"]);

      stream.Write(fileContent, 0, fileContent.Length);

      // Call Delete of Annotation

      service.Delete(annotation.Id);

    }

    }

    catch (FaultException<OrganizationServiceFault> ex)

    {

    }

    }

    Hope this helps.

  • Community Member Profile Picture
    on at
    RE: C# sdk downloading note attachment from a record

    Yeah, I just don't quite understand it.   What is _annotationId? and where and how do I get it?                

    #region Download attachment from annotation record

                       // Define columns to retrieve from the annotation record.

                       ColumnSet cols = new ColumnSet("filename", "documentbody");

                       // Retrieve the annotation record.

                       Annotation retrievedAnnotation =

                           (Annotation)_serviceProxy.Retrieve("annotation", _annotationId, cols);

                       Console.WriteLine(", and retrieved.");

                       _fileName = retrievedAnnotation.FileName;

                       // Download the attachment in the current execution folder.

                       using (FileStream fileStream = new FileStream(retrievedAnnotation.FileName, FileMode.OpenOrCreate))

                       {

                           byte[] fileContent = Convert.FromBase64String(retrievedAnnotation.DocumentBody);

                           fileStream.Write(fileContent, 0, fileContent.Length);

                       }

                       Console.WriteLine("Attachment downloaded.");

    #endregion Download attachment from annotation record

  • Suggested answer
    Mahendar Pal Profile Picture
    45,095 on at
    RE: C# sdk downloading note attachment from a record

    Hello,

    Did you check SDK Sample: msdn.microsoft.com/.../gg328429.aspx

    once you have the record id you can use delete method to delete attachment record.

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 52

#3
dkrishna Profile Picture

dkrishna 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans