Dynamics 365使用Execute Multiple Request删除系统作业实体记录
2021-03-20 22:07
1226 查看
摘要: 本人微信公众号:微软动态CRM专家罗勇 ,回复295或者20190112可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me 。
系统作业实体(实体逻辑名称为asyncoperation),若这个实体记录数太多,会对系统造成较大压力,可以利用系统标准的【批量删除】功能(Bulk Delete)来定期删除,批量删除最频繁的密度为每天运行一次。有时候记录数暴增,可能会用到程序来删除,我这里有个例子,上代码,这个是每次获取5000条记录,每次提交500条记录进行删除。
using Microsoft.Crm.Sdk.Messages;using Microsoft.Xrm.Sdk;using Microsoft.Xrm.Sdk.Client;using Microsoft.Xrm.Sdk.Messages;using Microsoft.Xrm.Sdk.Query;using System;using System.Collections.Generic;using System.Configuration;using System.IO;using System.Linq;using System.Net;using System.ServiceModel.Description;using System.Text;using System.Threading;using System.Threading.Tasks;using System.Xml;namespace DeleteAsyncOperations { class Program { static void Main(string[] args) { try { string inputKey; ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; IServiceManagement<IOrganizationService> orgServiceMgr = ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri(ConfigurationManager.AppSettings["orgUrl"])); AuthenticationCredentials orgAuCredentials = new AuthenticationCredentials(); orgAuCredentials.ClientCredentials.UserName.UserName = ConfigurationManager.AppSettings["userName"]; orgAuCredentials.ClientCredentials.UserName.Password = ConfigurationManager.AppSettings["passWord"]; string needConfirm = ConfigurationManager.AppSettings["needConfirm"]; using (var orgSvc = GetProxy<IOrganizationService, OrganizationServiceProxy>(orgServiceMgr, orgAuCredentials)) { orgSvc.Timeout = new TimeSpan(8, 0, 0); WhoAmIRequest whoReq = new WhoAmIRequest(); var whoRsp = orgSvc.Execute(whoReq) as WhoAmIResponse; var userEntity = orgSvc.Retrieve("systemuser", whoRsp.UserId, new Microsoft.Xrm.Sdk.Query.ColumnSet("fullname")); Console.WriteLine(string.Format("欢迎【{0}】登陆到【{1}】", userEntity.GetAttributeValue<string>("fullname"), ConfigurationManager.AppSettings["orgUrl"])); Console.WriteLine("本程序用于删除已经成功或者取消的系统作业!"); if (needConfirm == "Y") { Console.WriteLine("当前处于需要确认才会继续的模式,若要继续请输入Y然后回车确认!"); inputKey = Console.ReadLine(); if (inputKey.ToUpper() == "Y") { UpdateContactCountryCode(orgSvc); } else { Console.WriteLine("你选择了取消运行!"); } } else { UpdateContactCountryCode(orgSvc); } } Console.Write("程序运行完成,按任意键退出." + DateTime.Now.ToString()); Console.ReadLine(); } catch (Exception ex) { Console.WriteLine("程序运行出错:" + ex.Message + ex.StackTrace); Console.ReadLine(); } } private static void UpdateContactCountryCode(OrganizationServiceProxy orgSvc) { const string functionName = "删除已经成功或者取消的系统作业"; Console.WriteLine(string.Format("开始 {0} - {1}", functionName, DateTime.Now.ToString())); try { var updateContactCountryCodeResult = ConfigurationManager.AppSettings["updateContactCountryCodeResult"]; string line = string.Empty; var contactFetchxml = @"<fetch version='1.0' mapping='logical' distinct='false' no-lock='true'> <entity name='asyncoperation'> <attribute name='asyncoperationid' /> <filter type='and'> <condition attribute='statuscode' operator='in'> <value>32</value> <value>30</value> </condition> </filter> </entity> </fetch>"; int pageNumber = 1; int fetchCount = 5000; string pagingCookie = null; orgSvc.Timeout = new TimeSpan(10, 0, 0); int j = 1; int i = 1; ExecuteMultipleRequest multiReqs = new ExecuteMultipleRequest() { Settings = new ExecuteMultipleSettings() { ContinueOnError = true, ReturnResponses = false }, Requests = new OrganizationRequestCollection() }; while (true) { string xml = CreateXml(contactFetchxml, pagingCookie, pageNumber, fetchCount); RetrieveMultipleRequest pageRequest1 = new RetrieveMultipleRequest { Query = new FetchExpression(xml) }; EntityCollection returnCollection = ((RetrieveMultipleResponse)orgSvc.Execute(pageRequest1)).EntityCollection; int count = returnCollection.Entities.Count(); foreach (var item in returnCollection.Entities) { DeleteRequest req = new DeleteRequest(); req.Target = new EntityReference("asyncoperation", item.Id); if (j <= 500) { multiReqs.Requests.Add(req); } else { multiReqs.Requests = new OrganizationRequestCollection(); multiReqs.Requests.Add(req); j = 1; } if (j == 500 || i == count) { orgSvc.Execute(multiReqs); } j++; i++; } Console.WriteLine(string.Format("共{0}条处理完毕!{1}", pageNumber * fetchCount, DateTime.Now.ToString())); if (returnCollection.MoreRecords) { pageNumber++; pagingCookie = returnCollection.PagingCookie; } else { Console.WriteLine("最后一页"); break; } } Console.WriteLine(string.Format("程序处理完成 {0}", updateContactCountryCodeResult)); } catch (Exception ex) { Console.WriteLine(string.Format("运行 {0} 出现异常:{1}", functionName, ex.Message + ex.StackTrace)); } Console.WriteLine(string.Format("结束 {0} - {1}", functionName, DateTime.Now.ToString())); Console.WriteLine("================================================"); } private static TProxy GetProxy<TService, TProxy>( IServiceManagement<TService> serviceManagement, AuthenticationCredentials authCredentials) where TService : class where TProxy : ServiceProxy<TService> { Type classType = typeof(TProxy); if (serviceManagement.AuthenticationType != AuthenticationProviderType.ActiveDirectory) { AuthenticationCredentials tokenCredentials = serviceManagement.Authenticate(authCredentials); return (TProxy)classType .GetConstructor(new Type[] { typeof(IServiceManagement<TService>), typeof(SecurityTokenResponse) }) .Invoke(new object[] { serviceManagement, tokenCredentials.SecurityTokenResponse }); } return (TProxy)classType .GetConstructor(new Type[] { typeof(IServiceManagement<TService>), typeof(ClientCredentials) }) .Invoke(new object[] { serviceManagement, authCredentials.ClientCredentials }); } public static string CreateXml(string xml, string cookie, int page, int count) { StringReader stringReader = new StringReader(xml); XmlTextReader reader = new XmlTextReader(stringReader); XmlDocument doc = new XmlDocument(); doc.Load(reader); return CreateXml(doc, cookie, page, count); } public static string CreateXml(XmlDocument doc, string cookie, int page, int count) { XmlAttributeCollection attrs = doc.DocumentElement.Attributes; if (cookie != null) { XmlAttribute pagingAttr = doc.CreateAttribute("paging-cookie"); pagingAttr.Value = cookie; attrs.Append(pagingAttr); } XmlAttribute pageAttr = doc.CreateAttribute("page"); pageAttr.Value = System.Convert.ToString(page); attrs.Append(pageAttr); XmlAttribute countAttr = doc.CreateAttribute("count"); countAttr.Value = System.Convert.ToString(count); attrs.Append(countAttr); StringBuilder sb = new StringBuilder(1024); StringWriter stringWriter = new StringWriter(sb); XmlTextWriter writer = new XmlTextWriter(stringWriter); doc.WriteTo(writer); writer.Close(); return sb.ToString(); } } }
使用的配置文件示例:
<?xml version="1.0" encoding="utf-8" ?><configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> </startup> <appSettings> <add key="userName" value="luoyong\crmadmin" /> <add key="passWord" value="*******" /> <add key="orgUrl" value="https://demo.luoyong.me/XRMServices/2011/Organization.svc" /> </appSettings></configuration>
相关文章推荐
- Dynamics 365检查工作流、SDK插件步骤是否选中运行成功后自动删除系统作业记录
- 教你删除系统中U盘的使用记录
- 无法删除尚未附加的实体-linq中使用反射删除记录出现错误,应该如何解决。
- 使用一条SQL语句删除表中重复记录
- 远程桌面使用痕迹清理【完全删除远程桌面连接历史记录】
- 在XP操作系统中使用批处理命令删除系统垃圾文件
- 使用Log4j记录系统日志(转载)
- adb shell下使用命令行删除android系统中指定文件和文件夹
- 在win7环境下删除USB使用记录
- 使用一条SQL语句删除表中重复记录
- [记录]由Virtools的设计想到的实体系统...
- instsrv.exe下载和使用方法(添加删除系统服务工具)
- 【数据安全案例】交警计算机系统再遭***,交通违法记录随意删除
- 使用SQL语句对重复记录查询、统计重复次数、删除重复数据
- 使用中发现用Request.Cookies.Remove()无法删除cookie
- (最新修正版2011-7-25)使用MongoDB替换Log4net记录系统异常日志(转)
- 使用MERGE INTO删除记录
- 使用一条语句删除重复记录
- iCallBR通话记录删除工具使用教程
- 使用ext3grep恢复ext3文件系统中误删除文件