Produced by Araxis Merge on 2/1/2017 2:56:33 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\EarlyBound | BasicContextExamples.cs | Tue Dec 20 19:51:45 2016 UTC |
| 2 | Wed Feb 1 19:56:33 2017 UTC |
| Description | Between Files 1 and 2 |
|
|---|---|---|
| Text Blocks | Lines | |
| Unchanged | 0 | 0 |
| Changed | 0 | 0 |
| Inserted | 0 | 0 |
| Removed | 1 | 525 |
| 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 BasicConte xtExamples > | |||||
| 17 | using Syst em; | |||||
| 18 | using Syst em.IO; | |||||
| 19 | using Syst em.Linq; | |||||
| 20 | using Syst em.Service Model; | |||||
| 21 | using Syst em.Text; | |||||
| 22 | ||||||
| 23 | // These n amespaces are found in the Mic rosoft.Xrm .Sdk.dll a ssembly | |||||
| 24 | // located in the SD K\bin fold er of the SDK downlo ad. | |||||
| 25 | using Micr osoft.Xrm. Sdk; | |||||
| 26 | using Micr osoft.Xrm. Sdk.Client ; | |||||
| 27 | ||||||
| 28 | // These n amespaces are found in the Mic rosoft.Crm .Sdk.Proxy .dll assem bly | |||||
| 29 | // located in the SD K\bin fold er of the SDK downlo ad. | |||||
| 30 | using Micr osoft.Crm. Sdk.Messag es; | |||||
| 31 | ||||||
| 32 | namespace Microsoft. Crm.Sdk.Sa mples | |||||
| 33 | { | |||||
| 34 | /// <s ummary> | |||||
| 35 | /// De monstrates how to do use the O rganizatio nServiceCo ntext to w ork with m ultiple | |||||
| 36 | /// re cords simu ltaneously .</summary > | |||||
| 37 | /// <r emarks> | |||||
| 38 | /// NO TE: To exe cute file attachment step, you must have a text fi le named | |||||
| 39 | /// "s ample.txt" in the fo lder in wh ich this c ode is exe cuted. | |||||
| 40 | /// | |||||
| 41 | /// At run-time, you will be given t he option to delete all the | |||||
| 42 | /// da tabase rec ords creat ed by this program.< /remarks> | |||||
| 43 | public class Bas icContextE xamples | |||||
| 44 | { | |||||
| 45 | #r egion Clas s Level Me mbers | |||||
| 46 | ||||||
| 47 | pr ivate Guid _accountI d; | |||||
| 48 | pr ivate Guid _contactI d; | |||||
| 49 | pr ivate Guid _annotati onId; | |||||
| 50 | pr ivate Guid _priceLev elId; | |||||
| 51 | pr ivate Guid _quoteId; | |||||
| 52 | pr ivate Guid _productI d; | |||||
| 53 | pr ivate Guid _productP riceLevelI d; | |||||
| 54 | pr ivate Guid _quoteDet ailId; | |||||
| 55 | pr ivate Guid _opportun ityId; | |||||
| 56 | pr ivate Guid _incident Id; | |||||
| 57 | pr ivate Orga nizationSe rviceProxy _serviceP roxy; | |||||
| 58 | pr ivate Orga nizationSe rviceConte xt _orgCon text; | |||||
| 59 | pr ivate IOrg anizationS ervice _se rvice; | |||||
| 60 | ||||||
| 61 | #e ndregion C lass Level Members | |||||
| 62 | ||||||
| 63 | #r egion How To Sample Code | |||||
| 64 | // / <summary > | |||||
| 65 | // / This met hod first connects t o the Orga nization s ervice and creates t he | |||||
| 66 | // / Organiza tionServic eContext. Then, seve ral entity creation and relati onship | |||||
| 67 | // / operatio ns are per formed. | |||||
| 68 | // / </summar y> | |||||
| 69 | // / <param n ame="serve rConfig">C ontains se rver conne ction info rmation.</ param> | |||||
| 70 | // / <param n ame="promp tforDelete ">When Tru e, the use r will be prompted t o delete a ll | |||||
| 71 | // / created entities.< /param> | |||||
| 72 | pu blic void Run(Server Connection .Configura tion serve rConfig, b ool prompt forDelete) | |||||
| 73 | { | |||||
| 74 | try | |||||
| 75 | { | |||||
| 76 | //<s nippetBasi cContextEx amples1> | |||||
| 77 | // C onnect to the Organi zation ser vice. | |||||
| 78 | // T he using s tatement a ssures tha t the serv ice proxy will be pr operly dis posed. | |||||
| 79 | usin g (_servic eProxy = n ew Organiz ationServi ceProxy(se rverConfig .Organizat ionUri, se rverConfig .HomeRealm Uri, | |||||
| 80 | s erverConfi g.Credenti als, serve rConfig.De viceCreden tials)) | |||||
| 81 | { | |||||
| 82 | // This st atement is required to enable early-boun d type sup port. | |||||
| 83 | _servicePr oxy.Enable ProxyTypes (); | |||||
| 84 | ||||||
| 85 | _service = (IOrganiz ationServi ce)_servic eProxy; | |||||
| 86 | ||||||
| 87 | CreateRequ iredRecord s(); | |||||
| 88 | ||||||
| 89 | // The Org anizationS erviceCont ext is an object tha t wraps th e service | |||||
| 90 | // proxy a nd allows creating/u pdating mu ltiple rec ords simul taneously. | |||||
| 91 | _orgContex t = new Or ganization ServiceCon text(_serv ice); | |||||
| 92 | ||||||
| 93 | // Create a new cont act called Mary Kay Andersen. | |||||
| 94 | var contac t = new Co ntact() | |||||
| 95 | { | |||||
| 96 | FirstN ame = "Mar y Kay", | |||||
| 97 | LastNa me = "Ande rsen", | |||||
| 98 | Addres s1_Line1 = "23 Marke t St.", | |||||
| 99 | Addres s1_City = "Sammamish ", | |||||
| 100 | Addres s1_StateOr Province = "MT", | |||||
| 101 | Addres s1_PostalC ode = "999 99", | |||||
| 102 | Teleph one1 = "12 345678", | |||||
| 103 | EMailA ddress1 = "marykay@c ontoso.com ", | |||||
| 104 | Id = G uid.NewGui d() | |||||
| 105 | }; | |||||
| 106 | _contactId = contact .Id; | |||||
| 107 | _orgContex t.AddObjec t(contact) ; | |||||
| 108 | Console.Wr ite("Insta ntiating c ontact, ") ; | |||||
| 109 | ||||||
| 110 | // Create an account called Co ntoso. | |||||
| 111 | var accoun t = new Ac count() | |||||
| 112 | { | |||||
| 113 | Name = "Contoso" , | |||||
| 114 | Addres s1_City = "Redmond", | |||||
| 115 | // set the accou nt categor y to 'Pref erred Cust omer' | |||||
| 116 | Accoun tCategoryC ode = new OptionSetV alue(1), | |||||
| 117 | LastUs edInCampai gn = DateT ime.Now, | |||||
| 118 | Market Cap = new Money(1200 00), | |||||
| 119 | DoNotE Mail = tru e, | |||||
| 120 | Descri ption = "C ontoso is a fictiona l company! ", | |||||
| 121 | Id = G uid.NewGui d(), | |||||
| 122 | }; | |||||
| 123 | _accountId = account .Id; | |||||
| 124 | Console.Wr ite("insta ntiating a ccount, ") ; | |||||
| 125 | ||||||
| 126 | // Set Mar y Kay Ande rsen as th e primary contact | |||||
| 127 | _orgContex t.AddRelat edObject( | |||||
| 128 | contac t, | |||||
| 129 | new Re lationship ("account_ primary_co ntact"), | |||||
| 130 | accoun t); | |||||
| 131 | SaveChange sHelper(co ntact, acc ount); | |||||
| 132 | Console.Wr iteLine("a nd creatin g both rec ords in CR M."); | |||||
| 133 | ||||||
| 134 | // Remove the primar y contact value from Mary Kay Andersen | |||||
| 135 | _orgContex t.Attach(c ontact); | |||||
| 136 | _orgContex t.DeleteLi nk( | |||||
| 137 | contac t, | |||||
| 138 | new Re lationship ("account_ primary_co ntact"), | |||||
| 139 | accoun t); | |||||
| 140 | SaveChange sHelper(co ntact, acc ount); | |||||
| 141 | Console.Wr ite("Remov ing primar y contact status, ") ; | |||||
| 142 | ||||||
| 143 | // Add Mar y Kay Ande rsen to th e contact list for t he account Contoso. | |||||
| 144 | _orgContex t.Attach(a ccount); | |||||
| 145 | _orgContex t.Attach(c ontact); | |||||
| 146 | _orgContex t.AddLink( | |||||
| 147 | accoun t, | |||||
| 148 | new Re lationship ("contact_ customer_a ccounts"), | |||||
| 149 | contac t); | |||||
| 150 | SaveChange sHelper(co ntact, acc ount); | |||||
| 151 | Console.Wr iteLine("a nd adding contact to account's contact l ist."); | |||||
| 152 | ||||||
| 153 | // Add a n ote with a document attachment to the co ntact's re cord. | |||||
| 154 | var attach ment = Fil e.OpenRead ("sample.t xt"); | |||||
| 155 | var data = new byte[ attachment .Length]; | |||||
| 156 | attachment .Read(data , 0, (int) attachment .Length); | |||||
| 157 | ||||||
| 158 | var note = new Annot ation() | |||||
| 159 | { | |||||
| 160 | Subjec t = "Note subject... ", | |||||
| 161 | NoteTe xt = "Note Details.. ..", | |||||
| 162 | Docume ntBody = C onvert.ToB ase64Strin g(data), | |||||
| 163 | FileNa me = Path. GetFileNam e(attachme nt.Name), | |||||
| 164 | MimeTy pe = "text /plain", | |||||
| 165 | Id = G uid.NewGui d(), | |||||
| 166 | // Ass ociate the note to t he contact . | |||||
| 167 | Object Id = conta ct.ToEntit yReference (), | |||||
| 168 | Object TypeCode = Contact.E ntityLogic alName | |||||
| 169 | }; | |||||
| 170 | _annotatio nId = note .Id; | |||||
| 171 | Console.Wr ite("Insta ntiating a note, "); | |||||
| 172 | _orgContex t.AddObjec t(note); | |||||
| 173 | _orgContex t.Attach(c ontact); | |||||
| 174 | // Set the contact a s the Rega rding attr ibute of t he note. | |||||
| 175 | _orgContex t.AddLink( | |||||
| 176 | contac t, | |||||
| 177 | new Re lationship ("Contact_ Annotation "), | |||||
| 178 | note); | |||||
| 179 | SaveChange sHelper(no te, contac t); | |||||
| 180 | Console.Wr iteLine("c reating th e note in CRM and li nking to c ontact."); | |||||
| 181 | ||||||
| 182 | // Change the owning user of t he contact Mary Kay Andersen | |||||
| 183 | // Find a user with an email a ddress of "someone@e xample.com " | |||||
| 184 | var newOwn er = (from u in _org Context.Cr eateQuery< SystemUser >() | |||||
| 185 | wher e u.Intern alEMailAdd ress == "s omeone@exa mple.com" | |||||
| 186 | sele ct u).Sing le(); | |||||
| 187 | AssignRequ est assign Request = new Assign Request() | |||||
| 188 | { | |||||
| 189 | Target = contact .ToEntityR eference() , | |||||
| 190 | Assign ee = newOw ner.ToEnti tyReferenc e() | |||||
| 191 | }; | |||||
| 192 | _orgContex t.Execute( assignRequ est); | |||||
| 193 | Console.Wr iteLine("C hanging ow nership of contact r ecord."); | |||||
| 194 | ||||||
| 195 | // Create a new pric e list cal led Retail Price Lis t. | |||||
| 196 | var priceL ist = new PriceLevel () | |||||
| 197 | { | |||||
| 198 | Name = "Retail P rice List" , | |||||
| 199 | BeginD ate = Date Time.Now, | |||||
| 200 | EndDat e = DateTi me.Now, | |||||
| 201 | Descri ption = "C ontoso's p rimary pri celist.", | |||||
| 202 | Id = G uid.NewGui d() | |||||
| 203 | }; | |||||
| 204 | _priceLeve lId = pric eList.Id; | |||||
| 205 | _orgContex t.AddObjec t(priceLis t); | |||||
| 206 | Console.Wr ite("Insta ntiating p rice list "); | |||||
| 207 | ||||||
| 208 | // Create a new prod uct called Widget A. | |||||
| 209 | var newPro duct = new Product() | |||||
| 210 | { | |||||
| 211 | Name = "Widget A ", | |||||
| 212 | Descri ption = "I ndustrial widget for hi-tech i ndustries" , | |||||
| 213 | Produc tStructure = new Opt ionSetValu e(1), // 1 = Product | |||||
| 214 | Quanti tyOnHand = 2, | |||||
| 215 | Produc tNumber = "WIDG-A", | |||||
| 216 | Price = new Mone y(decimal. Parse("12. 50")), | |||||
| 217 | Quanti tyDecimal = 2, // Se ts the Dec imals Supp orted valu e | |||||
| 218 | Id = G uid.NewGui d(), | |||||
| 219 | Defaul tUoMSchedu leId = new EntityRef erence( | |||||
| 220 | UoMSched ule.Entity LogicalNam e, | |||||
| 221 | _orgCont ext.Create Query<UoMS chedule>() .First().I d), | |||||
| 222 | Defaul tUoMId = n ew EntityR eference( | |||||
| 223 | UoM.Enti tyLogicalN ame, | |||||
| 224 | _orgCont ext.Create Query<UoM> ().First() .Id) | |||||
| 225 | }; | |||||
| 226 | _productId = newProd uct.Id; | |||||
| 227 | _orgContex t.AddObjec t(newProdu ct); | |||||
| 228 | Console.Wr iteLine("a nd product ."); | |||||
| 229 | SaveChange sHelper(pr iceList, n ewProduct) ; | |||||
| 230 | ||||||
| 231 | // Add Wid get A to t he Retail Price List . | |||||
| 232 | var priceL evelProduc t = new Pr oductPrice Level() | |||||
| 233 | { | |||||
| 234 | Produc tId = newP roduct.ToE ntityRefer ence(), | |||||
| 235 | UoMId = newProdu ct.Default UoMId, | |||||
| 236 | Amount = new Mon ey(decimal .Parse("12 .50")), | |||||
| 237 | PriceL evelId = p riceList.T oEntityRef erence(), | |||||
| 238 | Id = G uid.NewGui d() | |||||
| 239 | }; | |||||
| 240 | _productPr iceLevelId = priceLe velProduct .Id; | |||||
| 241 | _orgContex t.AddObjec t(priceLev elProduct) ; | |||||
| 242 | Console.Wr ite("Assoc iating pro duct to pr ice list, "); | |||||
| 243 | ||||||
| 244 | // Publish the produ ct | |||||
| 245 | SetStateRe quest publ ishRequest = new Set StateReque st | |||||
| 246 | { | |||||
| 247 | Entity Moniker = newProduct .ToEntityR eference() , | |||||
| 248 | State = new Opti onSetValue ((int)Prod uctState.A ctive), | |||||
| 249 | Status = new Opt ionSetValu e(1) | |||||
| 250 | }; | |||||
| 251 | _servicePr oxy.Execut e(publishR equest); | |||||
| 252 | Console.Wr iteLine("a nd publish ing the pr oduct."); | |||||
| 253 | ||||||
| 254 | ||||||
| 255 | // Create a new quot e for Cont oso. | |||||
| 256 | var newQuo te = new Q uote() | |||||
| 257 | { | |||||
| 258 | Name = "Quotatio n for Cont oso", | |||||
| 259 | // Set s the pric elist to t he one we' ve just cr eated | |||||
| 260 | PriceL evelId = p riceList.T oEntityRef erence(), | |||||
| 261 | Id = G uid.NewGui d(), | |||||
| 262 | Custom erId = acc ount.ToEnt ityReferen ce() | |||||
| 263 | }; | |||||
| 264 | _quoteId = newQuote. Id; | |||||
| 265 | _orgContex t.AddObjec t(newQuote ); | |||||
| 266 | _orgContex t.Attach(a ccount); | |||||
| 267 | _orgContex t.AddLink( | |||||
| 268 | newQuo te, | |||||
| 269 | new Re lationship ("quote_cu stomer_acc ounts"), | |||||
| 270 | accoun t); | |||||
| 271 | Console.Wr ite("Insta ntiating a quote, ") ; | |||||
| 272 | ||||||
| 273 | // Add a q uote produ ct to this quote. | |||||
| 274 | var quoteP roduct = n ew QuoteDe tail() | |||||
| 275 | { | |||||
| 276 | Produc tId = newP roduct.ToE ntityRefer ence(), | |||||
| 277 | Quanti ty = 1, | |||||
| 278 | QuoteI d = newQuo te.ToEntit yReference (), | |||||
| 279 | UoMId = newProdu ct.Default UoMId, | |||||
| 280 | Id = G uid.NewGui d() | |||||
| 281 | }; | |||||
| 282 | _quoteDeta ilId = quo teProduct. Id; | |||||
| 283 | _orgContex t.AddObjec t(quotePro duct); | |||||
| 284 | Console.Wr iteLine("a nd adding product to quote."); | |||||
| 285 | ||||||
| 286 | // Create a sales op portunity with Conto so. | |||||
| 287 | var oppty = new Oppo rtunity() | |||||
| 288 | { | |||||
| 289 | Name = "Interest ed in Widg et A", | |||||
| 290 | Estima tedCloseDa te = DateT ime.Now.Ad dDays(30.0 ), | |||||
| 291 | Estima tedValue = new Money (decimal.P arse("3000 00.00")), | |||||
| 292 | CloseP robability = 25, // 25% probab ility of c losing thi s deal | |||||
| 293 | IsReve nueSystemC alculated = false, / / user-cal culated re venue | |||||
| 294 | Opport unityRatin gCode = ne w OptionSe tValue(2), // warm | |||||
| 295 | Custom erId = acc ount.ToEnt ityReferen ce(), | |||||
| 296 | Id = G uid.NewGui d() | |||||
| 297 | }; | |||||
| 298 | _opportuni tyId = opp ty.Id; | |||||
| 299 | _orgContex t.AddObjec t(oppty); | |||||
| 300 | Console.Wr ite("Insta ntiating o pportunity , "); | |||||
| 301 | //_orgCont ext.AddLin k( | |||||
| 302 | // oppt y, | |||||
| 303 | // new Relationsh ip("opport unity_cust omer_accou nts"), | |||||
| 304 | // acco unt); | |||||
| 305 | SaveChange sHelper(pr iceList, n ewQuote, n ewProduct, priceLeve lProduct, | |||||
| 306 | quoteP roduct, op pty, accou nt); | |||||
| 307 | Console.Wr iteLine("a nd creatin g all reco rds in CRM ."); | |||||
| 308 | ||||||
| 309 | // Associa te quote t o contact, which add s the Cont act record in the | |||||
| 310 | // "Other Contacts" section of a Quote r ecord. | |||||
| 311 | _orgContex t.Attach(c ontact); | |||||
| 312 | _orgContex t.Attach(n ewQuote); | |||||
| 313 | _orgContex t.AddLink( | |||||
| 314 | contac t, | |||||
| 315 | new Re lationship ("contactq uotes_asso ciation"), | |||||
| 316 | newQuo te); | |||||
| 317 | SaveChange sHelper(co ntact, new Quote); | |||||
| 318 | Console.Wr iteLine("A ssociating contact a nd quote." ); | |||||
| 319 | ||||||
| 320 | // Create a case for Mary Kay Andersen. | |||||
| 321 | var servic eRequest = new Incid ent() | |||||
| 322 | { | |||||
| 323 | Title = "Problem with Widg et B", | |||||
| 324 | Priori tyCode = n ew OptionS etValue(1) , // 1 = H igh | |||||
| 325 | CaseOr iginCode = new Optio nSetValue( 1), // 1 = Phone | |||||
| 326 | CaseTy peCode = n ew OptionS etValue(2) , // 2 = P roblem | |||||
| 327 | Subjec tId = | |||||
| 328 | ne w EntityRe ference( | |||||
| 329 | Subject. EntityLogi calName, | |||||
| 330 | _orgCont ext.Create Query<Subj ect>() | |||||
| 331 | .Fir st().Id), // use th e default subject | |||||
| 332 | Descri ption = "C ustomer ca n't switch the produ ct on.", | |||||
| 333 | Follow upBy = Dat eTime.Now. AddHours(3 .0), // fo llow-up in 3 hours | |||||
| 334 | Custom erId = con tact.ToEnt ityReferen ce(), | |||||
| 335 | Id = G uid.NewGui d() | |||||
| 336 | }; | |||||
| 337 | _incidentI d = servic eRequest.I d; | |||||
| 338 | _orgContex t.AddObjec t(serviceR equest); | |||||
| 339 | _orgContex t.Attach(c ontact); | |||||
| 340 | _orgContex t.AddLink( | |||||
| 341 | servic eRequest, | |||||
| 342 | new Re lationship ("incident _customer_ contacts") , | |||||
| 343 | contac t); | |||||
| 344 | SaveChange sHelper(se rviceReque st, contac t); | |||||
| 345 | Console.Wr iteLine("C reating se rvice case for conta ct."); | |||||
| 346 | ||||||
| 347 | // Deactiv ate the Ma ry Kay And ersen cont act record . | |||||
| 348 | SetStateRe quest setI nactiveReq uest = new SetStateR equest | |||||
| 349 | { | |||||
| 350 | Entity Moniker = contact.To EntityRefe rence(), | |||||
| 351 | State = new Opti onSetValue ((int)Cont actState.I nactive), | |||||
| 352 | Status = new Opt ionSetValu e(2) | |||||
| 353 | }; | |||||
| 354 | _orgContex t.Execute( setInactiv eRequest); | |||||
| 355 | Console.Wr iteLine("D eactivatin g the cont act record ."); | |||||
| 356 | ||||||
| 357 | DeleteRequ iredRecord s(promptfo rDelete); | |||||
| 358 | } | |||||
| 359 | //</ snippetBas icContextE xamples1> | |||||
| 360 | } | |||||
| 361 | ||||||
| 362 | // Catch any servi ce fault e xceptions that Micro soft Dynam ics CRM th rows. | |||||
| 363 | catch (F aultExcept ion<Micros oft.Xrm.Sd k.Organiza tionServic eFault>) | |||||
| 364 | { | |||||
| 365 | // Y ou can han dle an exc eption her e or pass it back to the calli ng method. | |||||
| 366 | thro w; | |||||
| 367 | } | |||||
| 368 | } | |||||
| 369 | ||||||
| 370 | pr ivate void SaveChang esHelper(p arams Enti ty[] entit ies) | |||||
| 371 | { | |||||
| 372 | SaveChan gesResultC ollection results = _orgContex t.SaveChan ges(); | |||||
| 373 | if (resu lts.HasErr or) | |||||
| 374 | { | |||||
| 375 | Dele teRequired Records(fa lse); | |||||
| 376 | Stri ngBuilder sb = new S tringBuild er(); | |||||
| 377 | fore ach (var r esult in r esults) | |||||
| 378 | { | |||||
| 379 | if (result .Error != null) | |||||
| 380 | { | |||||
| 381 | sb.App endFormat( "Error: {0 }\n", resu lt.Error.M essage); | |||||
| 382 | } | |||||
| 383 | } | |||||
| 384 | thro w new Inva lidOperati onExceptio n("Failure occurred while " + | |||||
| 385 | "calling O rganizatio nServiceCo ntext.Save Changes()\ n" + | |||||
| 386 | sb.ToStrin g()); | |||||
| 387 | } | |||||
| 388 | else | |||||
| 389 | { | |||||
| 390 | fore ach (Entit y e in ent ities) | |||||
| 391 | { | |||||
| 392 | e.EntitySt ate = null ; | |||||
| 393 | } | |||||
| 394 | } | |||||
| 395 | } | |||||
| 396 | ||||||
| 397 | // / <summary > | |||||
| 398 | // / Creates any entity records t hat this s ample requ ires. | |||||
| 399 | // / </summar y> | |||||
| 400 | pu blic void CreateRequ iredRecord s() | |||||
| 401 | { | |||||
| 402 | // Creat e a second user that we will r eference i n our samp le code. | |||||
| 403 | Guid use rId = Syst emUserProv ider.Retri eveSalesMa nager(_ser viceProxy) ; | |||||
| 404 | ||||||
| 405 | // Modif y email ad dress of u ser for sa mple. | |||||
| 406 | SystemUs er systemU ser = new SystemUser | |||||
| 407 | { | |||||
| 408 | Id = userId, | |||||
| 409 | Inte rnalEMailA ddress = " someone@ex ample.com" | |||||
| 410 | }; | |||||
| 411 | ||||||
| 412 | _service .Update(sy stemUser); | |||||
| 413 | } | |||||
| 414 | ||||||
| 415 | // / <summary > | |||||
| 416 | // / Deletes any entity records t hat were c reated for this samp le. | |||||
| 417 | // / <param n ame="promp t">Indicat es whether to prompt the user | |||||
| 418 | // / to delet e the reco rds create d in this sample.</p aram> | |||||
| 419 | // / </summar y> | |||||
| 420 | pu blic void DeleteRequ iredRecord s(bool pro mpt) | |||||
| 421 | { | |||||
| 422 | // The s ystem user named "Ke vin Cook" that was c reated by this sampl e will | |||||
| 423 | // conti nue to exi st on your system be cause syst em users c annot be d eleted | |||||
| 424 | // in Mi crosoft Dy namics CRM . They ca n only be enabled or disabled. | |||||
| 425 | ||||||
| 426 | bool del eteRecords = true; | |||||
| 427 | ||||||
| 428 | if (prom pt) | |||||
| 429 | { | |||||
| 430 | Cons ole.WriteL ine("\nDo you want t hese entit y records deleted? ( y/n) [y]: "); | |||||
| 431 | Stri ng answer = Console. ReadLine() ; | |||||
| 432 | ||||||
| 433 | dele teRecords = (answer. StartsWith ("y") || a nswer.Star tsWith("Y" ) || answe r == Strin g.Empty); | |||||
| 434 | } | |||||
| 435 | ||||||
| 436 | if (dele teRecords) | |||||
| 437 | { | |||||
| 438 | _ser vice.Delet e(Incident .EntityLog icalName, _incidentI d); | |||||
| 439 | _ser vice.Delet e(Opportun ity.Entity LogicalNam e, _opport unityId); | |||||
| 440 | _ser vice.Delet e(QuoteDet ail.Entity LogicalNam e, _quoteD etailId); | |||||
| 441 | _ser vice.Delet e(ProductP riceLevel. EntityLogi calName, _ productPri ceLevelId) ; | |||||
| 442 | _ser vice.Delet e(Product. EntityLogi calName, _ productId) ; | |||||
| 443 | _ser vice.Delet e(Quote.En tityLogica lName, _qu oteId); | |||||
| 444 | _ser vice.Delet e(PriceLev el.EntityL ogicalName , _priceLe velId); | |||||
| 445 | _ser vice.Delet e(Annotati on.EntityL ogicalName , _annotat ionId); | |||||
| 446 | _ser vice.Delet e(Contact. EntityLogi calName, _ contactId) ; | |||||
| 447 | _ser vice.Delet e(Account. EntityLogi calName, _ accountId) ; | |||||
| 448 | Cons ole.WriteL ine("Entit y record(s ) have bee n deleted. "); | |||||
| 449 | } | |||||
| 450 | } | |||||
| 451 | ||||||
| 452 | #e ndregion H ow To Samp le Code | |||||
| 453 | ||||||
| 454 | #r egion Main method | |||||
| 455 | ||||||
| 456 | // / <summary > | |||||
| 457 | // / Standard Main() me thod used by most SD K samples. | |||||
| 458 | // / </summar y> | |||||
| 459 | // / <param n ame="args" ></param> | |||||
| 460 | st atic publi c void Mai n(string[] args) | |||||
| 461 | { | |||||
| 462 | try | |||||
| 463 | { | |||||
| 464 | // O btain the target org anization' s Web addr ess and cl ient logon | |||||
| 465 | // c redentials from the user. | |||||
| 466 | Serv erConnecti on serverC onnect = n ew ServerC onnection( ); | |||||
| 467 | Serv erConnecti on.Configu ration con fig = serv erConnect. GetServerC onfigurati on(); | |||||
| 468 | ||||||
| 469 | Basi cContextEx amples app = new Bas icContextE xamples(); | |||||
| 470 | app. Run(config , true); | |||||
| 471 | } | |||||
| 472 | catch (F aultExcept ion<Micros oft.Xrm.Sd k.Organiza tionServic eFault> ex ) | |||||
| 473 | { | |||||
| 474 | Cons ole.WriteL ine("The a pplication terminate d with an error."); | |||||
| 475 | Cons ole.WriteL ine("Times tamp: {0}" , ex.Detai l.Timestam p); | |||||
| 476 | Cons ole.WriteL ine("Code: {0}", ex. Detail.Err orCode); | |||||
| 477 | Cons ole.WriteL ine("Messa ge: {0}", ex.Detail. Message); | |||||
| 478 | Cons ole.WriteL ine("Plugi n Trace: { 0}", ex.De tail.Trace Text); | |||||
| 479 | Cons ole.WriteL ine("Inner Fault: {0 }", | |||||
| 480 | null == ex .Detail.In nerFault ? "No Inner Fault" : "Has Inner Fault"); | |||||
| 481 | } | |||||
| 482 | catch (S ystem.Time outExcepti on ex) | |||||
| 483 | { | |||||
| 484 | Cons ole.WriteL ine("The a pplication terminate d with an error."); | |||||
| 485 | Cons ole.WriteL ine("Messa ge: {0}", ex.Message ); | |||||
| 486 | Cons ole.WriteL ine("Stack Trace: {0 }", ex.Sta ckTrace); | |||||
| 487 | Cons ole.WriteL ine("Inner Fault: {0 }", | |||||
| 488 | null == ex .InnerExce ption.Mess age ? "No Inner Faul t" : ex.In nerExcepti on.Message ); | |||||
| 489 | } | |||||
| 490 | catch (S ystem.Exce ption ex) | |||||
| 491 | { | |||||
| 492 | Cons ole.WriteL ine("The a pplication terminate d with an error."); | |||||
| 493 | Cons ole.WriteL ine(ex.Mes sage); | |||||
| 494 | ||||||
| 495 | // D isplay the details o f the inne r exceptio n. | |||||
| 496 | if ( ex.InnerEx ception != null) | |||||
| 497 | { | |||||
| 498 | Console.Wr iteLine(ex .InnerExce ption.Mess age); | |||||
| 499 | ||||||
| 500 | FaultExcep tion<Micro soft.Xrm.S dk.Organiz ationServi ceFault> f e = ex.Inn erExceptio n | |||||
| 501 | as Fau ltExceptio n<Microsof t.Xrm.Sdk. Organizati onServiceF ault>; | |||||
| 502 | if (fe != null) | |||||
| 503 | { | |||||
| 504 | Consol e.WriteLin e("Timesta mp: {0}", fe.Detail. Timestamp) ; | |||||
| 505 | Consol e.WriteLin e("Code: { 0}", fe.De tail.Error Code); | |||||
| 506 | Consol e.WriteLin e("Message : {0}", fe .Detail.Me ssage); | |||||
| 507 | Consol e.WriteLin e("Plugin Trace: {0} ", fe.Deta il.TraceTe xt); | |||||
| 508 | Consol e.WriteLin e("Inner F ault: {0}" , | |||||
| 509 | nu ll == fe.D etail.Inne rFault ? " No Inner F ault" : "H as Inner F ault"); | |||||
| 510 | } | |||||
| 511 | } | |||||
| 512 | } | |||||
| 513 | // Addit ional exce ptions to catch: Sec urityToken Validation Exception, ExpiredSe curityToke nException , | |||||
| 514 | // Secur ityAccessD eniedExcep tion, Mess ageSecurit yException , and Secu rityNegoti ationExcep tion. | |||||
| 515 | ||||||
| 516 | finally | |||||
| 517 | { | |||||
| 518 | Cons ole.WriteL ine("Press <Enter> t o exit."); | |||||
| 519 | Cons ole.ReadLi ne(); | |||||
| 520 | } | |||||
| 521 | } | |||||
| 522 | #e ndregion M ain method | |||||
| 523 | } | |||||
| 524 | } | |||||
| 525 | //</snippe tBasicCont extExample s> |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.