In this article , I am going to explain how to Create Update , Retrieve and Delete Email Attachment
For Better Understanding I divided this article in multiple part
(i) Create Email Activity
(ii) Create an e-mail attachment
(iii) Retrieve an attachment
(iv) Update attachment
(v) Retrieve all attachments associated with the email activity
(vi) Delete Attachment
Namespace need to include
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Crm.Sdk.Messages;
Create Email Activity
Email email = new Email
{
Subject = "This is an example email",
ActivityId = Guid.NewGuid()
};
Guid _emailId = _serviceProxy.Create(email);
Create an e-mail attachment
ActivityMimeAttachment _sampleAttachment = new ActivityMimeAttachment
{
ObjectId = new EntityReference(Email.EntityLogicalName, _emailId),
ObjectTypeCode = Email.EntityLogicalName,
Subject = String.Format("Sample Attachment {0}", i),
Body = System.Convert.ToBase64String(
new ASCIIEncoding().GetBytes("Example Attachment")),
FileName = String.Format("ExampleAttachment{0}.txt", i)
};
_emailAttachmentId[i] = _serviceProxy.Create(_sampleAttachment);
Retrieve an attachment
ActivityMimeAttachment _singleAttachment =
(ActivityMimeAttachment)_serviceProxy.Retrieve(
ActivityMimeAttachment.EntityLogicalName,
_emailAttachmentId[0],
new ColumnSet("activitymimeattachmentid",
"subject",
"filename",
"body"));
Update an attachment
_singleAttachment.FileName = "ExampleAttachmentUpdated.txt";
_serviceProxy.Update(_singleAttachment);
Retrieve all attachments associated with the email activity
QueryExpression _attachmentQuery = new QueryExpression
{
EntityName = ActivityMimeAttachment.EntityLogicalName,
ColumnSet = new ColumnSet("activitymimeattachmentid"),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression
{
AttributeName = "objectid",
Operator = ConditionOperator.Equal,
Values = {_emailId}
},
new ConditionExpression
{
AttributeName = "objecttypecode",
Operator = ConditionOperator.Equal,
Values = {Email.EntityLogicalName}
}
}
}
};
EntityCollection results = _serviceProxy.RetrieveMultiple(
_attachmentQuery);
Delete an Attachment
_serviceProxy.Delete(ActivityMimeAttachment.EntityLogicalName, _emailAttachmentId[0]);
Complete Code
using (OrganizationServiceProxy _serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri,
serverConfig.HomeRealmUri,
serverConfig.Credentials,
serverConfig.DeviceCredentials))
{
_serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
// Create Email Activity
Email email = new Email
{
Subject = "This is an example email",
ActivityId = Guid.NewGuid()
};
Guid _emailId = _serviceProxy.Create(email);
Guid[] _emailAttachmentId = new Guid[3];
// Create three e-mail attachments
for (int i = 0; i < 3; i++)
{
ActivityMimeAttachment _sampleAttachment = new ActivityMimeAttachment
{
ObjectId = new EntityReference(Email.EntityLogicalName, _emailId),
ObjectTypeCode = Email.EntityLogicalName,
Subject = String.Format("Sample Attachment {0}", i),
Body = System.Convert.ToBase64String(
new ASCIIEncoding().GetBytes("Example Attachment")),
FileName = String.Format("ExampleAttachment{0}.txt", i)
};
_emailAttachmentId[i] = _serviceProxy.Create(_sampleAttachment);
}
// Retrieve an attachment including its id, subject, filename and body.
ActivityMimeAttachment _singleAttachment =
(ActivityMimeAttachment)_serviceProxy.Retrieve(
ActivityMimeAttachment.EntityLogicalName,
_emailAttachmentId[0],
new ColumnSet("activitymimeattachmentid",
"subject",
"filename",
"body"));
// Update attachment
_singleAttachment.FileName = "ExampleAttachmentUpdated.txt";
_serviceProxy.Update(_singleAttachment);
// Retrieve all attachments associated with the email activity.
QueryExpression _attachmentQuery = new QueryExpression
{
EntityName = ActivityMimeAttachment.EntityLogicalName,
ColumnSet = new ColumnSet("activitymimeattachmentid"),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression
{
AttributeName = "objectid",
Operator = ConditionOperator.Equal,
Values = {_emailId}
},
new ConditionExpression
{
AttributeName = "objecttypecode",
Operator = ConditionOperator.Equal,
Values = {Email.EntityLogicalName}
}
}
}
};
EntityCollection results = _serviceProxy.RetrieveMultiple(
_attachmentQuery);
// Delete an Attachment
_serviceProxy.Delete(ActivityMimeAttachment.EntityLogicalName, _emailAttachmentId[0]);
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.