微软动态CRM专家罗勇 ,回复313或者20190311可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me 。
PrincipalObjectAccess表是用于共享记录,这个表如果记录太多,会导致系统整体性能下降,最好能控制下记录数量。
如果部署的是Dynamics 365 Customer Engagement On-Premise版本,直接上数据库查询可以,执行如下T-SQL即可。
select count(*) from PrincipalObjectAccess with (nolock)
如果估计返回记录不超过5000行记录,也可以直接用Web API来查看,带上$count=true即可,实例代码如下:
https://demo.luoyong.me/api/data/v9.0/principalobjectaccessset?$select=principalobjectaccessid&$count=true
https://demo.luoyong.me/api/data/v9.0/principalobjectaccessset?$select=principalobjectaccessid&$filter=objecttypecode eq 'template'&$count=true
执行效果类似如下,如果返回的 @odata.count 元素值等于5000,请不要相信这是准确数据,请用后面的方法查看。
如果前面返回的记录数为5000,但是估计在5万条下,也可以执行使用类似如下的URL进行查询,当然如果超过5万行会报错。
https://demo.luoyong.me/api/data/v8.2/principalobjectaccessset?fetchXml=%3Cfetch%20aggregate=%27true%27%20version=%271.0%27%20mapping=%27logical%27%20distinct=%27false%27%3E%0A%C2%A0%3Centity%20name=%27principalobjectaccess%27%3E%0A%C2%A0%C2%A0%3Cattribute%20aggregate=%27count%27%20alias=%27recordscount%27%20name=%27principalobjectaccessid%27%20/%3E%0A%C2%A0%3C/entity%3E%0A%3C/fetch%3E%0A%20%20%20%20
这其实是用Web API执行如下的聚合FetchXml:
<fetch aggregate='true' version='1.0' mapping='logical' distinct='false'>
<entity name='principalobjectaccess'>
<attribute aggregate='count' alias='recordscount' name='principalobjectaccessid' />
</entity>
</fetch>
如果超过5万行的话怎么计数?当然我不会阻拦你自己写程序来做,但是我已经有工具了,建议你不要重复造轮子。参考我的博文:无依赖简单易用的Dynamics 365实体记录数计数器并能计算出FetchXml返回的记录数 。直接执行如下的FetchXml即可:
<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'> <entity name='principalobjectaccess'> <attribute name='principalobjectaccessid' /> </entity> </fetch>
也可以对这个实体做筛选,类似如下的FetchXml:
<fetch version='1.0' mapping='logical' distinct='false'> <entity name='principalobjectaccess'> <attribute name='principalobjectaccessid' /> <filter type='and'> <condition attribute='objecttypecode' operator='eq' value='2010' /> </filter> </entity> </fetch>
执行效果如下图所示:
你可能会有疑问,怎么看一个实体的ObjectTypeCode的值,根据实体的逻辑名称来查看实体的ObjectTypeCode用类似如下的URL:
https://demo.luoyong.me/api/data/v9.0/EntityDefinitions(LogicalName='account')?$select=EntitySetName,ObjectTypeCode
你可能还会问,如果根据ObjectTypeCode来看是哪个实体,用如下的URL即可:
https://demo.luoyong.me/api/data/v9.0/EntityDefinitions?$select=EntitySetName,LogicalName&$filter=ObjectTypeCode eq 2010
当然也可以将记录导出来分析,下面是一个示例,将数据生成CSV文件,然后用Excel或者导入到数据库中分析,数据分页查询请参考:Page large result sets with QueryExpression 。
private static void GetAllPOA(OrganizationServiceProxy orgSvc) { string outPutFileName = ConfigurationManager.AppSettings["outPutFileName"]; if (!File.Exists(outPutFileName)) { string clientHeader = $"principalobjectaccessid,principalid,objecttypecode,objectid,principaltypecode,inheritedaccessrightsmask,changeon,accessrightsmask{Environment.NewLine}"; File.WriteAllText(outPutFileName, clientHeader); } int queryCount = 5000; int pageNumber = 1; QueryExpression pagequery = new QueryExpression("principalobjectaccess"); pagequery.NoLock = true; pagequery.AddOrder("changedon", OrderType.Descending); pagequery.ColumnSet = new ColumnSet("principalobjectaccessid", "principalid", "objecttypecode", "objectid", "principaltypecode", "inheritedaccessrightsmask", "accessrightsmask", "changedon"); pagequery.PageInfo = new PagingInfo(); pagequery.PageInfo.Count = queryCount; pagequery.PageInfo.PageNumber = pageNumber; pagequery.PageInfo.PagingCookie = null; while (true) { EntityCollection results = orgSvc.RetrieveMultiple(pagequery); if (results.Entities != null) { foreach (var entity in results.Entities) { File.AppendAllText(outPutFileName, $"{entity.GetAttributeValue<Guid>("principalobjectaccessid")}," + $"{entity.GetAttributeValue<Guid>("principalid")}," + $"{entity.GetAttributeValue<string>("objecttypecode")}," + $"{entity.GetAttributeValue<Guid>("objectid")}," + $"{entity.GetAttributeValue<string>("principaltypecode")}," + $"{entity.GetAttributeValue<int>("inheritedaccessrightsmask")}," + $"{entity.GetAttributeValue<DateTime>("changedon").ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss.fff")}," + $"{entity.GetAttributeValue<int>("accessrightsmask")}{Environment.NewLine}"); } } if (results.MoreRecords) { pagequery.PageInfo.PageNumber++; pagequery.PageInfo.PagingCookie = results.PagingCookie; } else { break; } } }