码迷,mamicode.com
首页 > 其他好文 > 详细

crm创建启用停用用户

时间:2014-07-17 10:40:27      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:crm启用和停用用户   crm给用户添加安全角色   crm创建用户   crm操作用户   crm   

        public static readonly string entityName = "systemuser";
        public Guid userId = Guid.Empty;

        /// <summary>
        /// 创建用户
        /// </summary>
        public void Create(IOrganizationService service)
        {
            Entity en = new Entity() { LogicalName = entityName };
            en["domainname"] = "test";
            en["fullname"] = "测试";
            userId = service.Create(en);
        }

        /// <summary>
        /// 启用和停用用户
        /// </summary>
        /// <param name="service"></param>
        public void UpdateUserState(IOrganizationService service)
        {
            //启用用户
            UpdateState(entityName, userId, 0, -1, service);
            //停用用户
            UpdateState(entityName, userId, -1, -1, service);
        }

        /// <summary>
        /// 添加和移除安全角色
        /// </summary>
        /// <param name="service">服务</param>
        /// <param name="roleId">安全角色</param>
        public void AddAndRemoveRole(IOrganizationService service,Guid roleId)
        {
            //添加安全角色
            AddConnection(service, "systemuserroles_association", new EntityReference() { LogicalName = "role", Id = roleId });
            //删除安全角色
            RemoveConnection(service, "systemuserroles_association", new EntityReference() { LogicalName = "role", Id = roleId });
        }

        private void UpdateState(string enName, Guid id, int state, int status, IOrganizationService service)
        {
            SetStateRequest setState = new SetStateRequest()
            {
                EntityMoniker = new EntityReference()
                {
                    Id = id,
                    LogicalName = enName
                },
                State = new OptionSetValue(state),
                Status = new OptionSetValue(status)
            };
            service.Execute(setState);
        }

        public void AddConnection(IOrganizationService service, string name, params EntityReference[] array)
        {
            Relationship ship = new Relationship(name);
            AssociateRequest request = new AssociateRequest();
            request.Relationship = ship;
            request.Target = new EntityReference() { LogicalName = entityName, Id = userId };
            request.RelatedEntities = new EntityReferenceCollection();
            request.RelatedEntities.AddRange(array);
            service.Execute(request);
        }

        public void RemoveConnection(IOrganizationService service, string name, params EntityReference[] array)
        {
            Relationship ship = new Relationship(name);
            DisassociateRequest request = new DisassociateRequest();
            request.Relationship = ship;
            request.Target = new EntityReference() { LogicalName = entityName, Id = userId };
            request.RelatedEntities = new EntityReferenceCollection();
            request.RelatedEntities.AddRange(array);
            service.Execute(request);
        }

crm创建启用停用用户,布布扣,bubuko.com

crm创建启用停用用户

标签:crm启用和停用用户   crm给用户添加安全角色   crm创建用户   crm操作用户   crm   

原文地址:http://blog.csdn.net/y_f123/article/details/37900289

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!