PowerObjects Blog 

for Microsoft Business Applications


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


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

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 Maria Valley

Leave a Reply

Your email address will not be published. Required fields are marked *

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

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

  2. 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

  3. 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