我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面的微软最有价值专家(Microsoft MVP),欢迎关注我的微信公众号 MSFTDynamics365erLuoYong ,回复392或者20200205可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!
我们知道当前版本(我说的是当前,以后可能会更改)Dynamics 365的附件是存储在annotation实体的,是以Base64编码存储的,默认情况下单个附件最大是5兆,可以通过 Settings > Administrator > System Settings > Email查看到如下:
你可能会问最大能设置到多大?Dynamics 365 Customer Engagement V9.X版本是 128MB ,也就是 131072 KB ,之前的版本应该是32MB。
如果强行上传更大的文件会报错,报错如下:
当然也可以限制上传文件类型,比如我限制不能上传zip文件,导航到Settings > Administrator > System Settings > General 进行设置,就是添加 ;zip 。
如果强行上传zip文件的话会报错如下:
有的用户可能想限制某些实体下单条记录上传附件总的大小,比如我这里以限制系统标准的incident实体,每个incident记录关联的附件不能超过10MB。
我使用的代码如下:
using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; using System; namespace PluginDemo { public class PostAnnotationCreate : IPlugin { public void Execute(IServiceProvider serviceProvider) { // 获取日志服务 ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); //写一些日志,方便跟踪 tracingService.Trace($"Enter PostAnnotationCreate on {DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss:ms")}"); IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) { //插件针对的当前实体记录,对于Pre Create消息来讲,该对象包括了所有设置的字段值 Entity currentEntity = (Entity)context.InputParameters["Target"]; //获取系统管理员角色的特殊服务账号代表的组织服务,方便查询出所有数据 IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService orgAdminSvc = serviceFactory.CreateOrganizationService(null); //只有注释包括附件才做检查 if (currentEntity.Contains("isdocument") && currentEntity.GetAttributeValue<bool>("isdocument") && currentEntity.Contains("objectid")) { tracingService.Trace($"The notes has attachment {DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss:ms")}"); var regardingObj = currentEntity.GetAttributeValue<EntityReference>("objectid"); tracingService.Trace($"The notes regarding {regardingObj.LogicalName} with id {regardingObj.Id} {DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss:ms")}"); if (regardingObj.LogicalName == "incident") { tracingService.Trace($"The notes regarding incident {DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss:ms")}"); //通过聚合运算算出当前注释关联case的附件大小,这个计算会包括当前要上传的附件的大小 string caseAttachmentSizeSumQuery = string.Format(@"<fetch version='1.0' mapping='logical' no-lock='true' aggregate='true'> <entity name='annotation'> <attribute name='filesize' alias='filesize_sum' aggregate='sum' /> <filter type='and'> <condition attribute='isdocument' operator='eq' value='1' /> </filter> <link-entity name='incident' from='incidentid' to='objectid' link-type='inner' alias='ab'> <filter type='and'> <condition attribute='incidentid' operator='eq' value='{0}' /> </filter> </link-entity> </entity> </fetch>", regardingObj.Id); EntityCollection caseAttachmentSizeSum_result = orgAdminSvc.RetrieveMultiple(new FetchExpression(caseAttachmentSizeSumQuery)); if (caseAttachmentSizeSum_result.Entities.Count >= 1 && caseAttachmentSizeSum_result.Entities[0].Contains("filesize_sum") && caseAttachmentSizeSum_result.Entities[0]["filesize_sum"] != null && ((AliasedValue)caseAttachmentSizeSum_result.Entities[0]["filesize_sum"]).Value != null) { var caseAttachmentSizeSum = (Int32)((AliasedValue)caseAttachmentSizeSum_result.Entities[0]["filesize_sum"]).Value; tracingService.Trace($"The case has {caseAttachmentSizeSum} bytes of attachment including current attachement {DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss:ms")}"); //如果该case下面的附件大小超过了10兆,就不允许继续上传 if ((caseAttachmentSizeSum) >= 10 * 1024 * 1024) { throw new InvalidPluginExecutionException("一个Case下的所有附件大小之和不能超过10MB!若要继续上传请减少上传文件大小或者删除已经上传的附件!"); } } else { tracingService.Trace($"The notes regarding {regardingObj.LogicalName} with id {regardingObj.Id} does not have attachment {DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss:ms")}"); } } } } tracingService.Trace($"Leave PostAnnotationCreate on {DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss:ms")}"); } } }
注册插件如下:
可以看到效果如下: