Random Musings

December 8, 2008

Tagging attachment during DeliverIncomingEmail event

Filed under: Dynamics CRM — haditeo @ 12:01 pm

In the plugin, DeliverIncomingEmail event on the email entity can be used to process any incoming emails. At that event, the email record has been created properly, so that developers can utilize the record for any other purposes, let’s say create a new custom case.

Just recently found out that the email’s attachments are actually created automatically in the activitymimeattachment records. Now here is the problem: how to tag the email’s attachments with the custom case as well ?

The solution is by creating Annotation records based on the ActivityMimeAttachment records.

Refer to this code snippet for sample :

public static void CreateAnnotation(ref CrmService service, email mail, Guid incidentGuid)
{
    try
    {
	BusinessEntityCollection activityMimeAttachmentColl = RetrieveByAttributeReturnColl(ref service, "activitymimeattachment", "activityid", mail.activityid.Value.ToString(), new string[] { "body", "attachmentnumber", "filesize", "filename" });

	foreach (DynamicEntity deActivityMimeAttachment in activityMimeAttachmentColl.BusinessEntities)
	{
	    DynamicEntity deAnnotation = new DynamicEntity();
	    deAnnotation.Name = "annotation";

	    StringProperty spDocumentBody = new StringProperty();
	    if (deActivityMimeAttachment.Properties.Contains("body"))
	    {
               spDocumentBody.Value = deActivityMimeAttachment["body"].ToString();
	    }
	    else
	    {
               spDocumentBody.Value = string.Empty;
	    }
	    spDocumentBody.Name = "documentbody";
	    deAnnotation.Properties.Add(spDocumentBody);

	    StringProperty spFileName = new StringProperty();
	    if (deActivityMimeAttachment.Properties.Contains("filename"))
	    {
               spFileName.Value = deActivityMimeAttachment["filename"].ToString();
	    }
	    else
	    {
               spFileName.Value = string.Empty;
	    }
	    spFileName.Name = "filename";
	    deAnnotation.Properties.Add(spFileName);

	    StringProperty spSubject = new StringProperty();
	    if (deActivityMimeAttachment.Properties.Contains("attachmentnumber"))
	    {
                spSubject.Value = "attachment - " + ((CrmNumber)deActivityMimeAttachment["attachmentnumber"]).Value.ToString();
	    }
	    else
	    {
                spSubject.Value = "subject";
	    }
	    spSubject.Name = "subject";
	    deAnnotation.Properties.Add(spSubject);

	    CrmNumberProperty cnpFileSize = new CrmNumberProperty();
	    if (deActivityMimeAttachment.Properties.Contains("filesize"))
	    {
                cnpFileSize.Value = ((CrmNumber)deActivityMimeAttachment["filesize"]);
	    }
	    else
	    {
                cnpFileSize.Value = new CrmNumber(0);
	    }
	    cnpFileSize.Name = "filesize";
	    deAnnotation.Properties.Add(cnpFileSize);

	    CrmBooleanProperty cbpIsDocument = new CrmBooleanProperty();
	    cbpIsDocument.Value = new CrmBoolean(true);
	    cbpIsDocument.Name = "isdocument";
	    deAnnotation.Properties.Add(cbpIsDocument);

	    EntityNameReferenceProperty enrpObjectTypeCode = new EntityNameReferenceProperty();
	    enrpObjectTypeCode.Value = new EntityNameReference("incident");
	    enrpObjectTypeCode.Name = "objecttypecode";
	    deAnnotation.Properties.Add(enrpObjectTypeCode);

	    LookupProperty lpObjectId = new LookupProperty();
	    lpObjectId.Value = new Lookup("incident", incidentGuid);
	    lpObjectId.Name = "objectid";
	    deAnnotation.Properties.Add(lpObjectId);

	    TargetCreateDynamic target = new TargetCreateDynamic();
	    target.Entity = deAnnotation;
	    CreateRequest req = new CreateRequest();
	    req.Target = target;
	    CreateResponse res = (CreateResponse)service.Execute(req);
	}
    }
    catch (System.Web.Services.Protocols.SoapException ex)
    {
        WriteEntryToEventLog("public static void CreateAnnotation(ref CrmService service, email mail, Guid incidentGuid)", ex);

        throw ex;
    }
    catch (Exception e)
    {
        WriteEntryToEventLog("public static void CreateAnnotation(ref CrmService service, email mail, Guid incidentGuid)", e);

        throw e;
    }
}
Advertisements

2 Comments »

  1. Hello!
    How I can use this above code? Can you please put some more details.
    Thanks

    Comment by Colonel87 — January 7, 2009 @ 1:09 am | Reply

  2. Hello Colonel87,

    During the DeliverIncomingEmail message inside the plugin you can retrieve the ActivityId from the Context.

    By using the ActivityId, you can retrieve the instance of the ActivityMimeAttachment related to the ActivityId. This below code is my custom code in retrieving the related ActivityMimeAttachment based on ActivityId

    BusinessEntityCollection activityMimeAttachmentColl = RetrieveByAttributeReturnColl(ref service, “activitymimeattachment”, “activityid”, mail.activityid.Value.ToString(), new string[] { “body”, “attachmentnumber”, “filesize”, “filename” });

    Basically you need to some query to SDK to retrieve this : SELECT * FROM ActivityMimeAttachment WHERE ActivityId = SomeId

    Hope this helps,

    hadi teo

    Comment by haditeo — January 7, 2009 @ 1:30 pm | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: