Looking for PowerObjects? Don’t worry, you’re in the right place! We’ve been part of HCL for several years, and we’ve now taken the final step in our acquisition journey: moving our website to the HCL domain. Nothing else is changing – we are still fanatically focused on Microsoft Business Applications!

PowerObjects Blog 

for Microsoft Business Applications


Retrieve and Create Attachments Associated To Notes of an Entity in Dynamics CRM

Post Author: Joe D365 |

You may have a need to retrieve attachments and create attachments associated to notes of an entity in Microsoft Dynamics CRM. We have provided the sample .net code that shows how to retrieve these attachments:

Sample 1

private
EntityCollection RetrieveNoteAttachments(Guid entityId)

{


EntityCollection results = null;


QueryExpression _noteattachmentQuery = new
QueryExpression

{

EntityName = "annotation",

ColumnSet = new
ColumnSet("subject",


"filename",


"notetext",


"documentbody"),

Criteria = new
FilterExpression

{

Conditions =

{


new
ConditionExpression

{

AttributeName = "objectid",

Operator = ConditionOperator.Equal,

Values = {entityId}

},


new
ConditionExpression

{

AttributeName = "isdocument",

Operator = ConditionOperator.Equal,

Values = {true}

}

}

}

};


using (var service = new
OrganizationService("CrmConnection"))

{

 

results = service.RetrieveMultiple(_noteattachmentQuery);

}


return results;

}

Where

entityId=Guid/ID of the entity in CRM for which we are retrieving the attachments

CRMConnection= The connection string to connect to the CRM org for which we are connecting to.

 

Sample 2

Here is the sample .net code that shows how to create a note with attachment in CRM for an entity:


private
Guid CreateNoteAttachment(string subject, Guid entityId, string filename, byte[] data)

{

 


Guid attachmentId = Guid.Empty;


Entity note = new
Entity("annotation");

note["subject"]=subject;

note["filename"]=filename;

note["documentbody"]=Convert.ToBase64String(data);

note["objectid"] = new
EntityReference("contact", entityId);


using (var service = new
OrganizationService("CrmConnection"))

{

attachmentId = service.Create(note);

}


return attachmentId;

}

Where

entityId=Guid/ID of the entity in CRM for which we are retrieving the attachments

subject= Subject of the note that we are creating

filename=The name of the file we are attaching to note

data=the content of the file we are attaching to note

CRMConnection= The connection string to connect to the CRM org for which we are connecting to.

 

Note: In case we are using the code in a plugin, we can use the instance of the IOrganizationService to connect to Dynamics CRM.

We hope you find this topic as helpful and informative as we do! For even more information check out another blog we think will be helpful for you:

Joe CRM
By Joe D365
Joe D365 is a Microsoft Dynamics 365 superhero who runs on pure Dynamics adrenaline. As the face of PowerObjects, Joe D365’s mission is to reveal innovative ways to use Dynamics 365 and bring the application to more businesses and organizations around the world.

6 comments on “Retrieve and Create Attachments Associated To Notes of an Entity in Dynamics CRM”

  1. How to export/import Notes/Tasks/Activities associated with a Contact records in CRM 2015

  2. Hi Joe,
    thanks for giving information. In my requirement i want to update a annotation, can u plz suggest me for this. Thank you +

  3. Hi there,
    Is there any chance you could tell me how to run this code within CRM? Or point me in the direction where I can learn?
    Thanks,
    Luke

  4. Hi. Joe, good afternoon, you could tell me how to apply it, or where I can get information.

    Thanks for your help

    regards

PowerObjects Recommends