/// <summary>
        /// Converts the contact into the user.
        /// </summary>
        /// <param name="contact">The contact.</param>
        /// <returns>The user.</returns>
        public CRMUser Convert(DynamicEntity contact)
        {
            Assert.ArgumentNotNull(contact, "contact");

            var nameProperty = (StringProperty)contact.Properties.ByName(Configuration.Settings.UniqueKeyProperty);
            var keyProperty  = (KeyProperty)contact.Properties.ByName("contactid");

            var emailProperty = contact.Properties.ByName("emailaddress1") as StringProperty;

            emailProperty = emailProperty ?? new StringProperty
            {
                Name  = "emailaddress1",
                Value = String.Empty
            };

            var descriptionProperty = contact.Properties.ByName("description") as StringProperty;

            descriptionProperty = descriptionProperty ?? new StringProperty
            {
                Name  = "description",
                Value = String.Empty
            };

            var createdProperty = contact.Properties.ByName("createdon") as CrmDateTimeProperty;

            createdProperty = createdProperty ?? new CrmDateTimeProperty
            {
                Name  = "createdon",
                Value = new CrmDateTime {
                    Value = DateTime.MinValue.ToString()
                }
            };

            var lastActivityDateProperty = contact.Properties.ByName("lastusedincampaign") as CrmDateTimeProperty;

            lastActivityDateProperty = lastActivityDateProperty ?? new CrmDateTimeProperty
            {
                Name  = "lastusedincampaign",
                Value = new CrmDateTime {
                    Value = DateTime.MinValue.ToString()
                }
            };

            var user = new CRMUser(
                nameProperty.Value,
                keyProperty.Value.Value,
                emailProperty.Value,
                null,
                descriptionProperty.Value,
                true,
                false,
                DateTime.Parse(createdProperty.Value.Value),
                DateTime.Now,
                DateTime.Parse(lastActivityDateProperty.Value.Value),
                DateTime.MinValue,
                DateTime.MinValue);

            var propertyToValueConverter = new PropertyToValueConverterV3();

            foreach (var property in contact.Properties)
            {
                user.SetPropertyValue(property.Name, propertyToValueConverter.Convert(property));
            }

            return(user);
        }
Beispiel #2
0
        public override bool UpdateUserProperties(string userName, Dictionary <string, object> properties)
        {
            Assert.ArgumentNotNull(userName, "userName");
            Assert.ArgumentNotNull(properties, "properties");

            const string UpdateUserProrpertiesKey = "updateUserProperties";

            ConditionalLog.Info(String.Format("UpdateUserProperties({0}). Started.", userName), this, TimerAction.Start, UpdateUserProrpertiesKey);

            var result = false;

            var user = this.UserRepository.GetUser(userName);

            if (user != null)
            {
                if (properties.ContainsKey("fullname"))
                {
                    this.ProcessFullNameProperty((string)properties["fullname"], properties);
                }

                var propertiesToUpdate = new List <Property>();

                var keyProperty = new KeyProperty();
                keyProperty.Name        = "contactid";
                keyProperty.Value       = new Key();
                keyProperty.Value.Value = user.ID;

                propertiesToUpdate.Add(keyProperty);
                foreach (var property in properties)
                {
                    this.AddPropertyToCollection(property.Key, property.Value, this.GetPropertyType(property.Key), propertiesToUpdate);
                }

                var update = new UpdateRequest
                {
                    Target = new TargetUpdateDynamic
                    {
                        Entity = new DynamicEntity
                        {
                            Name       = EntityName.contact.ToString(),
                            Properties = propertiesToUpdate.ToArray()
                        }
                    }
                };

                try
                {
                    this.CrmService.Execute(update);
                    ConditionalLog.Info(String.Format("UpdateUserProperties({0}). Updated in CRM.", userName), this, TimerAction.Tick, UpdateUserProrpertiesKey);

                    var propertyToValueConverter = new PropertyToValueConverterV3();
                    foreach (var property in propertiesToUpdate)
                    {
                        user.SetPropertyValue(property.Name, propertyToValueConverter.Convert(property));
                    }

                    result = true;
                }
                catch (Exception e)
                {
                    ConditionalLog.Error(String.Format("Couldn't save profile changes for the contact {0} in CRM.", userName), e, this);
                }
            }

            ConditionalLog.Info(String.Format("UpdateUserProperties({0}). Finished.", userName), this, TimerAction.Stop, UpdateUserProrpertiesKey);
            return(result);
        }