首页 > 代码库 > crm创建团队设置团队的业务部门
crm创建团队设置团队的业务部门
using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Messages;
/// <summary>
/// 团队
/// </summary>
public class TeamHelper
{
public static readonly string entityName = "team";
public Guid teamId = Guid.Empty;
/// <summary>
/// 创建团队
/// </summary>
/// <param name="service">服务</param>
public void Create(IOrganizationService service)
{
Entity en = new Entity() { LogicalName = entityName };
en["name"] = "软件开发团队";
teamId = service.Create(en);
}
/// <summary>
/// 设置团队的业务部门
/// </summary>
/// <param name="service">服务</param>
/// <param name="unitId">业务部门id</param>
public void SetParentUnit(IOrganizationService service,Guid unitId)
{
SetParentTeamRequest request = new SetParentTeamRequest();
//业务部门id
request.BusinessId = unitId;
//团队id
request.TeamId = teamId;
service.Execute(request);
}
/// <summary>
/// 修改团队
/// </summary>
/// <param name="service">服务</param>
public void Update(IOrganizationService service)
{
Entity en = new Entity() { LogicalName = entityName,Id = teamId };
en["name"] = "软件研发团队";
service.Update(en);
}
/// <summary>
/// 向团队中添加成员
/// </summary>
/// <param name="service">服务</param>
/// <param name="userArrays">用户id数组</param>
public void AddUser(IOrganizationService service,Guid[] userArrays)
{
AddMembersTeamRequest request = new AddMembersTeamRequest();
//团队
request.TeamId = teamId;
//用户id数组
request.MemberIds = userArrays;
service.Execute(request);
}
/// <summary>
/// 向团队中移除成员
/// </summary>
/// <param name="service">服务</param>
/// <param name="userArrays">用户id数组</param>
public void RemoveUser(IOrganizationService service, Guid[] userArrays)
{
RemoveMembersTeamRequest request = new RemoveMembersTeamRequest();
//团队
request.TeamId = teamId;
//用户id数组
request.MemberIds = userArrays;
service.Execute(request);
}
/// <summary>
/// 查询团队的权限
/// </summary>
/// <param name="service">服务</param>
public void SearchTeamRole(IOrganizationService service)
{
RetrieveTeamPrivilegesRequest request = new RetrieveTeamPrivilegesRequest();
//团队
request.TeamId = teamId;
RetrieveTeamPrivilegesResponse response = (RetrieveTeamPrivilegesResponse)service.Execute(request);
if (response.RolePrivileges != null)
{
RolePrivilege[] array = response.RolePrivileges;
}
}
/// <summary>
/// 添加和移除安全角色
/// </summary>
/// <param name="service">服务</param>
/// <param name="roleId">安全角色的id</param>
public void AddAndRemoveRole(IOrganizationService service,Guid roleId)
{
//添加安全角色
AddConnection(service, "teamroles_association", new EntityReference() { LogicalName = "role",Id = roleId });
//移除安全角色
RemoveConnection(service, "teamroles_association", new EntityReference() { LogicalName = "role", Id = roleId });
}
/// <summary>
/// 把一条记录分配给团队
/// </summary>
/// <param name="service">服务</param>
/// <param name="target">记录对象</param>
public void AssignToTeam(IOrganizationService service,EntityReference target)
{
AssignRequest request = new AssignRequest();
request.Target = target;
request.Assignee = new EntityReference() { LogicalName = entityName,Id = teamId };
service.Execute(request);
}
/// <summary>
/// 删除团队
/// </summary>
/// <param name="service">服务</param>
public void Delete(IOrganizationService service)
{
service.Delete(entityName, teamId);
}
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 = teamId };
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 = teamId };
request.RelatedEntities = new EntityReferenceCollection();
request.RelatedEntities.AddRange(array);
service.Execute(request);
}
}