POWEROBJECTS

718 Washington Ave. N. Suite #101
Minneapolis, MN 55401
View map and all Locations

Send us a message

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:

development

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:

Avatar for JoeCRM

JoeCRM

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