crm操作业务部门

using System;

using System.Linq;

using Microsoft.Xrm.Sdk;

using Microsoft.Crm.Sdk.Messages;

using System.Collections.Generic;

using Microsoft.Xrm.Sdk.Messages;

/// <summary>

/// 操作业务部门

/// </summary>

public class BusinessUnitHelper

{

public static readonly string entityName = "businessunit";

public Guid businessunitId = Guid.Empty;

/// <summary>

/// 创建业务部门

/// </summary>

public void CreateUnit(IOrganizationService service)

{

Entity en = new Entity() { LogicalName = entityName };

en["name"] = "软件开发部";

businessunitId = service.Create(en);

}

/// <summary>

/// 修改业务部门

/// </summary>

public void UpdateUnit(IOrganizationService service)

{

Entity en = new Entity() { LogicalName = entityName,Id = businessunitId };

en["name"] = "软件开发事业部";

service.Update(en);

}

/// <summary>

/// 设置业务部门的上级部门

/// </summary>

/// <param name="service">服务</param>

/// <param name="parentId">上级业务部门id</param>

public void SetParentBusinessUnit(IOrganizationService service,Guid parentId)

{

SetParentBusinessUnitRequest request = new SetParentBusinessUnitRequest();

//当前业务部门id

request.BusinessUnitId = businessunitId;

//上级业务部门id

request.ParentId = parentId;

service.Execute(request);

}

/// <summary>

/// 检索业务部门层次结构中的所有业务部门

/// </summary>

public void SearchBusinessUnit(IOrganizationService service)

{

RetrieveBusinessHierarchyBusinessUnitRequest request = new RetrieveBusinessHierarchyBusinessUnitRequest();

request.EntityId = businessunitId;

request.ColumnSet = new Microsoft.Xrm.Sdk.Query.ColumnSet("name", "parentbusinessunitid");

RetrieveBusinessHierarchyBusinessUnitResponse response =

(RetrieveBusinessHierarchyBusinessUnitResponse)service.Execute(request);

if (response != null && response.EntityCollection != null)

{

List<BusinessUnit> list = new List<BusinessUnit>();

EntityCollection ecResult = response.EntityCollection;

int index = 1;

#region 获取所有的部门

foreach (Entity en in ecResult.Entities)

{

BusinessUnit unitE = new BusinessUnit();

unitE.businessUnitId = en.Id;

unitE.name = en.Contains("name") && en["name"] != null ? en["name"].ToString() : string.Empty;

if (en.Contains("parentbusinessunitid") && en["parentbusinessunitid"] != null)

{

EntityReference parEr = en["parentbusinessunitid"] as EntityReference;

unitE.parentBusinessUnitId = parEr.Id;

}

unitE.order = index;

list.Add(unitE);

index++;

}

#endregion

#region 重置业务部门的顺序

foreach (BusinessUnit model in list)

{

var result = list.Where(a => a.parentBusinessUnitId == a.businessUnitId);

if (result != null && result.Count() > 0)

{

var parModel = result.FirstOrDefault();

if (model.order + 1 != parModel.order) {

parModel.order = model.order + 1;

}

}

}

#endregion

}

}

public class BusinessUnit

{

public Guid businessUnitId { get; set; }

public string name { get; set; }

public Guid parentBusinessUnitId { get; set; }

public int order { get; set; }

}

/// <summary>

/// 添加用户或者移除用户

/// </summary>

/// <param name="service">服务</param>

/// <param name="userId">用户</param>

public void AddAndRemoveUser(IOrganizationService service,Guid userId)

{

//添加用户

AddConnection(service, "business_unit_system_users",

new EntityReference() { LogicalName = "systemuser", Id = userId });

//移除用户

RemoveConnection(service, "business_unit_system_users",

new EntityReference() { LogicalName = "systemuser", Id = userId });

}

/// <summary>

/// 添加团队或者移除团队

/// </summary>

/// <param name="service">服务</param>

/// <param name="teamId">团队</param>

public void AddAndRemoveUser(IOrganizationService service, Guid teamId)

{

//添加团队

AddConnection(service, "business_unit_teams",

new EntityReference() { LogicalName = "team", Id = teamId });

//移除团队

RemoveConnection(service, "business_unit_teams",

new EntityReference() { LogicalName = "team", Id = teamId });

}

/// <summary>

/// 停用或者启用业务部门

/// </summary>

public void SetBusinessUnitState(IOrganizationService service)

{

//启用业务部门

UpdateState(service, businessunitId, 0, -1);

//停用业务部门

UpdateState(service, businessunitId, -1, -1);

}

/// <summary>

/// 删除业务部门

/// </summary>

public void DeleteUnit(IOrganizationService service)

{

service.Delete(entityName, businessunitId);

}

private void UpdateState(IOrganizationService service,Guid id, int state, int status)

{

SetStateRequest setState = new SetStateRequest()

{

EntityMoniker = new EntityReference()

{

Id = id,

LogicalName = entityName

},

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 = businessunitId };

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 = businessunitId };

request.RelatedEntities = new EntityReferenceCollection();

request.RelatedEntities.AddRange(array);

service.Execute(request);

}

}

crm操作业务部门

时间: 2024-10-31 09:03:58

crm操作业务部门的相关文章

crm操作权限

using System; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; using System.Collections.Generic; using Microsoft.Crm.Sdk.Messages; /// <summary> /// 权限 /// </summary> public class PrivilegeHelper { public static readonly string entityNa

crm操作安全字段

using System; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Messages; /// <summary> /// 安全字段 /// </summary> public class FieldSecurityProfileHelper { public static readonly string entityName = "fieldsecurityprofile"; public static

crm操作安全角色

using System; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; using System.Collections.Generic; /// <summary> /// 安全角色 /// </summary> public class RoleHelper { public static readonly string entityName = "role"; public Guid

crm操作报价单实体

using System; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; using Microsoft.Xrm.Sdk.Query; /// <summary> /// 报价单 /// </summary> public class QuoteHelper { public static readonly string entityName = "quote"; public Guid q

crm操作约会实体

using System; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; using Microsoft.Xrm.Sdk.Query; /// <summary> /// 约会 /// </summary> public class AppointmentHelper { public static readonly string entityName = "appointment"; pu

crm操作知识库文章实体

using System; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; using Microsoft.Xrm.Sdk.Query; /// <summary> /// 知识库文章 /// </summary> public class KbArticleHelper { public static readonly string entityName = "kbarticle"; pub

crm操作案例实体

using System; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; using Microsoft.Xrm.Sdk.Query; /// <summary> /// 案例 /// </summary> public class IncidentHelper { public static readonly string entityName = "incident"; public G

crm操作发票实体

using System; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; using Microsoft.Crm.Sdk.Messages; /// <summary> /// 发票 /// </summary> public class InvoiceHelper { public static readonly string entityName = "invoice"; public Gui

crm操作销售订单实体

using System; using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; using Microsoft.Crm.Sdk.Messages; /// <summary> /// 销售订单 /// </summary> public class SoleOrderHelper { public static readonly string entityName = "salesorder"; pub