码迷,mamicode.com
首页 > 其他好文 > 详细

crm操作业务部门

时间:2014-07-16 14:41:00      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:crm操作业务部门   crm创建业务部门   crm设置业务部门的上级部门   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操作业务部门,布布扣,bubuko.com

crm操作业务部门

标签:crm操作业务部门   crm创建业务部门   crm设置业务部门的上级部门   crm停用或者启用业务部门   

原文地址:http://blog.csdn.net/y_f123/article/details/37874207

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!