In this webinar, our experts showcase a variety of demo use cases of how different components of the...
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:
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.
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:
How to export/import Notes/Tasks/Activities associated with a Contact records in CRM 2015
Hi Lincy - if you need to make adjustments to a few notes or activities, one option is the export data for re-import. This works well if you need to batch udpate a set of records. However, we recomend first testing in a test enviornment.
https://powerobjects.com/blog/2015/07/20/exporting-data-to-re-import-the-invalid-column-headings-issue/
how to do this in online CRM 2015 using javascript.
suggest me
Hi Joe,
thanks for giving information. In my requirement i want to update a annotation, can u plz suggest me for this. Thank you +
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
Hi. Joe, good afternoon, you could tell me how to apply it, or where I can get information.
Thanks for your help
regards