Пример #1
0
        /// <summary>
        /// Gets the role.
        /// </summary>
        /// <param name="roleName">Name of the role.</param>
        /// <returns>The role.</returns>
        public virtual CRMRole GetRole(string roleName)
        {
            Assert.ArgumentNotNull(roleName, "roleName");

            const string GetRoleKey = "getRole";

            ConditionalLog.Info(string.Format("GetRole({0}). Started.", roleName), this, TimerAction.Start, GetRoleKey);

            var role = (CRMRole)this.CacheService.RoleCache.Get(roleName);

            if (role != null)
            {
                ConditionalLog.Info(string.Format("GetRole({0}). Finished (retrieved role from cache).", roleName), this, TimerAction.Stop, GetRoleKey);
                return(role);
            }

            role = this.GetRoleFromCrm(roleName);
            if (role != null)
            {
                ConditionalLog.Info(string.Format("GetRole({0}). Retrieved role from CRM.", roleName), this, TimerAction.Tick, GetRoleKey);
                this.CacheService.RoleCache.Add(role);
            }

            ConditionalLog.Info(string.Format("GetRole({0}). Finished.", roleName), this, TimerAction.Stop, GetRoleKey);
            return(role);
        }
Пример #2
0
        protected override bool AddUsersToRoles(List <CRMUser> users, List <CRMRole> roles)
        {
            const string AddUsersToRolesKey = "addUsersToRoles";

            ConditionalLog.Info("AddUsersToRoles(...). Started.", this, TimerAction.Start, AddUsersToRolesKey);

            var result = true;

            foreach (var role in roles)
            {
                foreach (var user in users)
                {
                    var request = new AddMemberListRequest();
                    request.EntityId = user.ID;
                    request.ListId   = role.ID;

                    try
                    {
                        this.organizationServiceCache.GetOrganizationService().Execute(request);
                        ConditionalLog.Info(String.Format("AddUsersToRoles(...). User {0} has been added to the {1} role.", user.Name, role.Name), this, TimerAction.Tick, AddUsersToRolesKey);

                        this.CacheService.MembersCache.Remove(role.Name);
                        this.CacheService.MemberOfCache.Remove(user.Name);
                    }
                    catch (Exception e)
                    {
                        ConditionalLog.Error(String.Format("Couldn't add contact {0} to marketing list {1} in CRM.", user.Name, role.Name), e, this);
                        result = false;
                    }
                }
            }

            ConditionalLog.Info("AddUsersToRoles(...). Finished.", this, TimerAction.Stop, AddUsersToRolesKey);
            return(result);
        }
        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 Dictionary <string, object>();

                propertiesToUpdate.Add("contactid", user.ID);
                foreach (var property in properties)
                {
                    this.AddPropertyToCollection(property.Key, property.Value, this.GetPropertyType(property.Key), propertiesToUpdate);
                }

                var update = new UpdateRequest();
                update.Target             = new Entity();
                update.Target.LogicalName = "contact";
                foreach (var property in propertiesToUpdate)
                {
                    update.Target.Attributes.Add(property.Key, property.Value);
                }

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

                    foreach (var property in propertiesToUpdate)
                    {
                        user.SetPropertyValue(property.Key, property.Value);
                    }

                    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);
        }
Пример #4
0
        public override bool DeactivateRole(string roleName)
        {
            Assert.ArgumentNotNull(roleName, "roleName");

            const string DeactivateRoleKey = "deactivateRole";

            ConditionalLog.Info(String.Format("DeactivateRole({0}). Started.", roleName), this, TimerAction.Start, DeactivateRoleKey);

            var result = false;

            var role = this.GetRole(roleName);

            if (role != null)
            {
                var request = new SetStateRequest();
                request.EntityMoniker = new EntityReference("list", role.ID);
                request.State         = new OptionSetValue {
                    Value = 1
                };
                request.Status = new OptionSetValue {
                    Value = 1
                };

                try
                {
                    this.organizationServiceCache.GetOrganizationService().Execute(request);
                    ConditionalLog.Info(String.Format("DeactivateRole({0}). Role has been deactivated in CRM.", roleName), this, TimerAction.Tick, DeactivateRoleKey);

                    this.CacheService.MemberOfCache.Clear();
                    this.CacheService.MembersCache.Remove(roleName);
                    this.CacheService.RoleCache.Remove(roleName);

                    result = true;
                }
                catch (Exception e)
                {
                    ConditionalLog.Error(String.Format("Couldn't deactivate role {0} in CRM.", roleName), e, this);
                }
            }

            ConditionalLog.Info(String.Format("DeactivateRole({0}). Finished.", roleName), this, TimerAction.Stop, DeactivateRoleKey);
            return(result);
        }
Пример #5
0
        /// <summary>
        /// Creates CRM metadata service.
        /// </summary>
        /// <param name="settings">The configuration settings</param>
        /// <returns>The CRM metadata service.</returns>
        public IMetadataServiceV3 CreateMetadataService(ConfigurationSettings settings)
        {
            Assert.ArgumentNotNull(settings, "settings");

            const string CreateMetadataServiceKey = "createMetadataService";

            ConditionalLog.Info("CreateMetadataService(settings). Started.", this, TimerAction.Start, CreateMetadataServiceKey);

            MetadataService metadataService = null;

            try
            {
                metadataService             = new MetadataService();
                metadataService.Url         = settings.Url.Replace("crmservice.asmx", "metadataservice.asmx");
                metadataService.Timeout     = 360 * 1000;
                metadataService.Credentials = CrmHelper.CreateNetworkCredential(settings.User, settings.Password);
                metadataService.UnsafeAuthenticatedConnectionSharing = settings.UnsafeAuthenticatedConnectionSharing;
                metadataService.PreAuthenticate     = settings.PreAuthenticate;
                metadataService.ConnectionGroupName = CrmHelper.GetConnectionGroupName((NetworkCredential)metadataService.Credentials);

                metadataService.GetTimestamp();

                ConditionalLog.Info("CreateMetadataService(settings). CRM metadata service has been created.", this, TimerAction.Tick, CreateMetadataServiceKey);
            }
            catch (Exception e)
            {
                ConditionalLog.Error("Couldn't create CRM metadata service.", e, this);
                return(null);
            }
            finally
            {
                ConditionalLog.Info("CreateMetadataService(settings). Finished.", this, TimerAction.Stop, CreateMetadataServiceKey);
            }

            return(metadataService);
        }
Пример #6
0
        /// <summary>
        /// Creates CRM service.
        /// </summary>
        /// <param name="settings">The configuration settings</param>
        /// <returns>The CRM service.</returns>
        public ICrmServiceV3 CreateService(ConfigurationSettings settings)
        {
            Assert.ArgumentNotNull(settings, "settings");

            const string CreateServiceKey = "createService";

            ConditionalLog.Info("CreateService(settings). Started.", this, TimerAction.Start, CreateServiceKey);

            CrmService crmService = null;

            try
            {
                crmService             = new CrmService();
                crmService.Url         = settings.Url;
                crmService.Timeout     = 360 * 1000;
                crmService.Credentials = CrmHelper.CreateNetworkCredential(settings.User, settings.Password);
                crmService.UnsafeAuthenticatedConnectionSharing = settings.UnsafeAuthenticatedConnectionSharing;
                crmService.PreAuthenticate     = settings.PreAuthenticate;
                crmService.ConnectionGroupName = CrmHelper.GetConnectionGroupName((NetworkCredential)crmService.Credentials);

                crmService.Execute(new WhoAmIRequest());

                ConditionalLog.Info("CreateService(settings). CRM service has been created.", this, TimerAction.Tick, CreateServiceKey);
            }
            catch (Exception e)
            {
                ConditionalLog.Error("Couldn't create CRM service.", e, this);
                return(null);
            }
            finally
            {
                ConditionalLog.Info("CreateService(settings). Finished.", this, TimerAction.Stop, CreateServiceKey);
            }

            return(crmService);
        }
        /// <summary>
        /// Gets the user properties.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <param name="propertyNames">The property names.</param>
        /// <returns>The user properties.</returns>
        public virtual Dictionary <string, object> GetUserProperties(string userName, string[] propertyNames)
        {
            Assert.ArgumentNotNull(userName, "userName");
            Assert.ArgumentNotNull(propertyNames, "propertyNames");

            const string GetUserPropertiesKey = "getUserProperties";

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

            try
            {
                var user = this.UserRepository.GetUser(userName, propertyNames);
                if (user != null)
                {
                    return(user.ProfilePropertyNames.Where(propertyNames.Contains).ToDictionary(p => p, p => this.GetPropertyValue(user, p)));
                }

                return(new Dictionary <string, object>());
            }
            finally
            {
                ConditionalLog.Info(String.Format("GetUserProperties({0}). Finished.", userName), this, TimerAction.Stop, GetUserPropertiesKey);
            }
        }
Пример #8
0
        /// <summary>
        /// Gets the roles.
        /// </summary>
        /// <param name="roleNames">The role names.</param>
        /// <returns>The roles.</returns>
        public virtual List <CRMRole> GetRoles(string[] roleNames)
        {
            Assert.ArgumentNotNull(roleNames, "roleNames");

            const string GetRolesKey = "getRoles";

            ConditionalLog.Info(string.Format("GetRoles({0}). Started.", string.Join(",", roleNames)), this, TimerAction.Start, GetRolesKey);

            var result = new Dictionary <string, CRMRole>();

            var roles = roleNames.Select(n => (CRMRole)this.CacheService.RoleCache.Get(n)).Where(r => r != null).ToList();

            foreach (var role in roles)
            {
                ConditionalLog.Info(string.Format("GetRoles({0}). Retrieved role {1} from cache.", string.Join(",", roleNames), role.Name), this, TimerAction.Tick, GetRolesKey);
                result.Add(role.Name, role);
            }

            if (result.Count != roleNames.Length)
            {
                roles = this.GetRolesFromCrm(roleNames.Except(result.Keys).ToArray());
                ConditionalLog.Info(string.Format("GetRoles({0}). Retrieved {1} role(s) from CRM.", string.Join(",", roleNames), roles.Count), this, TimerAction.Tick, GetRolesKey);

                foreach (var role in roles)
                {
                    if (!result.ContainsKey(role.Name))
                    {
                        result.Add(role.Name, role);
                        this.CacheService.RoleCache.Add(role);
                    }
                }
            }

            ConditionalLog.Info(string.Format("GetRoles({0}). Finished.", string.Join(",", roleNames)), this, TimerAction.Stop, GetRolesKey);
            return(result.Values.ToList());
        }
Пример #9
0
        /// <summary>
        /// Gets all roles.
        /// </summary>
        /// <returns>The roles.</returns>
        public virtual List <CRMRole> GetAllRoles()
        {
            const string GetAllRolesKey = "getAllRoles";

            ConditionalLog.Info("GetAllRoles(). Started.", this, TimerAction.Start, GetAllRolesKey);

            var roles = this.GetAllRolesFromCrm();

            ConditionalLog.Info(string.Format("GetAllRoles(). Retrieved {0} role(s) from CRM.", roles.Count), this, TimerAction.Tick, GetAllRolesKey);

            var result = new Dictionary <string, CRMRole>();

            foreach (var role in roles)
            {
                if (!result.ContainsKey(role.Name))
                {
                    result.Add(role.Name, role);
                    this.CacheService.RoleCache.Add(role);
                }
            }

            ConditionalLog.Info("GetAllRoles(). Finished.", this, TimerAction.Stop, GetAllRolesKey);
            return(result.Values.ToList());
        }
Пример #10
0
        public override bool CreateRole(string roleName)
        {
            Assert.ArgumentNotNullOrEmpty(roleName, "roleName");

            const string CreateRoleKey = "createRole";

            ConditionalLog.Info(String.Format("CreateRole({0}). Started.", roleName), this, TimerAction.Start, CreateRoleKey);

            var marketingList = new Entity();

            marketingList.LogicalName        = "list";
            marketingList["listname"]        = roleName;
            marketingList["membertype"]      = 2;
            marketingList["createdfromcode"] = new OptionSetValue {
                Value = 2
            };

            var result = false;

            try
            {
                var id = this.organizationServiceCache.GetOrganizationService().Create(marketingList);
                ConditionalLog.Info(String.Format("CreateRole({0}). Role has been created in CRM.", roleName), this, TimerAction.Tick, CreateRoleKey);

                this.CacheService.RoleCache.Add(new CRMRole(roleName, id));

                result = true;
            }
            catch (Exception e)
            {
                ConditionalLog.Error(String.Format("Couldn't create role {0} in CRM.", roleName), e, this);
            }

            ConditionalLog.Info(String.Format("CreateRole({0}). Finished.", roleName), this, TimerAction.Stop, CreateRoleKey);
            return(result);
        }