Dynamics 365 Workflow Activity- Send Email to Team Members by a Workflow(Add Team Users to the Email Recipients)

In Dynamics 365, you can’t send E-mail to the team members out of box. But workaround for this is easy.

  • First you create your Email in Workflow by using out of box Create Record step.
  • If you have any “To” or “CC” users other than team members, you add those users to the e-mail in first step.
  • Then use below custom Workflow Activity to add team members to the recipients list. It will update your previously created e-mail.
  • Then you use out of box “Send Email” step in workflow.
public class AddTeamToEmailRecipients : CodeActivity
        public InArgument<EntityReference> Team { get; set; }

        public InArgument<EntityReference> Email { get; set; }

        #region Service Parameters
        private ITracingService tracingService;
        private IWorkflowContext context;
        private IOrganizationServiceFactory serviceFactory;
        private IOrganizationService service;
        #endregion Service Parameters

        protected override void Execute(CodeActivityContext executionContext)
            #region Initializing Services
                tracingService = executionContext.GetExtension<ITracingService>();
                context = executionContext.GetExtension<IWorkflowContext>();
                serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
                service = serviceFactory.CreateOrganizationService(context.UserId);

                tracingService.Trace("Services are initialized.");
            catch (Exception e)

                throw new InvalidOperationException($"There was an error during initializing the services:  {e.Message}");

            #endregion Initializing Services

            #region Parameters
            tracingService.Trace("Reading Input Parameters");

            var teamRef = this.Team.Get(executionContext);
            var emailRef = this.Email.Get(executionContext);

            if (teamRef.Id == Guid.Empty || emailRef.Id == Guid.Empty)
                throw new InvalidPluginExecutionException("Invalid input parameters! Please contact with your System Administrator.");

            #endregion Parameters

            #region Retrieve Team Members
            tracingService.Trace("Starting Retrieving Team Members...");

            var teamMembers = service.RetrieveMultiple(new QueryExpression("teammembership")
                ColumnSet = new ColumnSet(true),
                Criteria = new FilterExpression(LogicalOperator.And)
                    Conditions =
                            new ConditionExpression("teamid", ConditionOperator.Equal, teamRef.Id)

            tracingService.Trace("Team Members are Retrieved...");

            if (!teamMembers.Any())
                tracingService.Trace($"The Team with the ID: {teamRef.Id} does not contain any users.");

            #endregion Retrieve Team Members

            #region Update Email

            EntityCollection recipientsCollection = new EntityCollection();

            foreach (var membership in teamMembers)
                Entity to = new Entity("activityparty");
                to["partyid"] = new EntityReference("systemuser", membership.GetAttributeValue<Guid>("systemuserid"));


            tracingService.Trace($"Starting Update Email Recipients for the Email with the ID: {emailRef.Id} ...");

            Entity updateEmail = new Entity("email", emailRef.Id);
            updateEmail["to"] = recipientsCollection;

            tracingService.Trace($"Updated Email Recipients...");
            #endregion Update Email

One thought on “Dynamics 365 Workflow Activity- Send Email to Team Members by a Workflow(Add Team Users to the Email Recipients)

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 )

Facebook photo

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

Connecting to %s