Produced by Araxis Merge on 2/1/2017 2:56:31 PM Eastern Standard Time. See www.araxis.com for information about Merge. This report uses XHTML and CSS2, and is best viewed with a modern standards-compliant browser. For optimum results when printing this report, use landscape orientation and enable printing of background images and colours in your browser.
| # | Location | File | Last Modified |
|---|---|---|---|
| 1 | C:\Araxis_Merge_Comprasion\Pub_un\BTSSS_CIF_122016.zip\BTSSS_CIF_12_20_16\clean\CRM\trunk\SDK\SampleCode\CS\DataManagement\DuplicateDetection | BulkDetectDuplicates.cs | Tue Dec 20 19:51:43 2016 UTC |
| 2 | Wed Feb 1 19:56:31 2017 UTC |
| Description | Between Files 1 and 2 |
|
|---|---|---|
| Text Blocks | Lines | |
| Unchanged | 0 | 0 |
| Changed | 0 | 0 |
| Inserted | 0 | 0 |
| Removed | 1 | 372 |
| Whitespace | |
|---|---|
| Character case | Differences in character case are significant |
| Line endings | Differences in line endings (CR and LF characters) are ignored |
| CR/LF characters | Not shown in the comparison detail |
No regular expressions were active.
| 1 | // ======= ========== ========== ========== ========== ========== ========== == | |||||
| 2 | // This f ile is par t of the M icrosoft D ynamics CR M SDK code samples. | |||||
| 3 | // | |||||
| 4 | // Copyri ght (C) Mi crosoft Co rporation. All righ ts reserve d. | |||||
| 5 | // | |||||
| 6 | // This s ource code is intend ed only as a supplem ent to Mic rosoft | |||||
| 7 | // Develo pment Tool s and/or o n-line doc umentation . See the se other | |||||
| 8 | // materi als for de tailed inf ormation r egarding M icrosoft c ode sample s. | |||||
| 9 | // | |||||
| 10 | // THIS C ODE AND IN FORMATION ARE PROVID ED "AS IS" WITHOUT W ARRANTY OF ANY | |||||
| 11 | // KIND, EITHER EXP RESSED OR IMPLIED, I NCLUDING B UT NOT LIM ITED TO TH E | |||||
| 12 | // IMPLIE D WARRANTI ES OF MERC HANTABILIT Y AND/OR F ITNESS FOR A | |||||
| 13 | // PARTIC ULAR PURPO SE. | |||||
| 14 | // ======= ========== ========== ========== ========== ========== ========== == | |||||
| 15 | ||||||
| 16 | //<snippet BulkDetect Duplicates > | |||||
| 17 | using Syst em; | |||||
| 18 | using Syst em.Linq; | |||||
| 19 | using Syst em.Service Model; | |||||
| 20 | ||||||
| 21 | // These n amespaces are found in the Mic rosoft.Xrm .Sdk.dll a ssembly | |||||
| 22 | // located in the SD K\bin fold er of the SDK downlo ad. | |||||
| 23 | using Micr osoft.Xrm. Sdk; | |||||
| 24 | using Micr osoft.Xrm. Sdk.Query; | |||||
| 25 | using Micr osoft.Xrm. Sdk.Client ; | |||||
| 26 | ||||||
| 27 | // This na mespace is found in Microsoft. Crm.Sdk.Pr oxy.dll as sembly | |||||
| 28 | // found i n the SDK\ bin folder . | |||||
| 29 | using Micr osoft.Crm. Sdk.Messag es; | |||||
| 30 | ||||||
| 31 | namespace Microsoft. Crm.Sdk.Sa mples | |||||
| 32 | { | |||||
| 33 | /// <s ummary> | |||||
| 34 | /// Th is samples shows how to issue a BulkDete ctDuplicat es request . | |||||
| 35 | /// </ summary> | |||||
| 36 | /// <p aram name= "serverCon fig">Conta ins server connectio n informat ion.</para m> | |||||
| 37 | /// <p aram name= "promptfor Delete">Wh en True, t he user wi ll be prom pted to de lete all | |||||
| 38 | /// cr eated enti ties.</par am> | |||||
| 39 | public class Bul kDetectDup licates | |||||
| 40 | { | |||||
| 41 | #r egion Clas s Level Me mbers | |||||
| 42 | ||||||
| 43 | pr ivate Orga nizationSe rviceProxy _serviceP roxy; | |||||
| 44 | ||||||
| 45 | pr ivate Acco unt[] _dup licateAcco unts = new Account[2 ]; | |||||
| 46 | pr ivate Acco unt _accou nt; | |||||
| 47 | pr ivate Dupl icateRule _rule; | |||||
| 48 | pr ivate Bulk DetectDupl icatesResp onse _resp onse; | |||||
| 49 | ||||||
| 50 | #e ndregion C lass Level Members | |||||
| 51 | ||||||
| 52 | #r egion How To Sample Code | |||||
| 53 | // / <summary > | |||||
| 54 | // / This met hod first creates 3 accounts w ith the sa me name, t hen issues a BulkDet ectDuplica tes | |||||
| 55 | // / request to show th e duplicat e detectio n. | |||||
| 56 | // / </summar y> | |||||
| 57 | // / <param n ame="serve rConfig">C ontains se rver conne ction info rmation.</ param> | |||||
| 58 | // / <param n ame="promp tforDelete ">When Tru e, the use r will be prompted t o delete a ll | |||||
| 59 | // / created entities.< /param> | |||||
| 60 | ||||||
| 61 | pu blic void Run(Server Connection .Configura tion serve rConfig, b ool prompt forDelete) | |||||
| 62 | { | |||||
| 63 | try | |||||
| 64 | { | |||||
| 65 | //<s nippetBulk DetectDupl icates1> | |||||
| 66 | // C onnect to the Organi zation ser vice. | |||||
| 67 | // T he using s tatement a ssures tha t the serv ice proxy will be pr operly dis posed. | |||||
| 68 | usin g (_servic eProxy = n ew Organiz ationServi ceProxy(se rverConfig .Organizat ionUri, se rverConfig .HomeRealm Uri, | |||||
| 69 | s erverConfi g.Credenti als, serve rConfig.De viceCreden tials)) | |||||
| 70 | { | |||||
| 71 | // This st atement is required to enable early-boun d type sup port. | |||||
| 72 | _servicePr oxy.Enable ProxyTypes (); | |||||
| 73 | ||||||
| 74 | CreateRequ iredRecord s(); | |||||
| 75 | ||||||
| 76 | // Create the BulkDe tectDuplic atesReques t object | |||||
| 77 | Console.Wr iteLine(" Creating the BulkDe tectDuplic atesReques t object") ; | |||||
| 78 | BulkDetect Duplicates Request re quest = ne w BulkDete ctDuplicat esRequest( ) | |||||
| 79 | { | |||||
| 80 | JobNam e = "Detec t Duplicat e Accounts ", | |||||
| 81 | Query = new Quer yExpressio n() | |||||
| 82 | { | |||||
| 83 | En tityName = Account.E ntityLogic alName, | |||||
| 84 | Co lumnSet = new Column Set(true) | |||||
| 85 | }, | |||||
| 86 | Recurr encePatter n = String .Empty, | |||||
| 87 | Recurr enceStartT ime = Date Time.Now, | |||||
| 88 | ToReci pients = n ew Guid[0] , | |||||
| 89 | CCReci pients = n ew Guid[0] | |||||
| 90 | }; | |||||
| 91 | ||||||
| 92 | // Execute the reque st | |||||
| 93 | Console.Wr iteLine(" Executing BulkDetec tDuplicate sRequest") ; | |||||
| 94 | _response = (BulkDet ectDuplica tesRespons e)_service Proxy | |||||
| 95 | .Execu te(request ); | |||||
| 96 | ||||||
| 97 | #region ch eck succes s | |||||
| 98 | ||||||
| 99 | Console.Wr iteLine(" Waiting f or job to complete.. ."); | |||||
| 100 | WaitForAsy ncJobToFin ish(_respo nse.JobId, 120); | |||||
| 101 | ||||||
| 102 | QueryByAtt ribute que ry = new Q ueryByAttr ibute() | |||||
| 103 | { | |||||
| 104 | Column Set = new ColumnSet( true), | |||||
| 105 | Entity Name = "du plicaterec ord" | |||||
| 106 | }; | |||||
| 107 | query.Attr ibutes.Add ("asyncope rationid") ; | |||||
| 108 | query.Valu es.Add(_re sponse.Job Id); | |||||
| 109 | EntityColl ection res ults = _se rviceProxy .RetrieveM ultiple(qu ery); | |||||
| 110 | ||||||
| 111 | // check t o make sur e each id is found i n the coll ection | |||||
| 112 | var duplic ateIds = r esults.Ent ities.Sele ct((entity ) => | |||||
| 113 | ((Dupl icateRecor d)entity). BaseRecord Id.Id); | |||||
| 114 | foreach (v ar id in _ duplicateA ccounts.Se lect((acco unt) => ac count.Id)) | |||||
| 115 | { | |||||
| 116 | if (!d uplicateId s.Contains (id)) | |||||
| 117 | { | |||||
| 118 | th row new Ex ception(St ring.Forma t( | |||||
| 119 | "Account with ID { 0} was not detected as a dupli cate", | |||||
| 120 | id)); | |||||
| 121 | } | |||||
| 122 | } | |||||
| 123 | Console.Wr iteLine(" All accou nts detect ed as dupl icates suc cessfully" ); | |||||
| 124 | ||||||
| 125 | #endregion | |||||
| 126 | ||||||
| 127 | Console.Wr iteLine(); | |||||
| 128 | ||||||
| 129 | DeleteRequ iredRecord s(promptfo rDelete); | |||||
| 130 | } | |||||
| 131 | //</ snippetBul kDetectDup licates1> | |||||
| 132 | } | |||||
| 133 | ||||||
| 134 | // Catch any servi ce fault e xceptions that Micro soft Dynam ics CRM th rows. | |||||
| 135 | catch (F aultExcept ion<Micros oft.Xrm.Sd k.Organiza tionServic eFault>) | |||||
| 136 | { | |||||
| 137 | // Y ou can han dle an exc eption her e or pass it back to the calli ng method. | |||||
| 138 | thro w; | |||||
| 139 | } | |||||
| 140 | } | |||||
| 141 | ||||||
| 142 | #r egion Help er methods | |||||
| 143 | ||||||
| 144 | pr ivate void WaitForAs yncJobToFi nish(Guid jobId, int maxTimeSe conds) | |||||
| 145 | { | |||||
| 146 | for (int i = 0; i < maxTimeS econds; i+ +) | |||||
| 147 | { | |||||
| 148 | var asyncJob = _serviceP roxy.Retri eve(AsyncO peration.E ntityLogic alName, | |||||
| 149 | jobId, new ColumnSet ("statecod e")).ToEnt ity<AsyncO peration>( ); | |||||
| 150 | if ( asyncJob.S tateCode.H asValue && asyncJob. StateCode. Value == A syncOperat ionState.C ompleted) | |||||
| 151 | return; | |||||
| 152 | Syst em.Threadi ng.Thread. Sleep(1000 ); | |||||
| 153 | } | |||||
| 154 | throw ne w Exceptio n(String.F ormat( | |||||
| 155 | " E xceeded ma ximum time of {0} se conds wait ing for as ynchronous job to co mplete", | |||||
| 156 | maxT imeSeconds | |||||
| 157 | )); | |||||
| 158 | } | |||||
| 159 | ||||||
| 160 | #e ndregion | |||||
| 161 | ||||||
| 162 | #r egion Publ ic methods | |||||
| 163 | ||||||
| 164 | // / <summary > | |||||
| 165 | // / Creates any entity records t hat this s ample requ ires. | |||||
| 166 | // / </summar y> | |||||
| 167 | pu blic void CreateRequ iredRecord s() | |||||
| 168 | { | |||||
| 169 | #region Creating A ccounts | |||||
| 170 | ||||||
| 171 | String a ccountName = "Contos o, Ltd"; | |||||
| 172 | String w ebsiteUrl = "http:// www.contos o.com/"; | |||||
| 173 | ||||||
| 174 | Console. WriteLine( " Creatin g duplicat e records (Account n ame={0}, W ebsite URL ={1})", ac countName, | |||||
| 175 | webs iteUrl); | |||||
| 176 | // Creat e some dup licate rec ords | |||||
| 177 | for (int i = 0; i < 2; i++) | |||||
| 178 | { | |||||
| 179 | Acco unt accoun t = new Ac count() | |||||
| 180 | { | |||||
| 181 | Name = acc ountName, | |||||
| 182 | WebSiteURL = website Url | |||||
| 183 | }; | |||||
| 184 | acco unt.Id = _ servicePro xy.Create( account); | |||||
| 185 | _dup licateAcco unts[i] = account; | |||||
| 186 | } | |||||
| 187 | ||||||
| 188 | accountN ame = "Con toso Pharm aceuticals "; | |||||
| 189 | Console. WriteLine( " Creatin g a non-du plicate re cord (Acco unt name={ 0}, Websit e URL={1}) ", | |||||
| 190 | acco untName, w ebsiteUrl) ; | |||||
| 191 | // Creat e a record that is N OT a dupli cate | |||||
| 192 | Account distinctAc count = ne w Account( ) | |||||
| 193 | { | |||||
| 194 | Name = account Name, | |||||
| 195 | WebS iteURL = w ebsiteUrl | |||||
| 196 | }; | |||||
| 197 | distinct Account.Id = _servic eProxy.Cre ate(distin ctAccount) ; | |||||
| 198 | _account = distinc tAccount; | |||||
| 199 | ||||||
| 200 | #endregi on | |||||
| 201 | ||||||
| 202 | #region Create and Publish d uplicate d etection r ule | |||||
| 203 | ||||||
| 204 | Console. WriteLine( " Creatin g a duplic ate detect ion rule") ; | |||||
| 205 | // Creat e a duplic ate detect ion rule | |||||
| 206 | _rule = new Duplic ateRule() | |||||
| 207 | { | |||||
| 208 | Name = "Accoun ts with th e same Acc ount name and websit e url", | |||||
| 209 | Base EntityName = Account .EntityLog icalName, | |||||
| 210 | Matc hingEntity Name = Acc ount.Entit yLogicalNa me | |||||
| 211 | }; | |||||
| 212 | _rule.Id = _servic eProxy.Cre ate(_rule) ; | |||||
| 213 | ||||||
| 214 | // Creat e a duplic ate detect ion rule c ondition | |||||
| 215 | Duplicat eRuleCondi tion nameC ondition = new Dupli cateRuleCo ndition() | |||||
| 216 | { | |||||
| 217 | Base AttributeN ame = "nam e", | |||||
| 218 | Matc hingAttrib uteName = "name", | |||||
| 219 | Oper atorCode = new Optio nSetValue( 0), // val ue 0 = 'ex act match' | |||||
| 220 | // s et the reg arding id to point t o the rule created e arlier, | |||||
| 221 | // a ssociating this cond ition with that rule | |||||
| 222 | Rega rdingObjec tId = _rul e.ToEntity Reference( ) | |||||
| 223 | }; | |||||
| 224 | _service Proxy.Crea te(nameCon dition); | |||||
| 225 | ||||||
| 226 | Duplicat eRuleCondi tion websi teConditio n = new Du plicateRul eCondition () | |||||
| 227 | { | |||||
| 228 | Base AttributeN ame = "web siteurl", | |||||
| 229 | Matc hingAttrib uteName = "websiteur l", | |||||
| 230 | Oper atorCode = new Optio nSetValue( 0), | |||||
| 231 | Rega rdingObjec tId = _rul e.ToEntity Reference( ) | |||||
| 232 | }; | |||||
| 233 | _service Proxy.Crea te(website Condition) ; | |||||
| 234 | ||||||
| 235 | Console. WriteLine( " Publish ing duplic ate detect ion rule") ; | |||||
| 236 | // Publi sh the rul e | |||||
| 237 | PublishD uplicateRu leRequest publishReq uest = new PublishDu plicateRul eRequest() | |||||
| 238 | { | |||||
| 239 | Dupl icateRuleI d = _rule. Id | |||||
| 240 | }; | |||||
| 241 | PublishD uplicateRu leResponse publishRe sponse = ( PublishDup licateRule Response)_ servicePro xy.Execute (publishRe quest); | |||||
| 242 | ||||||
| 243 | // The P ublishDupl icateRule request re turns befo re the pub lish is co mpleted, | |||||
| 244 | // so we keep retr ieving the async job state unt il it is " Completed" | |||||
| 245 | Console. WriteLine( " Checkin g to see i f duplicat e detectio n rule has finished publishing "); | |||||
| 246 | WaitForA syncJobToF inish(publ ishRespons e.JobId, 6 0); | |||||
| 247 | ||||||
| 248 | #endregi on | |||||
| 249 | } | |||||
| 250 | ||||||
| 251 | // / <summary > | |||||
| 252 | // / Deletes any entity records t hat were c reated for this samp le. | |||||
| 253 | // / <param n ame="promp t">Indicat es whether to prompt the user | |||||
| 254 | // / to delet e the reco rds create d in this sample.</p aram> | |||||
| 255 | // / </summar y> | |||||
| 256 | pu blic void DeleteRequ iredRecord s(bool pro mpt) | |||||
| 257 | { | |||||
| 258 | bool toB eDeleted = true; | |||||
| 259 | ||||||
| 260 | if (prom pt) | |||||
| 261 | { | |||||
| 262 | // A sk the use r if the c reated ent ities shou ld be dele ted. | |||||
| 263 | Cons ole.Write( "\nDo you want these entity re cords dele ted? (y/n) [y]: "); | |||||
| 264 | Stri ng answer = Console. ReadLine() ; | |||||
| 265 | if ( answer.Sta rtsWith("y ") || | |||||
| 266 | answer.Sta rtsWith("Y ") || | |||||
| 267 | answer == String.Emp ty) | |||||
| 268 | { | |||||
| 269 | toBeDelete d = true; | |||||
| 270 | } | |||||
| 271 | else | |||||
| 272 | { | |||||
| 273 | toBeDelete d = false; | |||||
| 274 | } | |||||
| 275 | } | |||||
| 276 | ||||||
| 277 | if (toBe Deleted) | |||||
| 278 | { | |||||
| 279 | // D elete the job | |||||
| 280 | Cons ole.WriteL ine(" Del eting the job"); | |||||
| 281 | _ser viceProxy. Delete(Asy ncOperatio n.EntityLo gicalName, _response .JobId); | |||||
| 282 | ||||||
| 283 | // D elete the duplicate detection rule | |||||
| 284 | Cons ole.WriteL ine(" Del eting the duplicate detection rule"); | |||||
| 285 | _ser viceProxy. Delete(Dup licateRule .EntityLog icalName, _rule.Id); | |||||
| 286 | ||||||
| 287 | // D elete the accounts | |||||
| 288 | Cons ole.WriteL ine(" Del eting the accounts") ; | |||||
| 289 | fore ach (Accou nt account in _dupli cateAccoun ts) | |||||
| 290 | _servicePr oxy.Delete (Account.E ntityLogic alName, ac count.Id); | |||||
| 291 | _ser viceProxy. Delete(Acc ount.Entit yLogicalNa me, _accou nt.Id); | |||||
| 292 | Cons ole.WriteL ine("Entit y records have been deleted.") ; | |||||
| 293 | } | |||||
| 294 | } | |||||
| 295 | #e ndregion P ublic Meth ods | |||||
| 296 | ||||||
| 297 | #e ndregion H ow To Samp le Code | |||||
| 298 | ||||||
| 299 | #r egion Main method | |||||
| 300 | ||||||
| 301 | // / <summary > | |||||
| 302 | // / Standard Main() me thod used by most SD K samples. | |||||
| 303 | // / </summar y> | |||||
| 304 | // / <param n ame="args" ></param> | |||||
| 305 | st atic publi c void Mai n(string[] args) | |||||
| 306 | { | |||||
| 307 | try | |||||
| 308 | { | |||||
| 309 | // O btain the target org anization' s Web addr ess and cl ient logon | |||||
| 310 | // c redentials from the user. | |||||
| 311 | Serv erConnecti on serverC onnect = n ew ServerC onnection( ); | |||||
| 312 | Serv erConnecti on.Configu ration con fig = serv erConnect. GetServerC onfigurati on(); | |||||
| 313 | ||||||
| 314 | Bulk DetectDupl icates app = new Bul kDetectDup licates(); | |||||
| 315 | app. Run(config , true); | |||||
| 316 | } | |||||
| 317 | catch (F aultExcept ion<Micros oft.Xrm.Sd k.Organiza tionServic eFault> ex ) | |||||
| 318 | { | |||||
| 319 | Cons ole.WriteL ine("The a pplication terminate d with an error."); | |||||
| 320 | Cons ole.WriteL ine("Times tamp: {0}" , ex.Detai l.Timestam p); | |||||
| 321 | Cons ole.WriteL ine("Code: {0}", ex. Detail.Err orCode); | |||||
| 322 | Cons ole.WriteL ine("Messa ge: {0}", ex.Detail. Message); | |||||
| 323 | Cons ole.WriteL ine("Plugi n Trace: { 0}", ex.De tail.Trace Text); | |||||
| 324 | Cons ole.WriteL ine("Inner Fault: {0 }", | |||||
| 325 | null == ex .Detail.In nerFault ? "No Inner Fault" : "Has Inner Fault"); | |||||
| 326 | } | |||||
| 327 | catch (S ystem.Time outExcepti on ex) | |||||
| 328 | { | |||||
| 329 | Cons ole.WriteL ine("The a pplication terminate d with an error."); | |||||
| 330 | Cons ole.WriteL ine("Messa ge: {0}", ex.Message ); | |||||
| 331 | Cons ole.WriteL ine("Stack Trace: {0 }", ex.Sta ckTrace); | |||||
| 332 | Cons ole.WriteL ine("Inner Fault: {0 }", | |||||
| 333 | null == ex .InnerExce ption.Mess age ? "No Inner Faul t" : ex.In nerExcepti on.Message ); | |||||
| 334 | } | |||||
| 335 | catch (S ystem.Exce ption ex) | |||||
| 336 | { | |||||
| 337 | Cons ole.WriteL ine("The a pplication terminate d with an error."); | |||||
| 338 | Cons ole.WriteL ine(ex.Mes sage); | |||||
| 339 | ||||||
| 340 | // D isplay the details o f the inne r exceptio n. | |||||
| 341 | if ( ex.InnerEx ception != null) | |||||
| 342 | { | |||||
| 343 | Console.Wr iteLine(ex .InnerExce ption.Mess age); | |||||
| 344 | ||||||
| 345 | FaultExcep tion<Micro soft.Xrm.S dk.Organiz ationServi ceFault> f e = ex.Inn erExceptio n | |||||
| 346 | as Fau ltExceptio n<Microsof t.Xrm.Sdk. Organizati onServiceF ault>; | |||||
| 347 | if (fe != null) | |||||
| 348 | { | |||||
| 349 | Consol e.WriteLin e("Timesta mp: {0}", fe.Detail. Timestamp) ; | |||||
| 350 | Consol e.WriteLin e("Code: { 0}", fe.De tail.Error Code); | |||||
| 351 | Consol e.WriteLin e("Message : {0}", fe .Detail.Me ssage); | |||||
| 352 | Consol e.WriteLin e("Plugin Trace: {0} ", fe.Deta il.TraceTe xt); | |||||
| 353 | Consol e.WriteLin e("Inner F ault: {0}" , | |||||
| 354 | nu ll == fe.D etail.Inne rFault ? " No Inner F ault" : "H as Inner F ault"); | |||||
| 355 | } | |||||
| 356 | } | |||||
| 357 | } | |||||
| 358 | // Addit ional exce ptions to catch: Sec urityToken Validation Exception, ExpiredSe curityToke nException , | |||||
| 359 | // Secur ityAccessD eniedExcep tion, Mess ageSecurit yException , and Secu rityNegoti ationExcep tion. | |||||
| 360 | ||||||
| 361 | finally | |||||
| 362 | { | |||||
| 363 | Cons ole.WriteL ine("Press <Enter> t o exit."); | |||||
| 364 | Cons ole.ReadLi ne(); | |||||
| 365 | } | |||||
| 366 | } | |||||
| 367 | ||||||
| 368 | #e ndregion M ain method | |||||
| 369 | } | |||||
| 370 | } | |||||
| 371 | //</snippe tBulkDetec tDuplicate s> | |||||
| 372 |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.