locked
CRM 4.0 : Download Email Attachment - help please RRS feed

  • Question

  •  

    Hi All,

    I have an issue with downloading an Email Attachment. I have noticed there is a sample code in the SDK to demonstrate this, but no details (hardcoded stuff), for a new learner i am not sure how they will learn something from sdk.

    My scenario is when CRM receives an email as an Activity, I want build a workflow to save the attachment on the physical drive.

    Now my question is from an email how do I find out AttachmentId or do I need to look at the activitymimeattachment object and retrieve the attachment based on email.activityid. I tried with few things without much success. Can you guys please help on this… please?

     

    Thanks

    Tom

     

    My Code is as follows:

     

    Code Snippet

    namespace CustomActivityLibrary

    {

        [CrmWorkflowActivity("Custom Activity - Read Email Attachment", "Custom Activities Library")]

        public partial class CustomReadEmailAttachment : SequenceActivity

        {

            public CustomReadEmailAttachment()

            {

                InitializeComponent();

            }

     

            public static DependencyProperty myEmailIdProperty =

                    DependencyProperty.Register("myEmailId", typeof(Lookup), typeof(CustomReadEmailAttachment));

     

            [CrmInput("My emailid")]

            [CrmReferenceTarget("email")]

            public Lookup myEmailId

            {

                get

                {

                    return (Lookup)base.GetValue(myEmailIdProperty);

                }

                set

                {

                    base.SetValue(myEmailIdProperty, value);

                }

            }

     

            protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

            {

                //Get the context service

                IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));

                IWorkflowContext context = contextService.Context;

     

                //Use the context service to create an instance of CrmService

                ICrmService crmService = context.CreateCrmService();

     

                Guid newEmailId = myEmailId.Value;

     

                Microsoft.Crm.SdkTypeProxy.email crmEmail = (Microsoft.Crm.SdkTypeProxy.email)crmService.Retrieve(Microsoft.Crm.SdkTypeProxy.EntityName.email.ToString(), newEmailId, new Microsoft.Crm.Sdk.Query.AllColumns());

                if (crmEmail != null)

                {

                    Microsoft.Crm.SdkTypeProxy.activitymimeattachment attachment = (Microsoft.Crm.SdkTypeProxy.activitymimeattachment)crmService.Retrieve(Microsoft.Crm.SdkTypeProxy.EntityName.activitymimeattachment.ToString(), crmEmail.activityid.Value, new Microsoft.Crm.Sdk.Query.AllColumns());

                    if (attachment != null)

                    {

                        string attachid = attachment.activitymimeattachmentid.ToString();

                        string objecttypecode = Microsoft.Crm.SdkTypeProxy.EntityName.activitymimeattachment.ToString();

                        string url = "http://localhost:5555/Activities/Attachment/download.aspx?AttachmentType=" + objecttypecode + "&AttachmentId=" + attachid;

                        System.Net.WebClient myWebClient = new System.Net.WebClient();

                        myWebClient.Credentials = System.Net.CredentialCache.DefaultCredentials;

                        myWebClient.DownloadFile(url, attachment.filename);

     

                        return ActivityExecutionStatus.Closed;

                    }

                }

     

                return ActivityExecutionStatus.Faulting;

            }

     

        }

    }

     

     

    Tuesday, July 29, 2008 7:55 AM

Answers

  • I have developed a solution myself for this after doing R&D in CRM 4.0

     

    Here is the code to create workflow to download an attachment from email activity. Please change the username , password, server/domain and port details according  to your CRM server settings. Hope this will help

     

     

    Code Snippet

    namespace CustomActivityLibrary

    {

        [CrmWorkflowActivity("Custom Activity - Read Email Attachment", "Custom Activities Library")]

        public partial class CustomReadEmailAttachment: SequenceActivity

          {

                public CustomReadEmailAttachment()

                {

                      InitializeComponent();

                }

     

     

            public static DependencyProperty myEmailIdProperty =

                    DependencyProperty.Register("myEmailId", typeof(Lookup), typeof(CustomReadEmailAttachment));

     

     

            [CrmInput("My emailid")]

            [CrmReferenceTarget("email")]

            public Lookup myEmailId

            {

                get

                {

                    return (Lookup)base.GetValue(myEmailIdProperty);

                }

                set

                {

                    base.SetValue(myEmailIdProperty, value);

                }

            }

     

            protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

            {

                //Get the context service

                IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));

                IWorkflowContext context = contextService.Context;

     

                //Use the context service to create an instance of CrmService

                ICrmService crmService = context.CreateCrmService();

     

                Guid newEmailId = myEmailId.Value;

     

                try

                {

                    Microsoft.Crm.SdkTypeProxy.email crmEmail = (Microsoft.Crm.SdkTypeProxy.email)crmService.Retrieve(Microsoft.Crm.SdkTypeProxy.EntityName.email.ToString(), newEmailId, new Microsoft.Crm.Sdk.Query.AllColumns());

                    if (crmEmail != null)

                    {

                        QueryExpression query = new QueryExpression();

                        query.EntityName = EntityName.activitymimeattachment.ToString();

                        query.ColumnSet = new AllColumns();

     

                        ConditionExpression condition = new ConditionExpression();

                        condition.AttributeName = "activityid";

                        condition.Operator = ConditionOperator.Equal;

                        condition.Values = new string[] { crmEmail.activityid.Value.ToString() };

     

                        FilterExpression filter = new FilterExpression();

                        filter.FilterOperator = LogicalOperator.And;

                        //filter.Conditions = new ConditionExpression[] { condition };

                        filter.Conditions.Add(condition);

     

                        query.Criteria = filter;

     

                        // Retrieve the attachments

                        BusinessEntityCollection attachments = crmService.RetrieveMultiple(query);

                        foreach (activitymimeattachment attahment in attachments.BusinessEntities)

                        {

                            string attachid = attahment.activitymimeattachmentid.Value.ToString();

                            string objecttypecode = "1001"; //

                            string url = "http://YourServerNameOrIP:5555/Activities/Attachment/download.aspx?AttachmentType=" + objecttypecode + "&AttachmentId=" + attachid;

                            System.Net.WebClient myWebClient = new System.Net.WebClient();

                            //myWebClient.Credentials = System.Net.CredentialCache.DefaultCredentials;

                            myWebClient.Credentials = new NetworkCredential("Administrator", "pass@word1", "YourServerNameOrIP");

                            myWebClient.DownloadFile(url, @"C:\Downloads\" + attahment.filename);

                        }

                        return ActivityExecutionStatus.Closed;

                    }

                    else

                    {

                        return ActivityExecutionStatus.Faulting;

                    }

     

                }

                catch (System.Web.Services.Protocols.SoapException se)

                {

                    TextWriter w = new StreamWriter(@"C:\Downloads\SeCrmerror.txt");

                    w.WriteLine(se.Message +  "  " + se.InnerException.Message);

                    w.Close();

                    return ActivityExecutionStatus.Faulting;

                }

                catch (Exception ex)

                {

                    TextWriter w = new StreamWriter(@"C:\Downloads\ExCrmerror.txt");

                    w.WriteLine(ex.Message + "  " + ex.InnerException.Message);

                    w.Close();

                    return ActivityExecutionStatus.Faulting;

                }

     

            }

     

          }

    }

     

     

     

     

    Tom

     

    Thursday, July 31, 2008 5:28 AM

All replies

  • any one to help out .... please?

    Wednesday, July 30, 2008 12:42 AM
  • Hi All,

     

    I don't believe this, no one out there to help me or put some thoughs on this issue.

     

    Thursday, July 31, 2008 2:30 AM
  • I have developed a solution myself for this after doing R&D in CRM 4.0

     

    Here is the code to create workflow to download an attachment from email activity. Please change the username , password, server/domain and port details according  to your CRM server settings. Hope this will help

     

     

    Code Snippet

    namespace CustomActivityLibrary

    {

        [CrmWorkflowActivity("Custom Activity - Read Email Attachment", "Custom Activities Library")]

        public partial class CustomReadEmailAttachment: SequenceActivity

          {

                public CustomReadEmailAttachment()

                {

                      InitializeComponent();

                }

     

     

            public static DependencyProperty myEmailIdProperty =

                    DependencyProperty.Register("myEmailId", typeof(Lookup), typeof(CustomReadEmailAttachment));

     

     

            [CrmInput("My emailid")]

            [CrmReferenceTarget("email")]

            public Lookup myEmailId

            {

                get

                {

                    return (Lookup)base.GetValue(myEmailIdProperty);

                }

                set

                {

                    base.SetValue(myEmailIdProperty, value);

                }

            }

     

            protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)

            {

                //Get the context service

                IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));

                IWorkflowContext context = contextService.Context;

     

                //Use the context service to create an instance of CrmService

                ICrmService crmService = context.CreateCrmService();

     

                Guid newEmailId = myEmailId.Value;

     

                try

                {

                    Microsoft.Crm.SdkTypeProxy.email crmEmail = (Microsoft.Crm.SdkTypeProxy.email)crmService.Retrieve(Microsoft.Crm.SdkTypeProxy.EntityName.email.ToString(), newEmailId, new Microsoft.Crm.Sdk.Query.AllColumns());

                    if (crmEmail != null)

                    {

                        QueryExpression query = new QueryExpression();

                        query.EntityName = EntityName.activitymimeattachment.ToString();

                        query.ColumnSet = new AllColumns();

     

                        ConditionExpression condition = new ConditionExpression();

                        condition.AttributeName = "activityid";

                        condition.Operator = ConditionOperator.Equal;

                        condition.Values = new string[] { crmEmail.activityid.Value.ToString() };

     

                        FilterExpression filter = new FilterExpression();

                        filter.FilterOperator = LogicalOperator.And;

                        //filter.Conditions = new ConditionExpression[] { condition };

                        filter.Conditions.Add(condition);

     

                        query.Criteria = filter;

     

                        // Retrieve the attachments

                        BusinessEntityCollection attachments = crmService.RetrieveMultiple(query);

                        foreach (activitymimeattachment attahment in attachments.BusinessEntities)

                        {

                            string attachid = attahment.activitymimeattachmentid.Value.ToString();

                            string objecttypecode = "1001"; //

                            string url = "http://YourServerNameOrIP:5555/Activities/Attachment/download.aspx?AttachmentType=" + objecttypecode + "&AttachmentId=" + attachid;

                            System.Net.WebClient myWebClient = new System.Net.WebClient();

                            //myWebClient.Credentials = System.Net.CredentialCache.DefaultCredentials;

                            myWebClient.Credentials = new NetworkCredential("Administrator", "pass@word1", "YourServerNameOrIP");

                            myWebClient.DownloadFile(url, @"C:\Downloads\" + attahment.filename);

                        }

                        return ActivityExecutionStatus.Closed;

                    }

                    else

                    {

                        return ActivityExecutionStatus.Faulting;

                    }

     

                }

                catch (System.Web.Services.Protocols.SoapException se)

                {

                    TextWriter w = new StreamWriter(@"C:\Downloads\SeCrmerror.txt");

                    w.WriteLine(se.Message +  "  " + se.InnerException.Message);

                    w.Close();

                    return ActivityExecutionStatus.Faulting;

                }

                catch (Exception ex)

                {

                    TextWriter w = new StreamWriter(@"C:\Downloads\ExCrmerror.txt");

                    w.WriteLine(ex.Message + "  " + ex.InnerException.Message);

                    w.Close();

                    return ActivityExecutionStatus.Faulting;

                }

     

            }

     

          }

    }

     

     

     

     

    Tom

     

    Thursday, July 31, 2008 5:28 AM