Add html Tables while sending emails through custom workflow

I have created a custom workflow send email with table format dynamically in Dynamics CRM.

Email
My requirement is to create a html table and attach data dynamically.


Step1: First, I approached by query expression to get the data and total count.

           QueryExpression queryAccounts = new QueryExpression("entity");
            queryAccounts.ColumnSet = new ColumnSet(true);
            queryAccounts.ColumnSet = new ColumnSet(new String[] { "createdon", "subject", "statecode", "scheduledend", "createdby", "description" });
            queryAccounts.Criteria.AddCondition("subject", ConditionOperator.NotNull);
            queryAccounts.Criteria.AddCondition("statecode", ConditionOperator.Equal, 0);
            queryAccounts.Criteria.AddCondition(new ConditionExpression("createdby", ConditionOperator.In, values));

            EntityCollection collAccounts = service.RetrieveMultiple(queryAccounts);
            int count = collAccounts.Entities.Count();

Step2: Based on count, I started using stringbuilder format, To create html table in email body section.

                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.Append("<HTML><Head>");
                stringBuilder.Append("</head><body style='font-family:verdana'>");
                stringBuilder.Append("Dear Owner");
                stringBuilder.Append("<br/> Below are the list of this week.<br/><br/>");
                stringBuilder.Append("<Table border='1' cellspacing='0' cellpadding='3' style='width:100%; font-family:Segoe UI'>");
                stringBuilder.Append("<tr><th>Task Subject</th><th>Description</th><th>Due Date</th> </tr>");

                foreach (Entity entity in collAccounts.Entities)
                {
                //Based on your reqirements
                }

                stringBuilder.Append("</table>");
                stringBuilder.Append("</body>");
                stringBuilder.Append("</html>");

Step3: To send email without creating from and to emils dynamics by customworkflow. I have approached by using "activityparty" and "partyid" in my custom workflow and attached the strinbuilder in description


                Entity Fromparty = new Entity("activityparty");
                Fromparty.Attributes.Add("partyid", new EntityReference("systemuser", workflowContext.UserId));
                Entity Toparty = new Entity("activityparty");
                Toparty.Attributes.Add("partyid", new EntityReference("systemuser", erfUser.Id));

                Entity entity6 = new Entity("email");
                entity6["from"] = new Entity[] { Fromparty };
                entity6["to"] = new Entity[] { Toparty };
                entity6["subject"] = "Open task till today";
                entity6["description"] = Convert.ToString(stringBuilder); //stringBuilder.ToString();

                Guid guid = service.Create(entity6);

To send email by token,emailId. We used SendEmailRequest() and SendEmailResponse.
                SendEmailRequest req = new SendEmailRequest();
                req.EmailId = guid;
                req.TrackingToken = "";
                req.IssueSend = true;
                SendEmailResponse sendEmailResponse = (SendEmailResponse)service.Execute(req);

2 comments:

  1. Great job for publishing such a beneficial web site. Your web log isn’t only useful but it is additionally really creative too. เก้าอี้ทำงาน

    ReplyDelete