Пример #1
0
        private List <SubscriberDetail> GenerateSubscribersList(IEnumerable <Entity> contacts, string primaryEmail, MetadataHelper mdh)
        {
            trace.Trace("Generating Subscriber List");
            var subscribers = new List <SubscriberDetail>();

            foreach (Entity contact in contacts)
            {
                // remove the primary email field, it's sent as a separate param and we don't want duplicate fields
                var email = contact[primaryEmail].ToString();
                var name  = contact["fullname"].ToString();

                // check to make sure this contact isn't duplicated within the filter for the config
                if (!config.SyncDuplicateEmails && SharedLogic.CheckEmailIsDuplicate(orgService, config, primaryEmail, email))
                {
                    continue;
                }

                var fields = SharedLogic.ContactAttributesToSubscriberFields(orgService, trace, contact, contact.Attributes.Keys);
                fields = SharedLogic.PrettifySchemaNames(mdh, fields);

                subscribers.Add(new SubscriberDetail {
                    EmailAddress = email,
                    Name         = name,
                    CustomFields = fields
                });
            }

            return(subscribers);
        }
Пример #2
0
        public void SendMessage(Entity target)
        {
            var emailField = target["campmon_email"].ToString();
            List <SubscriberCustomField> contactData = JsonConvert.DeserializeObject <List <SubscriberCustomField> >(target["campmon_data"].ToString());

            // If campmon_syncduplicates = 'false', do a retrieve multiple for any contact
            //      that matches the email address found in the sync email of the contact.
            // if yes : set campmon_error on message to "Duplicate email"
            if (!campaignMonitorConfig.SyncDuplicateEmails)
            {
                var contactEmail = contactData.Where(x => x.Key == emailField).FirstOrDefault();
                if (contactEmail == null)
                {
                    tracer.Trace("The email field to sync was not found within the data for this message.");
                    target["campmon_error"] = "The email field to sync was not found within the data for this message.";
                    orgService.Update(target);
                    return;
                }

                bool emailIsDuplicate = SharedLogic.CheckEmailIsDuplicate(orgService, campaignMonitorConfig, emailField, contactEmail.Value.ToString());
                if (emailIsDuplicate)
                {
                    tracer.Trace("Duplicate email");
                    target["campmon_error"] = "Duplicate email";
                    orgService.Update(target);
                    return;
                }
            }

            try
            {
                SendSubscriberToList(campaignMonitorConfig.ListId, emailField, contactData);
            }
            catch (Exception ex)
            {
                target["campmon_error"] = ex.Message;
                orgService.Update(target);
                return;
            }

            tracer.Trace("User successfully sent to CM.");

            // deactivate msg if successful create/update
            target["statuscode"] = new OptionSetValue(2);
            target["statecode"]  = new OptionSetValue(1);
            orgService.Update(target);
        }