標籤:crm建立團隊 crm設定團隊的業務部門 crm添加和移除使用者 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"] = "軟體Team Dev";
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);
}
}