概述
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创建启用停用用户的全部内容,希望文章能够帮你解决crm创建启用停用用户所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复