Dynamics 365 IOrganizationService Extentions

今天继续发一个长期以来累积的Dynamics CRM/365开发扩展的辅助类ServiceProxyExt,分享给关注我的小伙伴们,没有关注的也不要忘记成为关注我的小伙伴哦;

/*
 * file name:lce.mscrm.engine.ServiceProxyExt.cs
 * author:lynx lynx.kor@163.com @ 2018/9/19 14:25:28
 * copyright (c) 2018 Copyright@lynxce.com
 * desc:
 * > add description for ServiceProxyExt
 * revision:
 *
*/

using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace lce.mscrm.engine
{
    /// <summary>
    /// action:ServiceProxyExt
    /// </summary>
    public static class ServiceProxyExt
    {
        /// <summary>
        /// 添加用户到团队
        /// </summary>
        /// <param name="service"></param>
        /// <param name="teamid"> </param>
        /// <param name="userids"></param>
        public static void AddMembersTeam(this IOrganizationService service, Guid teamid, Guid[] userids)
        {
            var request = new AddMembersTeamRequest
            {
                TeamId = teamid,
                MemberIds = userids
            };
            service.Execute(request);
        }

        /// <summary>
        /// 分派;把target分派给owner
        /// </summary>
        /// <param name="service">         </param>
        /// <param name="ownerEntityName"> </param>
        /// <param name="ownerId">         </param>
        /// <param name="targetEntityName"></param>
        /// <param name="targetId">        </param>
        public static void Assign(this IOrganizationService service, string ownerEntityName, Guid ownerId, string targetEntityName, Guid targetId)
        {
            var assign = new AssignRequest
            {
                Assignee = new EntityReference(ownerEntityName, ownerId),
                Target = new EntityReference(targetEntityName, targetId)
            };
            service.Execute(assign);
        }

        /// <summary>
        /// 分派;把target分派给owner
        /// </summary>
        /// <param name="service"></param>
        /// <param name="owner">  </param>
        /// <param name="target"> </param>
        public static void Assign(this IOrganizationService service, EntityReference owner, EntityReference target)
        {
            var assign = new AssignRequest
            {
                Assignee = owner,
                Target = target
            };
            service.Execute(assign);
        }

        /// <summary>
        /// 给实体指定查找字段建立关联关系
        /// </summary>
        /// <param name="service">         </param>
        /// <param name="sourceEntityName">查找实体</param>
        /// <param name="sourceId">        查找实体数据id</param>
        /// <param name="targetEntityName">关联实体</param>
        /// <param name="targetFieldName"> 关联字段</param>
        /// <param name="targetId">        关联数据id</param>
        public static void Associate(this IOrganizationService service, string sourceEntityName, Guid sourceId, string targetEntityName, string targetFieldName, Guid targetId)
        {
            var target = service.Retrieve(targetEntityName, targetId, new ColumnSet(new string[] { $@"{targetEntityName}id" }));
            target[targetFieldName] = new EntityReference(sourceEntityName, sourceId);
            service.Update(target);
        }

        /// <summary>
        /// 实体间建立数据1:N/N:N关系
        /// </summary>
        /// <param name="service">          </param>
        /// <param name="relationshipName"> </param>
        /// <param name="targetEntityName"> </param>
        /// <param name="tartgetEntityId">  </param>
        /// <param name="relatedEntityName"></param>
        /// <param name="relatedEntityIds"> </param>
        public static void Associate(this IOrganizationService service, string relationshipName, string targetEntityName, Guid tartgetEntityId, string relatedEntityName, IList<Guid> relatedEntityIds)
        {
            var relatedEntities = new EntityReferenceCollection();
            foreach (var relatedId in relatedEntityIds)
            {
                relatedEntities.Add(new EntityReference(relatedEntityName, relatedId));
            }
            var relationship = new Relationship(relationshipName);
            service.Associate(targetEntityName, tartgetEntityId, relationship, relatedEntities);
        }

        /// <summary>
        /// 更改用户业务部门
        /// </summary>
        /// <param name="service"></param>
        /// <param name="userId"> </param>
        /// <param name="bizId">  </param>
        public static void ChangeUserBiz(this IOrganizationService service, Guid userId, Guid bizId)
        {
            var request = new SetBusinessSystemUserRequest
            {
                BusinessId = bizId,
                UserId = userId,
                ReassignPrincipal = new EntityReference("systemuser", userId)
            };
            service.Execute(request);
        }

        /// <summary>
        /// 统计数据行
        /// </summary>
        /// <param name="service"> </param>
        /// <param name="fetchXml"></param>
        /// <returns></returns>
        public static int Count(this IOrganizationService service, string fetchXml)
        {
            service.RetrieveMultiple(fetchXml, out int totals);
            return totals;
        }

        /// <summary>
        /// 统计数据行
        /// </summary>
        /// <param name="service">   </param>
        /// <param name="entityName"></param>
        /// <param name="filterXml"> </param>
        /// <returns></returns>
        public static int Count(this IOrganizationService service, string entityName, string filterXml = "")
        {
            var fetchXml = FetchXmlExt.FetchXml(entityName, FetchXmlExt.QueryColumns(entityName, $"{entityName}id")
                , filterXml, page: 1, size: 1);
            return service.Count(fetchXml);
        }

        /// <summary>
        /// 统计数据行
        /// </summary>
        /// <param name="service">   </param>
        /// <param name="entityName"></param>
        /// <param name="filters">   </param>
        /// <returns></returns>
        public static int Count(this IOrganizationService service, string entityName, FilterExpression filters = null)
        {
            var query = new QueryExpression
            {
                Distinct = false,
                EntityName = entityName
            };
            query.ColumnSet.AddColumn($@"{entityName}id");
            query.PageInfo = new PagingInfo
            {
                Count = 1,
                PageNumber = 1,
                ReturnTotalRecordCount = true,
                PagingCookie = null
            };
            if (null != filters) query.Criteria.AddFilter(filters);
            var result = service.RetrieveMultiple(query);
            if (null != result)
            {
                return result.TotalRecordCount;
            }
            return 0;
        }

        /// <summary>
        /// 批量创建Entity
        /// </summary>
        /// <param name="service"> </param>
        /// <param name="entities"></param>
        /// <param name="istrans"> </param>
        /// <returns></returns>
        public static bool Create(this IOrganizationService service, IList<Entity> entities, bool istrans = true)
        {
            if (null == entities || !entities.Any()) return false;
            if (istrans)
            {
                var request = new ExecuteTransactionRequest()
                {
                    ReturnResponses = true,
                    Requests = new OrganizationRequestCollection()
                };
                foreach (var entity in entities)
                {
                    request.Requests.Add(new CreateRequest { Target = entity });
                }
                service.Execute(request);
                return true;
            }
            foreach (var entity in entities)
            {
                service.Create(entity);
            }
            return true;
        }

        /// <summary>
        /// 根据entity获取EntityMetadata
        /// </summary>
        /// <param name="service">   </param>
        /// <param name="entityName"></param>
        /// <returns></returns>
        public static EntityMetadata EntityMetadata(this IOrganizationService service, string entityName)
        {
            var request = new RetrieveEntityRequest
            {
                LogicalName = entityName,
                EntityFilters = EntityFilters.Attributes
            };
            var response = (RetrieveEntityResponse)service.Execute(request);
            return response.EntityMetadata;
        }

        /// <summary>
        /// 根据id查询实体指定字段
        /// </summary>
        /// <param name="service">   </param>
        /// <param name="entityName"></param>
        /// <param name="id">        </param>
        /// <param name="columns">   </param>
        /// <returns></returns>
        public static Entity Get(this IOrganizationService service, string entityName, Guid id, string columns)
        {
            return service.Get(entityName, id, columns.Split(new[] { ',' }));
        }

        /// <summary>
        /// 根据id查询实体指定字段
        /// </summary>
        /// <param name="service">   </param>
        /// <param name="entityName"></param>
        /// <param name="id">        </param>
        /// <param name="columns">   </param>
        /// <returns></returns>
        public static Entity Get(this IOrganizationService service, string entityName, Guid id, IList<string> columns = null)
        {
            return service.Get(entityName, new[] { new ConditionItem($@"{entityName}id", id) }, columns);
        }

        /// <summary>
        /// 根据字段名及值查询实体指定字段
        /// </summary>
        /// <param name="service">   </param>
        /// <param name="entityName"></param>
        /// <param name="fieldName"> </param>
        /// <param name="filedValue"></param>
        /// <param name="columns">   </param>
        /// <returns></returns>
        public static Entity Get(this IOrganizationService service, string entityName, string fieldName, object filedValue, IList<string> columns = null)
        {
            return service.Get(entityName, new[] { new ConditionItem(fieldName, filedValue) }, columns);
        }

        /// <summary>
        /// </summary>
        /// <param name="service">   </param>
        /// <param name="entityName"></param>
        /// <param name="conditions"></param>
        /// <param name="columns">   </param>
        /// <returns></returns>
        public static Entity Get(this IOrganizationService service, string entityName, IList<ConditionItem> conditions, IList<string> columns = null)
        {
            return service.List(entityName, 1, 1, out int totals, conditions, columns)?.FirstOrDefault();
        }

        /// <summary>
        /// 获取statecode为0的数据行
        /// </summary>
        /// <param name="service">   </param>
        /// <param name="entityName"></param>
        /// <param name="fieldName"> </param>
        /// <param name="filedValue"></param>
        /// <param name="columns">   </param>
        /// <returns></returns>
        public static Entity GetState0(this IOrganizationService service, string entityName, string fieldName, object filedValue, IList<string> columns = null)
        {
            return service.Get(entityName, new[] { new ConditionItem(fieldName, filedValue), new ConditionItem("statecode", 0) }, columns);
        }

        /// <summary>
        /// 共享记录(读与写)
        /// </summary>
        /// <param name="owner"> 要共享给用户或者团队</param>
        /// <param name="target">要共享的记录</param>
        public static void Grant(this IOrganizationService service, EntityReference owner, EntityReference target, bool writeable = false)
        {
            var accessMask = AccessRights.ReadAccess;
            if (writeable) accessMask = AccessRights.WriteAccess | AccessRights.ReadAccess;
            var grantAccessRequest = new GrantAccessRequest
            {
                Target = target,
                PrincipalAccess = new PrincipalAccess
                {
                    Principal = owner,
                    AccessMask = accessMask
                }
            };
            service.Execute(grantAccessRequest);
        }

        /// <summary>
        /// 列表查询
        /// </summary>
        /// <param name="service">   </param>
        /// <param name="entityName"></param>
        /// <param name="conditions"></param>
        /// <param name="columns">   </param>
        /// `
        /// <returns></returns>
        public static List<Entity> List(this IOrganizationService service, string entityName, IList<ConditionItem> conditions, IList<string> columns = null, IList<OrderItem> orders = null)
        {
            var fetchXml = FetchXmlExt.FetchXml(entityName, columns, conditions, orders);
            return service.RetrieveMultiple(fetchXml);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="service">   </param>
        /// <param name="entityName"></param>
        /// <param name="page">      </param>
        /// <param name="size">      </param>
        /// <param name="totals">    </param>
        /// <param name="conditions"></param>
        /// <param name="columns">   </param>
        /// <param name="orders">    </param>
        /// <returns></returns>
        public static List<Entity> List(this IOrganizationService service, string entityName, int page, int size, out int totals, IList<ConditionItem> conditions = null, IList<string> columns = null, IList<OrderItem> orders = null)
        {
            var fetchXml = FetchXmlExt.FetchXml(entityName, columns, conditions, orders, page, size);
            return service.RetrieveMultiple(fetchXml, out totals);
        }

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <param name="service">   </param>
        /// <param name="entityName"></param>
        /// <param name="conditions"></param>
        /// <param name="columns">   </param>
        /// <returns></returns>
        public static List<Entity> ListAll(this IOrganizationService service, string entityName, IList<ConditionItem> conditions, IList<string> columns = null)
        {
            var fetchXml = FetchXmlExt.FetchXml(entityName, columns, conditions);
            return service.RetrieveAll(fetchXml);
        }

        /// <summary>
        /// 数据合并
        /// </summary>
        /// <param name="service"> </param>
        /// <param name="entities"></param>
        /// <returns></returns>
        public static Entity Merge(this IOrganizationService service, IEnumerable<Entity> entities)
        {
            if (entities.Count() < 2)
            {
                throw new ArgumentException("entities needs 2 entity at least;");
            }
            entities = entities.OrderByDescending(x => (DateTime)x.Attributes["modifiedon"]);
            var arr = entities.ToArray();
            var main = arr[0];
            for (int i = 1; i < arr.Length; i++)
            {
                service.Merge(main, arr[i]);
            }
            return main;
        }

        /// <summary>
        /// 数据合并
        /// </summary>
        /// <param name="service">   </param>
        /// <param name="entityMain"></param>
        /// <param name="entitiesub"></param>
        /// <returns></returns>
        public static Entity Merge(this IOrganizationService service, Entity entityMain, Entity entitiesub)
        {
            var target = new EntityReference
            {
                Id = entityMain.Id,
                LogicalName = entityMain.LogicalName
            };
            var merge = new MergeRequest
            {
                SubordinateId = entitiesub.Id,
                Target = target,
                PerformParentingChecks = false,
                UpdateContent = entitiesub
            };
            service.Execute(merge);
            return entityMain;
        }

        / <summary>
        / </summary>
        / <param name="service">         </param>
        / <param name="targetEntityName"></param>
        / <param name="targetId">        </param>
        //public static void AssignShopOwner(this IOrganizationService service, string targetEntityName, Guid targetId)
        //{
        //    var domain = System.Configuration.ConfigurationManager.AppSettings["shopDomainName"];
        //    var query = new QueryExpression
        //    {
        //        Distinct = false,
        //        EntityName = "systemuser",
        //        ColumnSet = new ColumnSet("systemuserid"),
        //        Criteria =
        //        {
        //            Filters =
        //            {
        //                new FilterExpression
        //                {
        //                    FilterOperator=LogicalOperator.And,
        //                    Conditions =
        //                    {
        //                        new ConditionExpression("domainname",ConditionOperator.Equal,domain)
        //                    }
        //                }
        //            }
        //        }
        //    };
        //    var collection = service.RetrieveMultiple(query);
        //    if (null != collection)
        //    {
        //        if (collection.Entities.Count > 0)
        //        {
        //            var entity = collection.Entities[0];
        //            Assign(service, entity.LogicalName, entity.Id, targetEntityName, targetId);
        //        }
        //    }
        //}
        /// <summary>
        /// </summary>
        /// <param name="service">   </param>
        /// <param name="entityName"></param>
        /// <param name="entityId">  </param>
        /// <param name="stateCode"> </param>
        /// <param name="statusCode"></param>
        public static void ModifyStatus(this IOrganizationService service, string entityName, Guid entityId, int stateCode, int statusCode = -1)
        {
            var req = new SetStateRequest
            {
                EntityMoniker = new EntityReference(entityName, entityId),
                State = new OptionSetValue(stateCode),
                Status = new OptionSetValue(statusCode)
            };
            service.Execute(req);
        }

        /// <summary>
        /// 把用户从团队中移除
        /// </summary>
        /// <param name="service"></param>
        /// <param name="teamid"> </param>
        /// <param name="userids"></param>
        public static void RemoveMembersTeam(this IOrganizationService service, Guid teamid, Guid[] userids)
        {
            var request = new RemoveMembersTeamRequest
            {
                TeamId = teamid,
                MemberIds = userids
            };
            service.Execute(request);
        }

        /// <summary>
        /// 单行数据查询
        /// </summary>
        /// <param name="service"> </param>
        /// <param name="fetchXml"></param>
        /// <returns></returns>
        public static Entity Retrieve(this IOrganizationService service, string fetchXml)
        {
            var doc = XDocument.Parse(fetchXml);
            doc.Root.SetAttributeValue("top", "1");
            doc.Root.SetAttributeValue("no-lock", "true");
            fetchXml = doc.ToString();

            var query = new FetchExpression(fetchXml);
            var entities = service.RetrieveMultiple(query);
            if (entities.Entities.Count > 0)
            {
                return entities.Entities.FirstOrDefault();
            }
            return null;
        }

        /// <summary>
        /// 获取所有记录
        /// </summary>
        /// <param name="service"> </param>
        /// <param name="fetchXml"></param>
        /// <returns></returns>
        public static List<Entity> RetrieveAll(this IOrganizationService service, string fetchXml)
        {
            int page = 1, count = 5000;
            var doc = XDocument.Parse(fetchXml);
            if (!fetchXml.Contains("no-lock"))
            {
                doc.Root.SetAttributeValue("no-lock", "true");
            }
            var list = new List<Entity>();
            while (true)
            {
                doc.Root.SetAttributeValue("page", page.ToString());
                doc.Root.SetAttributeValue("count", count.ToString());

                var query = new FetchExpression(doc.ToString());
                var entities = service.RetrieveMultiple(query);
                if (entities.Entities.Count > 0)
                {
                    list.AddRange(entities.Entities);
                }

                if (!entities.MoreRecords) break;

                page++;
            }
            return list;
        }

        /// <summary>
        /// 列表查询
        /// </summary>
        /// <param name="service"> </param>
        /// <param name="fetchXml"></param>
        /// <returns></returns>
        public static List<Entity> RetrieveMultiple(this IOrganizationService service, string fetchXml)
        {
            if (!fetchXml.Contains("no-lock"))
            {
                var doc = XDocument.Parse(fetchXml);
                doc.Root.SetAttributeValue("no-lock", "true");
                fetchXml = doc.ToString();
            }
            var query = new FetchExpression(fetchXml);
            var entities = service.RetrieveMultiple(query);
            if (entities.Entities.Count > 0)
            {
                return entities.Entities.ToList();
            }
            return new List<Entity>();
        }

        /// <summary>
        /// 列表查询
        /// </summary>
        /// <param name="service"> </param>
        /// <param name="fetchXml"></param>
        /// <param name="totals">  </param>
        /// <returns></returns>
        public static List<Entity> RetrieveMultiple(this IOrganizationService service, string fetchXml, out int totals)
        {
            var doc = XDocument.Parse(fetchXml);
            doc.Root.SetAttributeValue("returntotalrecordcount", "true");
            if (!fetchXml.Contains("no-lock"))
            {
                doc.Root.SetAttributeValue("no-lock", "true");
            }
            var query = new FetchExpression(doc.ToString());
            var entities = service.RetrieveMultiple(query);
            totals = entities.TotalRecordCount;
            if (entities.Entities.Count > 0)
            {
                return entities.Entities.ToList();
            }
            return new List<Entity>();
        }

        /// <summary>
        /// 取消共享
        /// </summary>
        /// <param name="service"></param>
        /// <param name="target"> 要取消共享的记录</param>
        /// <param name="revokee">要取消共享给用户或者团队</param>
        public static void Revoke(this IOrganizationService service, EntityReference target, EntityReference revokee)
        {
            var revokeUserAccessReq = new RevokeAccessRequest
            {
                Revokee = revokee,
                Target = target
            };
            service.Execute(revokeUserAccessReq);
        }

        /// <summary>
        /// 取消共享
        /// </summary>
        /// <param name="service"></param>
        /// <param name="target"> 要取消共享的记录</param>
        /// <param name="ownerid">是否排除当前记录ownerid</param>
        public static void RevokeAll(this IOrganizationService service, EntityReference target, EntityReference ownerid = null)
        {
            var accesses = service.RetrieveAccess(target);
            if (null == accesses) return;
            if (null != ownerid)
                accesses = accesses.Where(x => x.Principal.Id != ownerid.Id).ToList();
            foreach (var access in accesses)
            {
                service.Revoke(target, access.Principal);
            }
        }

        /// <summary>
        /// 批量更新
        /// </summary>
        /// <param name="service"> </param>
        /// <param name="entities"></param>
        /// <param name="istrans"> </param>
        /// <returns></returns>
        public static bool Update(this IOrganizationService service, IList<Entity> entities, bool istrans = true)
        {
            if (null == entities || !entities.Any()) return false;
            if (istrans)
            {
                var request = new ExecuteTransactionRequest()
                {
                    ReturnResponses = true,
                    Requests = new OrganizationRequestCollection()
                };
                foreach (var entity in entities)
                {
                    request.Requests.Add(new UpdateRequest { Target = entity });
                }
                service.Execute(request);
                return true;
            }
            foreach (var entity in entities)
            {
                service.Update(entity);
            }
            return true;
        }

        private static List<PrincipalAccess> RetrieveAccess(this IOrganizationService service, EntityReference target)
        {
            var accessRequest = new RetrieveSharedPrincipalsAndAccessRequest
            {
                Target = target
            };

            var accessResponse = (RetrieveSharedPrincipalsAndAccessResponse)service.Execute(accessRequest);
            if (null != accessResponse && accessResponse.PrincipalAccesses.Length > 0)
                return accessResponse.PrincipalAccesses.ToList();
            return null;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值