1. 程式人生 > >Dynamics 365使用Execute Multiple Request刪除系統作業實體記錄

Dynamics 365使用Execute Multiple Request刪除系統作業實體記錄

摘要: 本人微信公眾號:微軟動態CRM專家羅勇 ,回覆295或者20190112可方便獲取本文,同時可以在第一間得到我釋出的最新博文資訊,follow me!我的網站是 www.luoyong.me 。

系統作業實體(實體邏輯名稱為asyncoperation),若這個實體記錄數太多,會對系統造成較大壓力,可以利用系統標準的【批量刪除】功能(Bulk Delete)來定期刪除,批量刪除最頻繁的密度為每隔7天執行一次。有時候記錄數暴增,可能會用到程式來刪除,我這裡有個例子,上程式碼,這個是每次獲取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>