Пример #1
0
        private Guid CreateTemplatedEmail(IOrganizationService service, EntityReference incident, Guid templateId)
        {
            InstantiateTemplateRequest request = new InstantiateTemplateRequest()
            {
                TemplateId = templateId,
                ObjectId   = incident.Id,
                ObjectType = incident.LogicalName
            };

            try
            {
                InstantiateTemplateResponse response = (InstantiateTemplateResponse)service.Execute(request);

                Entity email   = response.EntityCollection[0];
                Guid   emailId = service.Create(email);
                return(emailId);
            }
            catch (FaultException <OrganizationServiceFault> ex)
            {
                throw new Exception("XrmWorkflowTools.CreateEmailFromTemplate.CreateTemplatedEmail: " + ex.Message);
            }
            catch (System.Exception ex)
            {
                throw new Exception("XrmWorkflowTools.CreateEmailFromTemplate.CreateTemplatedEmail: " + ex.Message);
            }
        }
Пример #2
0
        public JsonResult PreviewEmail(EmailEntity crmEmail)
        {
            log.Info("Entered PreviewEmail constructor");


            var activityPartyBccColl = new EntityCollection();

            if (crmEmail.ToAll)
            {
                PopulateAllContactsFromFilter(crmEmail, activityPartyBccColl);
            }
            else
            {
                activityPartyBccColl = PopulateContactsProvided(crmEmail, activityPartyBccColl);
            }

            Entity toContact = new Entity("contact");

            toContact.Id = ((EntityReference)activityPartyBccColl.Entities[0]["partyid"]).Id;

            var    jobjTemplate  = (JObject)crmEmail.Template;
            string strTemplateId = null;

            if (jobjTemplate != null)
            {
                strTemplateId = getPropertyValue(jobjTemplate, "Id");
            }
            var emailId = Guid.Empty;

            EntityCollection coll = new EntityCollection();

            if (!string.IsNullOrEmpty(strTemplateId))
            {
                UpdateContactWithPersonalNotes(crmEmail.EmailText, crmEmail.EmailSubject, crmEmail.EmailBody, toContact.Id);
                InstantiateTemplateRequest instTemplate = new InstantiateTemplateRequest();
                instTemplate.TemplateId = new Guid(strTemplateId);
                instTemplate.ObjectId   = toContact.Id;
                instTemplate.ObjectType = "contact";

                InstantiateTemplateResponse instTemplateResponse = (InstantiateTemplateResponse)ServiceProxy.Execute(instTemplate);

                Entity emailPreview = (Entity)instTemplateResponse.EntityCollection.Entities[0];
                coll.Entities.Add(emailPreview);
            }



            log.Info("Exiting PreviewEmail constructor");

            return(new JsonResult()
            {
                Data = GetResultSet(coll)
            });
        }
            /// <summary>
            /// Creates an Email entity from a template and value entity
            /// </summary>
            /// <param name="service">The organization service</param>
            /// <param name="tracingService">The tracing service</param>
            /// <param name="templateId">The id of the template to instantiate</param>
            /// <param name="entityReference">The entity to use to substitute values in the template</param>
            /// <returns>An Email Entity</returns>
            public static List <Email> InstantiateTemplate(IOrganizationService service, ITracingService tracingService, Guid templateId, EntityReference entityReference)
            {
                InstantiateTemplateRequest request = new InstantiateTemplateRequest();

                request.TemplateId = templateId;
                request.ObjectId   = entityReference.Id;
                request.ObjectType = entityReference.LogicalName;

                InstantiateTemplateResponse response = (InstantiateTemplateResponse)service.Execute(request);

                return(response.EntityCollection.Entities.Select(e => e.ToEntity <Email>()).ToList());
            }
Пример #4
0
        /// <summary>
        /// Render email template data
        /// </summary>
        /// <param name="templateId">template id</param>
        /// <param name="primaryEntityId">primary entity id</param>
        /// <param name="primaryEntityName">primary entity name</param>
        /// <param name="service">organization service</param>
        /// <returns>rendered email object</returns>
        internal static Entity RenderEmailTemplateData(Guid templateId, Guid primaryEntityId, string primaryEntityName, IOrganizationService service)
        {
            // Instantiate Email template and return Email object
            InstantiateTemplateRequest instTemplateReq = new InstantiateTemplateRequest
            {
                TemplateId = templateId,
                ObjectId   = primaryEntityId,
                ObjectType = primaryEntityName
            };
            InstantiateTemplateResponse instTemplateResp = (InstantiateTemplateResponse)service.Execute(instTemplateReq);

            return(instTemplateResp.EntityCollection[0]);
        }
Пример #5
0
        private Entity GetEmailByTemplate(string strTemplateId, Entity toContact)
        {
            var emailId = Guid.Empty;
            InstantiateTemplateRequest instTemplate = new InstantiateTemplateRequest();

            instTemplate.TemplateId = new Guid(strTemplateId);
            instTemplate.ObjectId   = ((EntityReference)toContact["partyid"]).Id;
            instTemplate.ObjectType = "contact";

            InstantiateTemplateResponse instTemplateResponse = (InstantiateTemplateResponse)ServiceProxy.Execute(instTemplate);

            var emailCrmEntity = (Entity)instTemplateResponse.EntityCollection.Entities[0];

            return(emailCrmEntity);
        }
        private Entity GetEmailInstantiateFromTemplate(Entity template, Guid userId)
        {
            InstantiateTemplateRequest instantiateTemplateRequest = new InstantiateTemplateRequest()
            {
                ObjectId = userId
                ,
                ObjectType = "systemuser"
                ,
                TemplateId = template.Id
            };

            InstantiateTemplateResponse instantiateTemplateResponse = this.CrmOrganisationService.Execute(instantiateTemplateRequest) as InstantiateTemplateResponse;

            return(instantiateTemplateResponse.EntityCollection.Entities[0]);
        }
Пример #7
0
        /// <summary>
        /// Create an e-mail using a template.
        /// </summary>
        /// <param name="serverConfig">Contains server connection information.</param>
        /// <param name="promptforDelete">When True, the user will be prompted to delete all
        /// created entities.</param>
        public void Run(ServerConnection.Configuration serverConfig, bool promptforDelete)
        {
            try
            {
                // Connect to the Organization service. 
                // The using statement assures that the service proxy will be properly disposed.
                using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri,
                                                                     serverConfig.Credentials, serverConfig.DeviceCredentials))
                {
                    // This statement is required to enable early-bound type support.
                    _serviceProxy.EnableProxyTypes();

                    CreateRequiredRecords();

                    //<snippetCreateEmailUsingTemplate1>

                    // Use the InstantiateTemplate message to create an e-mail message using a template.
                    InstantiateTemplateRequest instTemplateReq = new InstantiateTemplateRequest
                    {
                        TemplateId = _templateId,
                        ObjectId = _accountId,
                        ObjectType = Account.EntityLogicalName
                    };
                    InstantiateTemplateResponse instTemplateResp = (InstantiateTemplateResponse)_serviceProxy.Execute(instTemplateReq);

                    // Serialize the email message to XML, and save to a file.
                    XmlSerializer serializer = new XmlSerializer(typeof(InstantiateTemplateResponse));
                    string filename = "email-message.xml";
                    using (StreamWriter writer = new StreamWriter(filename))
                    {
                        serializer.Serialize(writer, instTemplateResp);
                    }
                    Console.WriteLine("Created e-mail using the template.");
                    //</snippetCreateEmailUsingTemplate1>


                    DeleteRequiredRecords(promptforDelete);
                }
            }

            // Catch any service fault exceptions that Microsoft Dynamics CRM throws.
            catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>)
            {
                // You can handle an exception here or pass it back to the calling method.
                throw;
            }
        }
Пример #8
0
        /// <summary>
        /// Create an e-mail using a template.
        /// </summary>
        /// <param name="serverConfig">Contains server connection information.</param>
        /// <param name="promptforDelete">When True, the user will be prompted to delete all
        /// created entities.</param>
        public void Run(ServerConnection.Configuration serverConfig, bool promptforDelete)
        {
            try
            {
                // Connect to the Organization service.
                // The using statement assures that the service proxy will be properly disposed.
                using (_serviceProxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri,
                                                                    serverConfig.Credentials, serverConfig.DeviceCredentials))
                {
                    // This statement is required to enable early-bound type support.
                    _serviceProxy.EnableProxyTypes();

                    CreateRequiredRecords();

                    //<snippetCreateEmailUsingTemplate1>

                    // Use the InstantiateTemplate message to create an e-mail message using a template.
                    InstantiateTemplateRequest instTemplateReq = new InstantiateTemplateRequest
                    {
                        TemplateId = _templateId,
                        ObjectId   = _accountId,
                        ObjectType = Account.EntityLogicalName
                    };
                    InstantiateTemplateResponse instTemplateResp = (InstantiateTemplateResponse)_serviceProxy.Execute(instTemplateReq);

                    // Serialize the email message to XML, and save to a file.
                    XmlSerializer serializer = new XmlSerializer(typeof(InstantiateTemplateResponse));
                    string        filename   = "email-message.xml";
                    using (StreamWriter writer = new StreamWriter(filename))
                    {
                        serializer.Serialize(writer, instTemplateResp);
                    }
                    Console.WriteLine("Created e-mail using the template.");
                    //</snippetCreateEmailUsingTemplate1>


                    DeleteRequiredRecords(promptforDelete);
                }
            }

            // Catch any service fault exceptions that Microsoft Dynamics CRM throws.
            catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> )
            {
                // You can handle an exception here or pass it back to the calling method.
                throw;
            }
        }
Пример #9
0
        private Entity GetEmailInstantiateFromTemplate(Entity template, Guid userId)
        {
            TraceService.Trace("Email Template with ID {0} is going to be instanciated by user {1}", template.Id, userId);
            InstantiateTemplateRequest instantiateTemplateRequest = new InstantiateTemplateRequest()
            {
                ObjectId = userId
                ,
                ObjectType = "systemuser"
                ,
                TemplateId = template.Id
            };

            InstantiateTemplateResponse instantiateTemplateResponse = this.OrganizationService.Execute(instantiateTemplateRequest) as InstantiateTemplateResponse;
            Entity email = instantiateTemplateResponse.EntityCollection.Entities[0];

            TraceService.Trace("Email Template has been instanciated correctly. Email ID:{0}", email.Id);
            return(email);
        }
Пример #10
0
 public static void CreateEmailFromTemplate(IOrganizationService service, EntityCollection sendFromCol, EntityCollection sendToCol, string emailSubject, string previewTriggerVaule, EntityReference regard, Guid contactId, Entity template, string btnOrder)
 {
     if (template != null)
     {
         // Use the InstantiateTemplate message to create an e-mail message using a template.
         InstantiateTemplateRequest instTemplateReq = new InstantiateTemplateRequest
         {
             TemplateId = template.Id,
             ObjectId   = contactId,
             ObjectType = "contact"
         };
         InstantiateTemplateResponse instTemplateResp = (InstantiateTemplateResponse)service.Execute(instTemplateReq);
         Entity email = instTemplateResp.EntityCollection.Entities[0];
         email.Attributes["new_campaigninvatationid"] = previewTriggerVaule;
         email.Attributes["subject"] = emailSubject;
         email.Attributes.Add("from", sendFromCol);
         service.Create(email);
     }
 }
Пример #11
0
        public Entity CreateEmailFromTemplate(IOrganizationService service, EntityReference template, Guid userId)
        {
            var request = new InstantiateTemplateRequest
            {
                TemplateId = template.Id,
                ObjectId   = userId,
                ObjectType = "systemuser"
            };

            var response = (InstantiateTemplateResponse)service.Execute(request);

            var entity = response.EntityCollection.Entities.FirstOrDefault();

            if (entity == null)
            {
                throw new Exception(String.Format("Unable to create an email from the {0} template.", template.Name));
            }

            return(entity);
        }
Пример #12
0
        protected Tuple <string, string> GetEmailTemplateDescription(Guid templateGuid, Guid sysUser)
        {
            InstantiateTemplateRequest templateRequest = new InstantiateTemplateRequest();

            //object id is the system user since it is a general template
            templateRequest.ObjectId   = sysUser;
            templateRequest.ObjectType = SystemUser.EntityLogicalName;
            var body = ""; var subject = "";

            templateRequest.TemplateId = templateGuid;

            InstantiateTemplateResponse instTempResp = (InstantiateTemplateResponse)_service.Execute(templateRequest);

            if (instTempResp.EntityCollection.Entities != null && instTempResp.EntityCollection.Entities.Count > 0)
            {
                body    = (string)instTempResp.EntityCollection.Entities[0].Attributes["description"];
                subject = (string)instTempResp.EntityCollection.Entities[0].Attributes["subject"];
                return(new Tuple <string, string>(body, subject));
            }
            return(new Tuple <string, string>(body, subject));
        }
Пример #13
0
        private static Entity GenerateTemplate(IOrganizationService service, Guid templateId, Guid recordId, string logicalName)
        {
            try
            {
                InstantiateTemplateRequest request = new InstantiateTemplateRequest
                {
                    TemplateId = templateId,
                    ObjectId   = recordId,
                    ObjectType = logicalName
                };

                InstantiateTemplateResponse response = (InstantiateTemplateResponse)service.Execute(request);

                return(response.EntityCollection.Entities.Count == 1
                    ? response.EntityCollection.Entities[0]
                    : null);
            }
            catch (Exception)
            {
                throw new InvalidPluginExecutionException("Template type does not match entity in record url");
            }
        }
        public void RetrieveExistingTemplate()
        {
            Entity template = TemplatesHelper.RetrieveTemplateByName(TemplateName, this.CrmOrganisationService);

            Assert.IsTrue(template.Contains("presentationxml"));

            InstantiateTemplateRequest instantiateTemplateRequest = new InstantiateTemplateRequest()
            {
                ObjectId = Systemuser.GetCallingUserID(this.CrmOrganisationService)
                ,
                ObjectType = "systemuser"
                ,
                TemplateId = template.Id
            };

            InstantiateTemplateResponse instantiateTemplateResponse = this.CrmOrganisationService.Execute(instantiateTemplateRequest) as InstantiateTemplateResponse;

            Assert.IsNotNull(instantiateTemplateResponse.EntityCollection.Entities[0]);

            Entity email = instantiateTemplateResponse.EntityCollection.Entities[0];

            Assert.IsTrue(email.Contains("description"));
        }
Пример #15
0
        public Entity CreateEmailWithAttachments(string strTemplateId, Entity toContact, EntityCollection activityPartyToColl, EntityCollection activityPartyFromColl)
        {
            var emailId = Guid.Empty;
            InstantiateTemplateRequest instTemplate = new InstantiateTemplateRequest();

            instTemplate.TemplateId = new Guid(strTemplateId);
            instTemplate.ObjectId   = ((EntityReference)toContact["partyid"]).Id;
            instTemplate.ObjectType = "contact";

            InstantiateTemplateResponse instTemplateResponse = (InstantiateTemplateResponse)ServiceProxy.Execute(instTemplate);

            var emailCrmEntity = (Entity)instTemplateResponse.EntityCollection.Entities[0];

            emailCrmEntity.Attributes.Add("to", activityPartyToColl);
            emailCrmEntity.Attributes.Add("from", activityPartyFromColl);
            emailCrmEntity.Attributes.Add("directioncode", true);

            Entity template = ServiceProxy.Retrieve("template", new Guid(strTemplateId), new ColumnSet(true));

            emailId           = ServiceProxy.Create(emailCrmEntity);
            emailCrmEntity.Id = emailId;
            CreateActivityMimeAttachments(template, emailId, false, true);
            return(emailCrmEntity);
        }
Пример #16
0
        protected Tuple<string, string> GetEmailTemplateDescription(Guid templateGuid, Guid sysUser)
        {
            InstantiateTemplateRequest templateRequest = new InstantiateTemplateRequest();
            //object id is the system user since it is a general template
            templateRequest.ObjectId = sysUser;
            templateRequest.ObjectType = SystemUser.EntityLogicalName;
            var body = ""; var subject = "";
            templateRequest.TemplateId = templateGuid;

            InstantiateTemplateResponse instTempResp = (InstantiateTemplateResponse)_service.Execute(templateRequest);
            if (instTempResp.EntityCollection.Entities != null && instTempResp.EntityCollection.Entities.Count > 0)
            {
                body = (string)instTempResp.EntityCollection.Entities[0].Attributes["description"];
                subject = (string)instTempResp.EntityCollection.Entities[0].Attributes["subject"];
                return new Tuple<string, string>(body, subject);

            }
            return new Tuple<string, string>(body, subject);
        }
Пример #17
0
        [STAThread] // Added to support UX
        static void Main(string[] args)
        {
            CrmServiceClient service = null;

            try
            {
                service = SampleHelpers.Connect("Connect");
                if (service.IsReady)
                {
                    #region Sample Code
                    ////////////////////////////////////
                    #region Set up
                    SetUpSample(service);
                    #endregion Set up
                    #region Demonstrate


                    // Use the InstantiateTemplate message to create an e-mail message using a template.
                    var instTemplateReq = new InstantiateTemplateRequest
                    {
                        TemplateId = _templateId,
                        ObjectId   = _accountId,
                        ObjectType = Account.EntityLogicalName
                    };

                    var instTemplateResp = (InstantiateTemplateResponse)service.Execute(instTemplateReq);

                    // Serialize the email message to XML, and save to a file.
                    var    serializer = new XmlSerializer(typeof(InstantiateTemplateResponse));
                    string filename   = "email-message.xml";
                    using (var writer = new StreamWriter(filename))
                    {
                        serializer.Serialize(writer, instTemplateResp);
                    }
                    Console.WriteLine("Created e-mail using the template.");

                    #region Clean up
                    CleanUpSample(service);
                    #endregion Clean up
                }
                #endregion Demonstrate
                #endregion Sample Code
                else
                {
                    const string UNABLE_TO_LOGIN_ERROR = "Unable to Login to Microsoft Dataverse";
                    if (service.LastCrmError.Equals(UNABLE_TO_LOGIN_ERROR))
                    {
                        Console.WriteLine("Check the connection string values in cds/App.config.");
                        throw new Exception(service.LastCrmError);
                    }
                    else
                    {
                        throw service.LastCrmException;
                    }
                }
            }
            catch (Exception ex)
            {
                SampleHelpers.HandleException(ex);
            }

            finally
            {
                if (service != null)
                {
                    service.Dispose();
                }

                Console.WriteLine("Press <Enter> to exit.");
                Console.ReadLine();
            }
        }
Пример #18
0
 public Entity InitialiseEmailFromTemplate(string regardingType, Guid regardingId, Guid templateId)
 {
     var request = new InstantiateTemplateRequest()
     {
         ObjectId = regardingId,
         ObjectType = regardingType,
         TemplateId = templateId
     };
     var response = (InstantiateTemplateResponse) Execute(request);
     return response.EntityCollection.Entities.First();
 }