Search This Blog

Thursday, May 12, 2016

Create Email Activity in CRM Plugin


public void Execute(IServiceProvider serviceProvider)
        {
            try
            {
                IPluginExecutionContext context =
                   (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));


                // Get a reference to the Organization service.
                IOrganizationServiceFactory factory =                    (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

                IOrganizationService service = factory.CreateOrganizationService(context.UserId);
             
 if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    Entity objDetailDTO = (Entity)context.InputParameters["Target"];

                    ActivityParty fromUser = new ActivityParty
                    {
                        PartyId = new EntityReference(SystemUser.EntityLogicalName, objDetailDTO.Attributes["systemuserid"])
                    };

                    Email email = new Email();
                    email.From = new ActivityParty[] { fromUser };

                    email.To = new ActivityParty[] { /*Whom you want to send email(activityparty)*/ };

                    //email.Bcc = ccUsers.ToArray();

                    email.Subject = "subject";

                    email.Description = "body";

                    //Assign regarding    
                    email.RegardingObjectId = new EntityReference(objDetailDTO.Attributes["contactid"]);
                   
                    Guid emailGUID = service.Create(email);
                }
            }
            catch (FaultException<OrganizationServiceFault> ex)
            {
                if (ex != null)
                    throw new InvalidPluginExecutionException(string.Format("An error occurred : {0}", 
ex.Message), ex);

            }
        }

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.