Sunday, 1 September 2013

Plugin to send an email

        private void SendEmail(IOrganizationService service, Entity entity, Entity postImageEntity)
        {
            Email email = new Email();
            string optAttendees = "";
            string reqAttendees = "";
            string status = "";
            #region Get Server Url
            string serverUrl = GetServerUrl(service);
            string recordUrl = serverUrl + "/main.aspx?etn=" + entity.LogicalName + "&pagetype=entityrecord&id=" + entity.Id.ToString();
            #endregion
            EntityCollection requiredAttendees = new EntityCollection();
            DateTime startDate = new DateTime();
            DateTime endDate = new DateTime();
            string subject = "";
            string descriptoion = "";
            string minutesofMeeting = "";

            #region Get value from Appointment/Recurring Appointment
            EntityReference regardingObjectId = new EntityReference();
            OptionSetValue activityReference = new OptionSetValue();
            EntityCollection optionalAttendees = new EntityCollection();
            EntityReference ownerId = (EntityReference)postImageEntity["ownerid"];
         
            if (postImageEntity.Attributes.Contains("scheduledstart"))
            {
                startDate = (DateTime)postImageEntity["scheduledstart"];
                startDate = startDate.ToLocalTime();
            }
            if (postImageEntity.Attributes.Contains("scheduledend"))
            {
                endDate = (DateTime)postImageEntity["scheduledend"];
                endDate = endDate.ToLocalTime();
            }
            if (postImageEntity.Attributes.Contains("subject"))
            {
                subject = postImageEntity["subject"].ToString();
            }
            if (postImageEntity.Attributes.Contains("description"))
            {
                descriptoion = postImageEntity["description"].ToString();
            }
         
            if (postImageEntity.Attributes.Contains("lbg_activityreference"))
            {
                activityReference = (OptionSetValue)postImageEntity["lbg_activityreference"];
                if (activityReference.Value == 862870000)
                {
                    status = "Business Meeting";
                }
                if (activityReference.Value == 862870001)
                {
                    status = "Client Annual Review";
                }
                if (activityReference.Value == 862870002)
                {
                    status = "Client Query / Request";
                }
                if (activityReference.Value == 862870003)
                {
                    status = "Client Call";
                }
                if (activityReference.Value == 862870004)
                {
                    status = "Deal Pitch";
                }
                if (activityReference.Value == 862870005)
                {
                    status = "Product Introduction";
                }
                if (activityReference.Value == 862870006)
                {
                    status = "e-Channel Setup";
                }
                if (activityReference.Value == 862870007)
                {
                    status = "Mifid";
                }
                if (activityReference.Value == 862870008)
                {
                    status = "Marketing Event";
                }
                if (activityReference.Value == 862870009)
                {
                    status = "Product Roadshow";
                }
                if (activityReference.Value == 862870010)
                {
                    status = "Internal Client Meeting";
                }
                if (activityReference.Value == 862870011)
                {
                    status = "Other";
                }
                if (activityReference.Value == 862870012)
                {
                    status = "News Information";
                }
                if (activityReference.Value == 862870013)
                {
                    status = "Research Material";
                }
                if (activityReference.Value == 862870014)
                {
                    status = "Client Complaint";
                }
                if (activityReference.Value == 862870016)
                {
                    status = "Client Financial Results";
                }
                if (activityReference.Value == 862870017)
                {
                    status = "Collaboration Topic";
                }
                if (activityReference.Value == 862870018)
                {
                    status = "Credit Annual Review";
                }
                if (activityReference.Value == 862870019)
                {
                    status = "Customer Charter – Full Day";
                }
                if (activityReference.Value == 862870020)
                {
                    status = "Customer Charter – F2F Pricing";
                }
            }
            EntityCollection template = GetTemplateDetails(service, "Appointment Send Minutes");
            EntityReference userId = (EntityReference)postImageEntity["createdby"];
            if (postImageEntity.Attributes.Contains("regardingobjectid"))
            {
                regardingObjectId = (EntityReference)postImageEntity["regardingobjectid"];
            }
            if (postImageEntity.Attributes.Contains("optionalattendees"))
            {
                optionalAttendees = postImageEntity.GetAttributeValue<EntityCollection>("optionalattendees");
            }
            if (postImageEntity.Attributes.Contains("requiredattendees"))
            {
                requiredAttendees = postImageEntity.GetAttributeValue<EntityCollection>("requiredattendees");
            }
            #endregion
            ActivityParty fromParty = new ActivityParty
            {

                PartyId = new EntityReference(SystemUser.EntityLogicalName, userId.Id)
            };
            #region Retreive Participants
            ActivityParty[] sendTo = new ActivityParty[optionalAttendees.Entities.Count + requiredAttendees.Entities.Count];
            for (int i = 0; i < optionalAttendees.Entities.Count; i++)
            {
                ActivityParty toParty = new ActivityParty();
                Guid partyId = ((EntityReference)((EntityCollection)postImageEntity["optionalattendees"]).Entities[i].Attributes["partyid"]).Id;
                if (((EntityReference)((EntityCollection)postImageEntity["optionalattendees"]).Entities[i].Attributes["partyid"]).LogicalName == "systemuser")
                {
                    toParty.PartyId = new EntityReference(SystemUser.EntityLogicalName, partyId);
                    sendTo[i] = toParty;
                }
                optAttendees += ((EntityReference)((EntityCollection)postImageEntity["optionalattendees"]).Entities[i].Attributes["partyid"]).Name + ",";
            }
            int j = 0;
            for (int i = optionalAttendees.Entities.Count; i < requiredAttendees.Entities.Count + optionalAttendees.Entities.Count; i++)
            {

                ActivityParty toParty = new ActivityParty();
                Guid partyId = ((EntityReference)((EntityCollection)postImageEntity["requiredattendees"]).Entities[j].Attributes["partyid"]).Id;
                if (((EntityReference)((EntityCollection)postImageEntity["requiredattendees"]).Entities[j].Attributes["partyid"]).LogicalName == "systemuser")
                {
                    toParty.PartyId = new EntityReference(SystemUser.EntityLogicalName, partyId);
                    sendTo[i] = toParty;
                }
                reqAttendees += ((EntityReference)((EntityCollection)postImageEntity["requiredattendees"]).Entities[j].Attributes["partyid"]).Name + ",";
                j++;
            }
            #endregion
            #region Create email
            if (optAttendees.Length > 0)
            {
               optAttendees= optAttendees.Remove(optAttendees.Length - 1);
            }
           
            foreach (Entity emailTemplate in template.Entities)
            {
                email.Subject = GetDataFromXml(emailTemplate["subject"].ToString(), "match");
                email.Description = GetDataFromXml(emailTemplate.Attributes["body"].ToString(), "match");
                email.Subject = email.Subject.ToString().Replace("[Subject]", subject);
                email.Description = email.Description.ToString().Replace("[Description]", descriptoion);
                email.Description = email.Description.ToString().Replace("[Activity Reference]", status);
                email.Description = email.Description.ToString().Replace("[Minutes of meeting]", minutesofMeeting);
                email.Description = email.Description.ToString().Replace("[Start Time]", Convert.ToString(startDate));
                email.Description = email.Description.ToString().Replace("[End Time]", Convert.ToString(endDate));
                email.Description = email.Description.ToString().Replace("[Optional Attendees]", optAttendees);
                email.Description = email.Description.ToString().Replace("[Rquired Attendees]", reqAttendees);
                string urlToReplace = "<a href='" + recordUrl + "'>" +recordUrl+"</a>";
                email.Description = email.Description.ToString().Replace("URL:[URL", urlToReplace);
            }
            email.To = sendTo;
          
            //email.OwnerId = ownerId;
            email.From = new ActivityParty[] { fromParty };
            email.DirectionCode = true;
            Guid _emailId = service.Create(email);
            #endregion
            #region send mail

No comments:

Post a Comment