web
You’re offline. This is a read only version of the page.
close
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

I have the same question (0)
  • Suggested answer
    Mahendar Pal Profile Picture
    45,095 on at

    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.

  • Community Member Profile Picture
    on at

    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
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    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.

  • Verified answer
    Mahendar Pal Profile Picture
    45,095 on at

    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;

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans