Wednesday, December 4, 2013

Plugin Get Attachments From Regarding and upload with the Email CRM 2011 and CRM 2013

Scenario/Requirement:

When user create email from the Quote, the attachments of the Quote should automatically added with the Email.
Currently user has to add all the attachments that are already added with the Quote and then after submission the quote to customer user have to revise the Quote and modify it and also modification on the attachments of Quote. Again user will submit this to customer by email and again he has to add all attachments.

Solution:

I have sugest a dynamic solution to the customer for all entities. I have Add a button on the Email Entity of name GetAttachements. when user clicks on this button then all attachments of regarding object entity will be added on the email.

All Steps:

I have written a plugin that will retrieve attachments from the email regarding Object Entity. and then create a object of activitymimeattachement Enitty and fill the attributes.

Step 1:

Retrieve attachments from the email regarding Object Entity.
 
RetrieveMultipleRequest getRequest = new RetrieveMultipleRequest();
QueryExpression qex = new QueryExpression("email");
 
qex.Criteria = new FilterExpression();
qex.Criteria.FilterOperator = LogicalOperator.And;
qex.Criteria.AddCondition(new ConditionExpression("activityid", ConditionOperator.Equal, _emailId));
qex.ColumnSet.AddColumn("regardingobjectid");
qex.ColumnSet.AddColumn("new_isattachmentsloaded");

getRequest.Query = qex;
RetrieveMultipleResponse returnValues = (RetrieveMultipleResponse)service.Execute(getRequest);
 

Step 2:

Create a object of activitymimeattachement Enitty and fill the attributes.

Entity ActivityAttachement = new Entity("activitymimeattachment");

ActivityAttachement.Attributes["objectid"] = new EntityReference("email", _emailId);

ActivityAttachement.Attributes["objecttypecode"] = 4202;

ActivityAttachement.Attributes["subject"] = att.Attributes.Contains("subject") == false ? "" : att.Attributes["subject"];
ActivityAttachement.Attributes["body"] = att.Attributes.Contains("documentbody") == false ? "" : att.Attributes.Contains("documentbody") == false ? "" : att.Attributes["documentbody"];

ActivityAttachement.Attributes["mimetype"] = att.Attributes.Contains("mimetype") == false ? "" : att.Attributes["mimetype"];
ActivityAttachement.Attributes["filename"] = att.Attributes.Contains("filename") == false ? "" : att.Attributes["filename"];

ActivityAttachement.Attributes["filesize"] = att.Attributes.Contains("filesize") == false ? "" : att.Attributes["filesize"];

ActivityAttachement.Attributes["filesize"] = att.Attributes.Contains("filesize") == false ? "" : att.Attributes["filesize"];

service.Create(ActivityAttachement);

Full Code:

 
Entity entity = (Entity)context.InputParameters["Target"];
try
 
{
 
// Obtain the organization service reference.
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
Guid _emailId = new Guid(context.PrimaryEntityId.ToString());
RetrieveMultipleRequest getRequest = new RetrieveMultipleRequest();
QueryExpression qex = new QueryExpression("email");
 
qex.Criteria = new FilterExpression();
qex.Criteria.FilterOperator = LogicalOperator.And;
qex.Criteria.AddCondition(new ConditionExpression("activityid", ConditionOperator.Equal, _emailId));
qex.ColumnSet.AddColumn("regardingobjectid");
qex.ColumnSet.AddColumn("new_isattachmentsloaded");
 
getRequest.Query = qex;
 
RetrieveMultipleResponse returnValues = (RetrieveMultipleResponse)service.Execute(getRequest);
if (entity.Attributes.Contains("new_isattachmentsloaded") == true)
 
{
 
if (returnValues.EntityCollection.Entities[0].Contains("regardingobjectid") && Convert.ToBoolean(returnValues.EntityCollection.Entities[0].Attributes["new_isattachmentsloaded"]) == true
 
{                          
EntityReference RegardingEntityReff = (EntityReference)returnValues.EntityCollection.Entities[0].Attributes["regardingobjectid"];
Guid RegardingId = new Guid(RegardingEntityReff.Id.ToString());
 
 
RetrieveMultipleResponse attachment = GetAttachmentFromNotes(service, RegardingId);
foreach (Entity att in attachment.EntityCollection.Entities)
 
{
if (Convert.ToBoolean(att.Attributes["isdocument"]) == true)
 
{
Entity ActivityAttachement = new Entity("activitymimeattachment");
ActivityAttachement.Attributes["objectid"] = new EntityReference("email", _emailId);
ActivityAttachement.Attributes["objecttypecode"] = 4202;
ActivityAttachement.Attributes["subject"] = att.Attributes.Contains("subject") == false ? "" : att.Attributes["subject"];
ActivityAttachement.Attributes["body"] = att.Attributes.Contains("documentbody") == false ? "" : att.Attributes.Contains("documentbody") == false ? "" : att.Attributes["documentbody"];
ActivityAttachement.Attributes["mimetype"] = att.Attributes.Contains("mimetype") == false ? "" : att.Attributes["mimetype"];
ActivityAttachement.Attributes["filename"] = att.Attributes.Contains("filename") == false ? "" : att.Attributes["filename"];
ActivityAttachement.Attributes["filesize"] = att.Attributes.Contains("filesize") == false ? "" : att.Attributes["filesize"];
ActivityAttachement.Attributes["filesize"] = att.Attributes.Contains("filesize") == false ? "" : att.Attributes["filesize"];
 
service.Create(ActivityAttachement);
}
}
}
}
}
 

Entity GetAttachment(IOrganizationService service, Guid attachmentId)

{ 
RetrieveMultipleRequest getRequest = new RetrieveMultipleRequest();

QueryExpression query = new QueryExpression("activitymimeattachment");

query.Criteria = new FilterExpression();

query.Criteria.FilterOperator = LogicalOperator.And;

query.Criteria.AddCondition(new ConditionExpression("attachmentid", ConditionOperator.Equal, attachmentId));

query.ColumnSet.AddColumn("mimetype");

query.ColumnSet.AddColumn("filename");

query.ColumnSet.AddColumn("body");

getRequest.Query = query; 
RetrieveMultipleResponse returnAttachments = (RetrieveMultipleResponse)service.Execute(getRequest);

if (returnAttachments.EntityCollection.Entities.Count > 0)

{ return returnAttachments.EntityCollection.Entities[0]; }

else

{ return null; }

}

 

RetrieveMultipleResponse GetAttachmentFromNotes(IOrganizationService service, Guid QuoteGuid)

{
try

{
RetrieveMultipleRequest getRequest = new RetrieveMultipleRequest();

QueryExpression query = new QueryExpression("annotation");

query.Criteria = new FilterExpression();

query.Criteria.FilterOperator = LogicalOperator.And;

query.Criteria.AddCondition(new ConditionExpression("objectid", ConditionOperator.Equal, QuoteGuid));

query.ColumnSet.AddColumn("mimetype");

query.ColumnSet.AddColumn("filename");

query.ColumnSet.AddColumn("documentbody");

query.ColumnSet.AddColumn("subject");

query.ColumnSet.AddColumn("isdocument");

query.ColumnSet.AddColumn("filesize");

getRequest.Query = query;
RetrieveMultipleResponse returnAttachments = (RetrieveMultipleResponse)service.Execute(getRequest);

if (returnAttachments.EntityCollection.Entities.Count > 0)

{ return returnAttachments; }//.EntityCollection.Entities[0]; }

else

{ return null; }

}
catch (Exception ex)

{
throw ex;

}

}

Screenshots:


Quote image that is having attachments
Create New Email Image from Quote or attach the Quote in regarding field. In the below image the email is created with no attachments and same regarding quote as mention above image.
User has to click on the GetAttachemet buton.
 
 
Now all the attachments of the Quote will be added in the email.
 
If opportunity, Order, any custom entity is attached with the regarding then that particular entity record attachments will be added with the email.
 
 
Best of Luck...........................................................................................................