Business Unit Helper
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 = ;
#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() > )
{
var parModel = result.FirstOrDefault();
if (model.order + != parModel.order)
{
parModel.order = model.order + ;
}
}
}
#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 AddAndRemoveTeam(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, , -);
//停用业务部门
UpdateState(service, businessunitId, -, -);
} /// <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);
}
}
Business Unit Helper的更多相关文章
- Business Unit Lookup in Form
Just add the below code in lookup() of StringEdit control in Form to get the Business Unit Lookup: p ...
- C++ Under the Hood
The original article is taken from http://msdn.microsoft.com/archive/en-us/dnarvc/html/jangrayhood.a ...
- [C1] Andrew Ng - AI For Everyone
About this Course AI is not only for engineers. If you want your organization to become better at us ...
- Java中实现SAX解析xml文件到MySQL数据库
大致步骤: 1.Java bean 2.DBHelper.java 3.重写DefaultHandler中的方法:MyHander.java 4.循环写数据库:SAXParserDemo.java ① ...
- API Design
REST API Design Guidelines V 1.0.201208 Draft 5 Last Updated: 08/31/2012 1 简介 本文档旨在规范REST API的 ...
- 不变(Invariant), 协变(Covarinat), 逆变(Contravariant) : 一个程序猿进化的故事
阿袁工作的第1天: 不变(Invariant), 协变(Covarinat), 逆变(Contravariant)的初次约 阿袁,早!开始工作吧. 阿袁在笔记上写下今天工作清单: 实现一个scala类 ...
- English Training Material - 03
Cross-cultural understanding (2) 1 The following text is about cultural diversity. Read it through o ...
- Top Five Communication Skills for Project Managers
Research among project managers globally identifies top communication skills for leading teams. Lead ...
- CRM PrincipalObjectAccess(POA)
PrincipalObjectAccess (POA) table is an important table which holds all grants share on CRM objects. ...
随机推荐
- python中的内容编码
一.python编码简介 1)编码格式简介 python解释器在加载 .py 文件中的代码时,会对内容进行编码(默认ASCII),ASCII(American Standard Code for In ...
- 避免jar依赖冲突的一种办法
java中的依赖冲突问题一直比较头疼,特别是做公用包给其他系统用的时候,现在都不敢引入太多的依赖,基本上每次都要帮别人解决依赖冲突的问题,非常麻烦. 特别是碰到一些老系统还不是用maven管理的,人家 ...
- OS考研复习笔记——操作系统的定义、目标、作用和发展的主要动力
计算机系统由硬件和软件两部分组成.操作系统(OS,Operating System)是配置在计算机硬件上的第一层软件,是对硬件系统的首次补充. 硬件:计算机物理设备,即各种处理机存储器.输入/输出设备 ...
- linux邮件客户端mutt日志文件,发不出邮件
linux上面使用很方便的收发邮件客户端(即MUA),还有一个是mail,大家也可以试试. 在网上找了很多,都说需要msmtp配合使用,其实mutt里面也内建了smtp支持的. mutt and SM ...
- Java实例---简单的投票系统
代码分析 InputData.java package vote; import java.io.BufferedReader; import java.io.IOException; import ...
- matlab 黑白格子
有一个生成黑白格子的函数 40 这个参数是改变大小的 img=checkerboard(40)<0.5; figure; imshow(img,[])
- svn使用教程及常见问题解决方案
使用教程转自:http://www.cnblogs.com/armyfai/p/3985660.html SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很多不同的版本 ...
- [日常]蒟蒻的高一生活 Week 1~3
博主在三周前成功晋级为高中生OwO终于不再是假的HZOIer感受一波HZ的真正生活...(然而实际上级部不少监督一直没启动23333) (然而三周过去好多事情都忘了QAQ瞎写好了(逃)) Week 1 ...
- python操作mysql二
游标 游标是一种能从包括多条数据记录的结果集中每次提取一条记录的机制,游标充当指针的作用,尽管游标能遍历结果中的所有行,但它一次只指向一行,游标的作用就是用于对查询数据库所返回的记录进行遍历,以便进行 ...
- D、CSL 的字符串 【栈+贪心】 (“新智认知”杯上海高校程序设计竞赛暨第十七届上海大学程序设计春季联赛)
题目传送门:https://ac.nowcoder.com/acm/contest/551#question 题目描述 CSL 以前不会字符串算法,经过一年的训练,他还是不会……于是他打算向你求助. ...