Produced by Araxis Merge on 2/1/2017 2:56:35 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\GeneralProgramming\Queries | UseLinqQuery.cs | Tue Dec 20 19:51:46 2016 UTC |
| 2 | Wed Feb 1 19:56:35 2017 UTC |
| Description | Between Files 1 and 2 |
|
|---|---|---|
| Text Blocks | Lines | |
| Unchanged | 0 | 0 |
| Changed | 0 | 0 |
| Inserted | 0 | 0 |
| Removed | 1 | 559 |
| 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 UseLinqQue ry> | |||||
| 17 | using Syst em; | |||||
| 18 | using Syst em.Collect ions.Gener ic; | |||||
| 19 | using Syst em.Linq; | |||||
| 20 | using Syst em.Service Model; | |||||
| 21 | ||||||
| 22 | // These n amespaces are found in the Mic rosoft.Xrm .Sdk.dll a ssembly | |||||
| 23 | // located in the SD K\bin fold er of the SDK downlo ad. | |||||
| 24 | using Micr osoft.Xrm. Sdk; | |||||
| 25 | using Micr osoft.Xrm. Sdk.Client ; | |||||
| 26 | ||||||
| 27 | namespace Microsoft. Crm.Sdk.Sa mples | |||||
| 28 | { | |||||
| 29 | /// <summ ary> | |||||
| 30 | /// Demon strates ho w to make LINQ queri es with th e Organiza tionServic eContext | |||||
| 31 | /// objec t and stro ngly-typed entities. </summary> | |||||
| 32 | /// <rema rks> | |||||
| 33 | /// At ru n-time, yo u will be given the option to delete all the | |||||
| 34 | /// datab ase record s created by this pr ogram.</re marks> | |||||
| 35 | public cl ass UseLin qQuery | |||||
| 36 | { | |||||
| 37 | #region Class Leve l Members | |||||
| 38 | ||||||
| 39 | private List<Guid> _accountI ds = new L ist<Guid>( ); | |||||
| 40 | private List<Guid> _contactI ds = new L ist<Guid>( ); | |||||
| 41 | private List<Guid> _leadIds = new List <Guid>(); | |||||
| 42 | private List<Guid> _taskIds = new List <Guid>(); | |||||
| 43 | private Organizati onServiceP roxy _serv iceProxy; | |||||
| 44 | private IOrganizat ionService _service; | |||||
| 45 | ||||||
| 46 | #endregi on Class L evel Membe rs | |||||
| 47 | ||||||
| 48 | #region How To Sam ple Code | |||||
| 49 | /// <sum mary> | |||||
| 50 | /// This method fi rst connec ts to the Organizati on service and servi ce context . | |||||
| 51 | /// Afte rwards, se veral LINQ query tec hniques ar e demonstr ated. | |||||
| 52 | /// </su mmary> | |||||
| 53 | /// <par am name="s erverConfi g">Contain s server c onnection informatio n.</param> | |||||
| 54 | /// <par am name="p romptForDe lete">When True, the user will be prompt ed to dele te | |||||
| 55 | /// all created en tities.</p aram> | |||||
| 56 | public v oid Run(Se rverConnec tion.Confi guration s erverConfi g, bool pr omptForDel ete) | |||||
| 57 | { | |||||
| 58 | try | |||||
| 59 | { | |||||
| 60 | ||||||
| 61 | // Con nect to th e Organiza tion servi ce. | |||||
| 62 | // The using sta tement ass ures that the servic e proxy wi ll be prop erly dispo sed. | |||||
| 63 | using (_serviceP roxy = new Organizat ionService Proxy(serv erConfig.O rganizatio nUri, serv erConfig.H omeRealmUr i,serverCo nfig.Crede ntials, se rverConfig .DeviceCre dentials)) | |||||
| 64 | { | |||||
| 65 | // Th is stateme nt is requ ired to en able early -bound typ e support. | |||||
| 66 | _serv iceProxy.E nableProxy Types(); | |||||
| 67 | ||||||
| 68 | _serv ice = (IOr ganization Service)_s erviceProx y; | |||||
| 69 | ||||||
| 70 | Creat eRequiredR ecords(); | |||||
| 71 | ||||||
| 72 | Servi ceContext svcContext = | |||||
| 73 | n ew Service Context(_s ervice); | |||||
| 74 | ||||||
| 75 | ||||||
| 76 | // Re trieve rec ords with Skip/Take record pag ing. Setti ng a page size | |||||
| 77 | // ca n help you manage yo ur Skip an d Take cal ls, since Skip must be | |||||
| 78 | // pa ssed a mul tiple of T ake's para meter valu e. | |||||
| 79 | //<sn ippetUseLi nqQuery1> | |||||
| 80 | int p ageSize = 5; | |||||
| 81 | ||||||
| 82 | var a ccountsByP age = (fro m a in svc Context.Ac countSet | |||||
| 83 | sel ect new Ac count | |||||
| 84 | { | |||||
| 85 | Na me = a.Nam e, | |||||
| 86 | }); | |||||
| 87 | Syste m.Console. WriteLine( "Skip 10 a ccounts, t hen Take 5 accounts" ); | |||||
| 88 | Syste m.Console. WriteLine( "========= ========== ========== =========" ); | |||||
| 89 | forea ch (var a in account sByPage.Sk ip(2 * pag eSize).Tak e(pageSize )) | |||||
| 90 | { | |||||
| 91 | Syst em.Console .WriteLine (a.Name); | |||||
| 92 | } | |||||
| 93 | //</s nippetUseL inqQuery1> | |||||
| 94 | Syste m.Console. WriteLine( ); | |||||
| 95 | Syste m.Console. WriteLine( "<End of L isting>"); | |||||
| 96 | Syste m.Console. WriteLine( ); | |||||
| 97 | //OUT PUT: | |||||
| 98 | //Ski p 10 accou nts, then Take 5 acc ounts | |||||
| 99 | //=== ========== ========== ========== ===== | |||||
| 100 | //Fou rth Coffee 6 | |||||
| 101 | //Fou rth Coffee 7 | |||||
| 102 | //Fou rth Coffee 8 | |||||
| 103 | //Fou rth Coffee 9 | |||||
| 104 | //Fou rth Coffee 10 | |||||
| 105 | ||||||
| 106 | //<En d of Listi ng> | |||||
| 107 | ||||||
| 108 | ||||||
| 109 | ||||||
| 110 | // Us e orderBy to order i tems retri eved. | |||||
| 111 | //<sn ippetUseLi nqQuery2> | |||||
| 112 | var o rderedAcco unts = fro m a in svc Context.Ac countSet | |||||
| 113 | ord erby a.Nam e | |||||
| 114 | sel ect new Ac count | |||||
| 115 | { | |||||
| 116 | Na me = a.Nam e, | |||||
| 117 | }; | |||||
| 118 | Syste m.Console. WriteLine( "Display a ccounts or dered by n ame"); | |||||
| 119 | Syste m.Console. WriteLine( "========= ========== ========== ==="); | |||||
| 120 | forea ch (var a in ordered Accounts) | |||||
| 121 | { | |||||
| 122 | Syst em.Console .WriteLine (a.Name); | |||||
| 123 | } | |||||
| 124 | //</s nippetUseL inqQuery2> | |||||
| 125 | Syste m.Console. WriteLine( ); | |||||
| 126 | Syste m.Console. WriteLine( "<End of L isting>"); | |||||
| 127 | Syste m.Console. WriteLine( ); | |||||
| 128 | //OUT PUT: | |||||
| 129 | //Dis play accou nts ordere d by name | |||||
| 130 | //=== ========== ========== ========= | |||||
| 131 | //A. Datum Corp oration | |||||
| 132 | //Adv enture Wor ks | |||||
| 133 | //Coh o Vineyard | |||||
| 134 | //Fab rikam | |||||
| 135 | //Fou rth Coffee 1 | |||||
| 136 | //Fou rth Coffee 10 | |||||
| 137 | //Fou rth Coffee 2 | |||||
| 138 | //Fou rth Coffee 3 | |||||
| 139 | //Fou rth Coffee 4 | |||||
| 140 | //Fou rth Coffee 5 | |||||
| 141 | //Fou rth Coffee 6 | |||||
| 142 | //Fou rth Coffee 7 | |||||
| 143 | //Fou rth Coffee 8 | |||||
| 144 | //Fou rth Coffee 9 | |||||
| 145 | //Hum ongous Ins urance | |||||
| 146 | ||||||
| 147 | //<En d of Listi ng> | |||||
| 148 | ||||||
| 149 | ||||||
| 150 | // Fi lter multi ple entiti es using L INQ. | |||||
| 151 | //<sn ippetUseLi nqQuery3> | |||||
| 152 | var q uery = fro m c in svc Context.Co ntactSet | |||||
| 153 | joi n a in svc Context.Ac countSet | |||||
| 154 | on c.Cont actId equa ls a.Prima ryContactI d.Id | |||||
| 155 | whe re c.LastN ame == "Wi lcox" || c .LastName == "Andrew s" | |||||
| 156 | whe re a.Addre ss1_Teleph one1.Conta ins("(206) ") | |||||
| 157 | || a.Addr ess1_Telep hone1.Cont ains("(425 )") | |||||
| 158 | sel ect new | |||||
| 159 | { | |||||
| 160 | Co ntact = ne w Contact | |||||
| 161 | { | |||||
| 162 | F irstName = c.FirstNa me, | |||||
| 163 | L astName = c.LastName , | |||||
| 164 | }, | |||||
| 165 | Ac count = ne w Account | |||||
| 166 | { | |||||
| 167 | A ddress1_Te lephone1 = a.Address 1_Telephon e1 | |||||
| 168 | } | |||||
| 169 | }; | |||||
| 170 | ||||||
| 171 | Conso le.WriteLi ne("Join a ccount and contact") ; | |||||
| 172 | Conso le.WriteLi ne("List a ll records matching specified parameters "); | |||||
| 173 | Conso le.WriteLi ne("Contac t name: Wi lcox or An drews"); | |||||
| 174 | Conso le.WriteLi ne("Accoun t area cod e: 206 or 425"); | |||||
| 175 | Conso le.WriteLi ne("====== ========== ========== ========== ========== "); | |||||
| 176 | forea ch (var re cord in qu ery) | |||||
| 177 | { | |||||
| 178 | Cons ole.WriteL ine("Conta ct Name: { 0} {1}", | |||||
| 179 | record.Con tact.First Name, reco rd.Contact .LastName) ; | |||||
| 180 | Cons ole.WriteL ine("Accou nt Phone: {0}", | |||||
| 181 | record.Acc ount.Addre ss1_Teleph one1); | |||||
| 182 | } | |||||
| 183 | //</s nippetUseL inqQuery3> | |||||
| 184 | Conso le.WriteLi ne("<End o f Listing> "); | |||||
| 185 | Conso le.WriteLi ne(); | |||||
| 186 | //OUT PUT: | |||||
| 187 | //Joi n account and contac t | |||||
| 188 | //Lis t all reco rds matchi ng specifi ed paramet ers | |||||
| 189 | //Con tact name: Wilcox or Andrews | |||||
| 190 | //Acc ount area code: 206 or 425 | |||||
| 191 | //=== ========== ========== ========== ========== === | |||||
| 192 | //Con tact Name: Ben Andre ws | |||||
| 193 | //Acc ount Phone : (206)555 -5555 | |||||
| 194 | //Con tact Name: Ben Andre ws | |||||
| 195 | //Acc ount Phone : (425)555 -5555 | |||||
| 196 | //Con tact Name: Colin Wil cox | |||||
| 197 | //Acc ount Phone : (425)555 -5555 | |||||
| 198 | //<En d of Listi ng> | |||||
| 199 | ||||||
| 200 | ||||||
| 201 | ||||||
| 202 | // Bu ild a comp lex query with LINQ. This quer y includes multiple | |||||
| 203 | // JO INs and a complex WH ERE statem ent. | |||||
| 204 | //<sn ippetUseLi nqQuery4> | |||||
| 205 | var c omplexQuer y = from c in svcCon text.Conta ctSet | |||||
| 206 | join a in svcCon text.Accou ntSet | |||||
| 207 | on c.Cont actId equa ls a.Prima ryContactI d.Id | |||||
| 208 | join l in svcCon text.Creat eQuery<Lea d>() | |||||
| 209 | on a.Orig inatingLea dId.Id equ als l.Lead Id | |||||
| 210 | where c.LastName == "Wilco x" || c.La stName == "Andrews" | |||||
| 211 | where a.Address1 _Telephone 1.Contains ("(206)") | |||||
| 212 | || a.Address 1_Telephon e1.Contain s("(425)") | |||||
| 213 | select new | |||||
| 214 | { | |||||
| 215 | Conta ct = new C ontact | |||||
| 216 | { | |||||
| 217 | Firs tName = c. FirstName, | |||||
| 218 | Last Name = c.L astName, | |||||
| 219 | }, | |||||
| 220 | Accou nt = new A ccount | |||||
| 221 | { | |||||
| 222 | Addr ess1_Telep hone1 = a. Address1_T elephone1 | |||||
| 223 | }, | |||||
| 224 | Lead = new Lead | |||||
| 225 | { | |||||
| 226 | Lead Id = l.Lea dId | |||||
| 227 | } | |||||
| 228 | }; | |||||
| 229 | ||||||
| 230 | Conso le.WriteLi ne("Join a ccount, co ntact and lead"); | |||||
| 231 | Conso le.WriteLi ne("List a ll records matching specified parameters "); | |||||
| 232 | Conso le.WriteLi ne("Contac t name: Wi lcox or An drews"); | |||||
| 233 | Conso le.WriteLi ne("Accoun t area cod e: 206 or 425"); | |||||
| 234 | Conso le.WriteLi ne("====== ========== ========== ========== ========== "); | |||||
| 235 | forea ch (var re cord in co mplexQuery ) | |||||
| 236 | { | |||||
| 237 | Cons ole.WriteL ine("Lead ID: {0}", | |||||
| 238 | record.Lea d.LeadId); | |||||
| 239 | Cons ole.WriteL ine("Conta ct Name: { 0} {1}", | |||||
| 240 | record.Con tact.First Name, reco rd.Contact .LastName) ; | |||||
| 241 | Cons ole.WriteL ine("Accou nt Phone: {0}", | |||||
| 242 | record.Acc ount.Addre ss1_Teleph one1); | |||||
| 243 | } | |||||
| 244 | //</s nippetUseL inqQuery4> | |||||
| 245 | Conso le.WriteLi ne("<End o f Listing> "); | |||||
| 246 | Conso le.WriteLi ne(); | |||||
| 247 | //OUT PUT: | |||||
| 248 | //Joi n account, contact a nd lead | |||||
| 249 | //Lis t all reco rds matchi ng specifi ed paramet ers | |||||
| 250 | //Con tact name: Wilcox or Andrews | |||||
| 251 | //Acc ount area code: 206 or 425 | |||||
| 252 | //=== ========== ========== ========== ========== === | |||||
| 253 | //Lea d ID: 78d5 df14-64a3- e011-aea3- 00155dba38 18 | |||||
| 254 | //Con tact Name: Colin Wil cox | |||||
| 255 | //Acc ount Phone : (425)555 -5555 | |||||
| 256 | //<En d of Listi ng> | |||||
| 257 | ||||||
| 258 | //Ret rieve a re lated Task for a Con tact | |||||
| 259 | //Sho ws require ment that LoadProper ty must be used to a ccess the related re cord. | |||||
| 260 | //<sn ippetUseLi nqQuery5> | |||||
| 261 | Conta ct benAndr ews = svcC ontext.Con tactSet.Wh ere(c => c .FullName == "Ben An drews").Fi rstOrDefau lt(); | |||||
| 262 | if (b enAndrews != null) | |||||
| 263 | { | |||||
| 264 | //be nAndrews.C ontact_Tas ks is null until Loa dProperty is used. | |||||
| 265 | svcC ontext.Loa dProperty( benAndrews , "Contact _Tasks"); | |||||
| 266 | Task benAndrew sFirstTask = benAndr ews.Contac t_Tasks.Fi rstOrDefau lt(); | |||||
| 267 | if ( benAndrews FirstTask != null) | |||||
| 268 | { | |||||
| 269 | Con sole.Write Line("Ben Andrews fi rst task w ith Subjec t: '{0}' r etrieved." , benAndre wsFirstTas k.Subject) ; | |||||
| 270 | } | |||||
| 271 | } | |||||
| 272 | //</s nippetUseL inqQuery5> | |||||
| 273 | ||||||
| 274 | Delet eRequiredR ecords(pro mptForDele te); | |||||
| 275 | } | |||||
| 276 | } | |||||
| 277 | ||||||
| 278 | // Catc h any serv ice fault exceptions that Micr osoft Dyna mics CRM t hrows. | |||||
| 279 | catch ( FaultExcep tion<Micro soft.Xrm.S dk.Organiz ationServi ceFault>) | |||||
| 280 | { | |||||
| 281 | // You can handl e an excep tion here or pass it back to t he calling method. | |||||
| 282 | throw; | |||||
| 283 | } | |||||
| 284 | } | |||||
| 285 | ||||||
| 286 | /// <sum mary> | |||||
| 287 | /// Crea tes any en tity recor ds that th is sample requires. | |||||
| 288 | /// </su mmary> | |||||
| 289 | public v oid Create RequiredRe cords() | |||||
| 290 | { | |||||
| 291 | // Crea te 3 conta cts. | |||||
| 292 | Contact contact = new Conta ct() | |||||
| 293 | { | |||||
| 294 | FirstN ame = "Ben ", | |||||
| 295 | LastNa me = "Andr ews", | |||||
| 296 | EMailA ddress1 = "sample@ex ample.com" , | |||||
| 297 | Addres s1_City = "Redmond", | |||||
| 298 | Addres s1_StateOr Province = "WA" | |||||
| 299 | }; | |||||
| 300 | Guid be nAndrewsCo ntactId = _service.C reate(cont act); | |||||
| 301 | _contac tIds.Add(b enAndrewsC ontactId); | |||||
| 302 | ||||||
| 303 | //Creat e a task a ssociated with Ben A ndrews | |||||
| 304 | Task ta sk = new T ask() { | |||||
| 305 | Subjec t = "Sampl e Task", | |||||
| 306 | Regard ingObjectI d = new En tityRefere nce() { | |||||
| 307 | Logic alName = C ontact.Ent ityLogical Name, | |||||
| 308 | Id = benAndrews ContactId, | |||||
| 309 | Name = contact. FullName | |||||
| 310 | } | |||||
| 311 | }; | |||||
| 312 | _taskId s.Add(_ser vice.Creat e(task)); | |||||
| 313 | ||||||
| 314 | ||||||
| 315 | ||||||
| 316 | contact = new Con tact() | |||||
| 317 | { | |||||
| 318 | FirstN ame = "Col in", | |||||
| 319 | LastNa me = "Wilc ox", | |||||
| 320 | EMailA ddress1 = "sample@ex ample.com" , | |||||
| 321 | Addres s1_City = "Bellevue" , | |||||
| 322 | Addres s1_StateOr Province = "WA" | |||||
| 323 | }; | |||||
| 324 | _contac tIds.Add(_ service.Cr eate(conta ct)); | |||||
| 325 | ||||||
| 326 | contact = new Con tact() | |||||
| 327 | { | |||||
| 328 | FirstN ame = "Ben ", | |||||
| 329 | LastNa me = "Smit h", | |||||
| 330 | EMailA ddress1 = "sample@ex ample.com" , | |||||
| 331 | Addres s1_City = "Bellevue" , | |||||
| 332 | Addres s1_StateOr Province = "WA" | |||||
| 333 | }; | |||||
| 334 | _contac tIds.Add(_ service.Cr eate(conta ct)); | |||||
| 335 | ||||||
| 336 | // Crea te 3 leads . | |||||
| 337 | Lead le ad = new L ead() | |||||
| 338 | { | |||||
| 339 | FirstN ame = "Dan ", | |||||
| 340 | LastNa me = "Wils on", | |||||
| 341 | EMailA ddress1 = "sample@ex ample.com" , | |||||
| 342 | Addres s1_City = "Redmond", | |||||
| 343 | Addres s1_StateOr Province = "WA" | |||||
| 344 | }; | |||||
| 345 | _leadId s.Add(_ser vice.Creat e(lead)); | |||||
| 346 | ||||||
| 347 | lead = new Lead() | |||||
| 348 | { | |||||
| 349 | FirstN ame = "Jim ", | |||||
| 350 | LastNa me = "Wils on", | |||||
| 351 | EMailA ddress1 = "sample@ex ample.com" , | |||||
| 352 | Addres s1_City = "Bellevue" , | |||||
| 353 | Addres s1_StateOr Province = "WA" | |||||
| 354 | }; | |||||
| 355 | _leadId s.Add(_ser vice.Creat e(lead)); | |||||
| 356 | ||||||
| 357 | lead = new Lead() | |||||
| 358 | { | |||||
| 359 | FirstN ame = "Den ise", | |||||
| 360 | LastNa me = "Smit h", | |||||
| 361 | EMailA ddress1 = "sample@ex ample.com" , | |||||
| 362 | Addres s1_City = "Bellevue" , | |||||
| 363 | Addres s1_StateOr Province = "WA" | |||||
| 364 | }; | |||||
| 365 | _leadId s.Add(_ser vice.Creat e(lead)); | |||||
| 366 | ||||||
| 367 | // Crea te 5 custo mized Acco unts for t he LINQ sa mples. | |||||
| 368 | Account account = new Accou nt | |||||
| 369 | { | |||||
| 370 | Name = "A. Datum Corporati on", | |||||
| 371 | Addres s1_StateOr Province = "Colorado ", | |||||
| 372 | Addres s1_Telepho ne1 = "(20 6)555-5555 ", | |||||
| 373 | Primar yContactId = | |||||
| 374 | ne w EntityRe ference(Co ntact.Enti tyLogicalN ame, _cont actIds[0]) | |||||
| 375 | }; | |||||
| 376 | _accoun tIds.Add(_ service.Cr eate(accou nt)); | |||||
| 377 | ||||||
| 378 | account = new Acc ount | |||||
| 379 | { | |||||
| 380 | Name = "Adventur e Works", | |||||
| 381 | Addres s1_StateOr Province = "Illinois ", | |||||
| 382 | Addres s1_County = "Lake Co unty", | |||||
| 383 | Addres s1_Telepho ne1 = "(20 6)555-5555 ", | |||||
| 384 | Origin atingLeadI d = | |||||
| 385 | ne w EntityRe ference(Le ad.EntityL ogicalName , _leadIds [0]) | |||||
| 386 | }; | |||||
| 387 | _accoun tIds.Add(_ service.Cr eate(accou nt)); | |||||
| 388 | ||||||
| 389 | account = new Acc ount | |||||
| 390 | { | |||||
| 391 | Name = "Coho Vin eyard", | |||||
| 392 | Addres s1_StateOr Province = "Washingt on", | |||||
| 393 | Addres s1_County = "King Co unty", | |||||
| 394 | Addres s1_Telepho ne1 = "(42 5)555-5555 ", | |||||
| 395 | Primar yContactId = | |||||
| 396 | ne w EntityRe ference(Co ntact.Enti tyLogicalN ame, _cont actIds[1]) , | |||||
| 397 | Origin atingLeadI d = | |||||
| 398 | ne w EntityRe ference(Le ad.EntityL ogicalName , _leadIds [0]) | |||||
| 399 | }; | |||||
| 400 | _accoun tIds.Add(_ service.Cr eate(accou nt)); | |||||
| 401 | ||||||
| 402 | account = new Acc ount | |||||
| 403 | { | |||||
| 404 | Name = "Fabrikam ", | |||||
| 405 | Addres s1_StateOr Province = "Washingt on", | |||||
| 406 | Addres s1_Telepho ne1 = "(42 5)555-5555 ", | |||||
| 407 | Primar yContactId = | |||||
| 408 | ne w EntityRe ference(Co ntact.Enti tyLogicalN ame, _cont actIds[0]) | |||||
| 409 | }; | |||||
| 410 | _accoun tIds.Add(_ service.Cr eate(accou nt)); | |||||
| 411 | ||||||
| 412 | account = new Acc ount | |||||
| 413 | { | |||||
| 414 | Name = "Humongou s Insuranc e", | |||||
| 415 | Addres s1_StateOr Province = "Missouri ", | |||||
| 416 | Addres s1_County = "Saint L ouis Count y", | |||||
| 417 | Addres s1_Telepho ne1 = "(31 4)555-5555 ", | |||||
| 418 | Primar yContactId = | |||||
| 419 | ne w EntityRe ference(Co ntact.Enti tyLogicalN ame, _cont actIds[1]) | |||||
| 420 | }; | |||||
| 421 | _accoun tIds.Add(_ service.Cr eate(accou nt)); | |||||
| 422 | ||||||
| 423 | // Crea te 10 basi c Account records. | |||||
| 424 | for (in t i = 1; i <= 10; i+ +) | |||||
| 425 | { | |||||
| 426 | accoun t = new Ac count | |||||
| 427 | { | |||||
| 428 | Name = "Fourth Coffee " + i, | |||||
| 429 | Addre ss1_StateO rProvince = "Califor nia" | |||||
| 430 | }; | |||||
| 431 | _accou ntIds.Add( _service.C reate(acco unt)); | |||||
| 432 | } | |||||
| 433 | } | |||||
| 434 | ||||||
| 435 | /// <sum mary> | |||||
| 436 | /// Dele tes any en tity recor ds that we re created for this sample. | |||||
| 437 | /// <par am name="p rompt">Ind icates whe ther to pr ompt the u ser | |||||
| 438 | /// to d elete the records cr eated in t his sample .</param> | |||||
| 439 | /// </su mmary> | |||||
| 440 | public v oid Delete RequiredRe cords(bool prompt) | |||||
| 441 | { | |||||
| 442 | bool to BeDeleted = true; | |||||
| 443 | ||||||
| 444 | if (pro mpt) | |||||
| 445 | { | |||||
| 446 | // Ask the user if the cre ated entit ies should be delete d. | |||||
| 447 | Consol e.Write("\ nDo you wa nt these e ntity reco rds delete d? (y/n) [ y]: "); | |||||
| 448 | String answer = Console.Re adLine(); | |||||
| 449 | if (an swer.Start sWith("y") || | |||||
| 450 | an swer.Start sWith("Y") || | |||||
| 451 | an swer == St ring.Empty ) | |||||
| 452 | { | |||||
| 453 | toBeD eleted = t rue; | |||||
| 454 | } | |||||
| 455 | else | |||||
| 456 | { | |||||
| 457 | toBeD eleted = f alse; | |||||
| 458 | } | |||||
| 459 | } | |||||
| 460 | ||||||
| 461 | if (toB eDeleted) | |||||
| 462 | { | |||||
| 463 | // Del ete all re cords crea ted in thi s sample. | |||||
| 464 | ||||||
| 465 | ||||||
| 466 | foreac h (Guid ta skId in _t askIds) | |||||
| 467 | { | |||||
| 468 | _serv ice.Delete (Task.Enti tyLogicalN ame, taskI d); | |||||
| 469 | } | |||||
| 470 | foreac h (Guid ac countId in _accountI ds) | |||||
| 471 | { | |||||
| 472 | _serv ice.Delete (Account.E ntityLogic alName, ac countId); | |||||
| 473 | } | |||||
| 474 | foreac h (Guid co ntactId in _contactI ds) | |||||
| 475 | { | |||||
| 476 | _serv ice.Delete (Contact.E ntityLogic alName, co ntactId); | |||||
| 477 | } | |||||
| 478 | foreac h (Guid le adId in _l eadIds) | |||||
| 479 | { | |||||
| 480 | _serv ice.Delete (Lead.Enti tyLogicalN ame, leadI d); | |||||
| 481 | } | |||||
| 482 | Consol e.WriteLin e("Entity record(s) have been deleted.") ; | |||||
| 483 | } | |||||
| 484 | } | |||||
| 485 | ||||||
| 486 | #endregi on How To Sample Cod e | |||||
| 487 | ||||||
| 488 | #region Main metho d | |||||
| 489 | ||||||
| 490 | /// <sum mary> | |||||
| 491 | /// Stan dard Main( ) method u sed by mos t SDK samp les. | |||||
| 492 | /// </su mmary> | |||||
| 493 | /// <par am name="a rgs"></par am> | |||||
| 494 | static p ublic void Main(stri ng[] args) | |||||
| 495 | { | |||||
| 496 | try | |||||
| 497 | { | |||||
| 498 | // Obt ain the ta rget organ ization's Web addres s and clie nt logon | |||||
| 499 | // cre dentials f rom the us er. | |||||
| 500 | Server Connection serverCon nect = new ServerCon nection(); | |||||
| 501 | Server Connection .Configura tion confi g = server Connect.Ge tServerCon figuration (); | |||||
| 502 | ||||||
| 503 | UseLin qQuery app = new Use LinqQuery( ); | |||||
| 504 | app.Ru n(config, true); | |||||
| 505 | } | |||||
| 506 | catch ( FaultExcep tion<Micro soft.Xrm.S dk.Organiz ationServi ceFault> e x) | |||||
| 507 | { | |||||
| 508 | Consol e.WriteLin e("The app lication t erminated with an er ror."); | |||||
| 509 | Consol e.WriteLin e("Timesta mp: {0}", ex.Detail. Timestamp) ; | |||||
| 510 | Consol e.WriteLin e("Code: { 0}", ex.De tail.Error Code); | |||||
| 511 | Consol e.WriteLin e("Message : {0}", ex .Detail.Me ssage); | |||||
| 512 | Consol e.WriteLin e("Plugin Trace: {0} ", ex.Deta il.TraceTe xt); | |||||
| 513 | Consol e.WriteLin e("Inner F ault: {0}" , | |||||
| 514 | nu ll == ex.D etail.Inne rFault ? " No Inner F ault" : "H as Inner F ault"); | |||||
| 515 | } | |||||
| 516 | catch ( System.Tim eoutExcept ion ex) | |||||
| 517 | { | |||||
| 518 | Consol e.WriteLin e("The app lication t erminated with an er ror."); | |||||
| 519 | Consol e.WriteLin e("Message : {0}", ex .Message); | |||||
| 520 | Consol e.WriteLin e("Stack T race: {0}" , ex.Stack Trace); | |||||
| 521 | Consol e.WriteLin e("Inner F ault: {0}" , | |||||
| 522 | nu ll == ex.I nnerExcept ion.Messag e ? "No In ner Fault" : ex.Inne rException .Message); | |||||
| 523 | } | |||||
| 524 | catch ( System.Exc eption ex) | |||||
| 525 | { | |||||
| 526 | Consol e.WriteLin e("The app lication t erminated with an er ror."); | |||||
| 527 | Consol e.WriteLin e(ex.Messa ge); | |||||
| 528 | ||||||
| 529 | // Dis play the d etails of the inner exception. | |||||
| 530 | if (ex .InnerExce ption != n ull) | |||||
| 531 | { | |||||
| 532 | Conso le.WriteLi ne(ex.Inne rException .Message); | |||||
| 533 | ||||||
| 534 | Fault Exception< Microsoft. Xrm.Sdk.Or ganization ServiceFau lt> fe = e x.InnerExc eption | |||||
| 535 | a s FaultExc eption<Mic rosoft.Xrm .Sdk.Organ izationSer viceFault> ; | |||||
| 536 | if (f e != null) | |||||
| 537 | { | |||||
| 538 | Cons ole.WriteL ine("Times tamp: {0}" , fe.Detai l.Timestam p); | |||||
| 539 | Cons ole.WriteL ine("Code: {0}", fe. Detail.Err orCode); | |||||
| 540 | Cons ole.WriteL ine("Messa ge: {0}", fe.Detail. Message); | |||||
| 541 | Cons ole.WriteL ine("Plugi n Trace: { 0}", fe.De tail.Trace Text); | |||||
| 542 | Cons ole.WriteL ine("Inner Fault: {0 }", | |||||
| 543 | null == fe .Detail.In nerFault ? "No Inner Fault" : "Has Inner Fault"); | |||||
| 544 | } | |||||
| 545 | } | |||||
| 546 | } | |||||
| 547 | // Addi tional exc eptions to catch: Se curityToke nValidatio nException , ExpiredS ecurityTok enExceptio n, | |||||
| 548 | // Secu rityAccess DeniedExce ption, Mes sageSecuri tyExceptio n, and Sec urityNegot iationExce ption. | |||||
| 549 | ||||||
| 550 | finally | |||||
| 551 | { | |||||
| 552 | Consol e.WriteLin e("Press < Enter> to exit."); | |||||
| 553 | Consol e.ReadLine (); | |||||
| 554 | } | |||||
| 555 | } | |||||
| 556 | #endregi on Main me thod | |||||
| 557 | } | |||||
| 558 | } | |||||
| 559 | //</snippe tUseLinqQu ery>> |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.