Produced by Araxis Merge on 2/1/2017 2:56:24 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\BusinessDataModel\BusinessManagement | SerializeAndDeserialize.cs | Tue Dec 20 19:51:44 2016 UTC |
| 2 | Wed Feb 1 19:56:24 2017 UTC |
| Description | Between Files 1 and 2 |
|
|---|---|---|
| Text Blocks | Lines | |
| Unchanged | 0 | 0 |
| Changed | 0 | 0 |
| Inserted | 0 | 0 |
| Removed | 1 | 263 |
| 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 | // <snippe tSerialize AndDeseria lize> | |||||
| 17 | using Syst em; | |||||
| 18 | using Syst em.IO; | |||||
| 19 | using Syst em.Runtime .Serializa tion; | |||||
| 20 | using Syst em.Service Model; | |||||
| 21 | using Micr osoft.Xrm. Sdk; | |||||
| 22 | using Micr osoft.Xrm. Sdk.Client ; | |||||
| 23 | using Micr osoft.Xrm. Sdk.Query; | |||||
| 24 | ||||||
| 25 | namespace Microsoft. Crm.Sdk.Sa mples | |||||
| 26 | { | |||||
| 27 | /// <s ummary> | |||||
| 28 | /// Th is sample shows how to seriali ze both ea rly-bound and late-b ound entit y instance s (records ). In | |||||
| 29 | /// ad dition, it shows how to de-ser ialize fro m XML to a n early-bo und entity instance. | |||||
| 30 | /// </ summary> | |||||
| 31 | public class Ser ializeAndD eserialize | |||||
| 32 | { | |||||
| 33 | #r egion Clas s Level Me mbers | |||||
| 34 | ||||||
| 35 | pr ivate Orga nizationSe rviceProxy _serviceP roxy; | |||||
| 36 | pr ivate Guid _contactI d; | |||||
| 37 | ||||||
| 38 | #e ndregion C lass Level Members | |||||
| 39 | ||||||
| 40 | #r egion How To Sample Code | |||||
| 41 | ||||||
| 42 | // / <summary > | |||||
| 43 | // / Run this sample, w hich shows both how to seriali ze late-bo und and | |||||
| 44 | // / early-bo und entity instances to XML an d how to d e-serializ e them bac k from | |||||
| 45 | // / XML into entity in stances. | |||||
| 46 | // / </summar y> | |||||
| 47 | // / <param n ame="serve rConfig"> Contains s erver conn ection inf ormation.< /param> | |||||
| 48 | // / <param n ame="promp tToDelete" > When Tru e, the use r will be prompted t o delete a ll | |||||
| 49 | // / created entities. | |||||
| 50 | pu blic void Run(Server Connection .Configura tion serve rConfig, b ool prompt ToDelete) | |||||
| 51 | { | |||||
| 52 | using (_ servicePro xy = new O rganizatio nServicePr oxy(server Config.Org anizationU ri, server Config.Hom eRealmUri, serverConf ig.Credent ials, serv erConfig.D eviceCrede ntials)) | |||||
| 53 | { | |||||
| 54 | // T his statem ent is req uired to e nable earl y-bound ty pe support . | |||||
| 55 | _ser viceProxy. EnableProx yTypes(); | |||||
| 56 | ||||||
| 57 | Crea teRequired Records(); | |||||
| 58 | ||||||
| 59 | #reg ion Retrie ve the con tact from Microsoft CRM | |||||
| 60 | ||||||
| 61 | // C reate the column set object th at indicat es the fie lds to be retrieved. | |||||
| 62 | var columns = new Column Set( | |||||
| 63 | "contactid ", | |||||
| 64 | "firstname ", | |||||
| 65 | "lastname" , | |||||
| 66 | "jobtitle" ); | |||||
| 67 | ||||||
| 68 | // R etrieve th e contact from Micro soft CRM u sing the I D of the r ecord that was just created. | |||||
| 69 | // T he EntityL ogicalName indicates the Entit yType of t he object being retr ieved. | |||||
| 70 | var contact = (Contact)_ servicePro xy.Retriev e( | |||||
| 71 | Contact.En tityLogica lName, _co ntactId, c olumns); | |||||
| 72 | ||||||
| 73 | Cons ole.WriteL ine("The c ontact for the sampl e has been retrieved ."); | |||||
| 74 | ||||||
| 75 | #end region | |||||
| 76 | ||||||
| 77 | #reg ion Serial ize the co ntact into XML and s ave it | |||||
| 78 | ||||||
| 79 | // S erialize t he contact into XML and write it to the hard drive . | |||||
| 80 | var earlyBound Serializer = new Dat aContractS erializer( typeof(Con tact)); | |||||
| 81 | ||||||
| 82 | // C reate a un ique file name for t he XML. | |||||
| 83 | Stri ng earlybo undFile = "Contact_e arly_" + c ontact.Con tactId.Val ue.ToStrin g("B") + " .xml"; | |||||
| 84 | ||||||
| 85 | // W rite the s erialized object to a file. T he using s tatement w ill | |||||
| 86 | // e nsure that the FileS tream is d isposed of correctly . The Fil eMode | |||||
| 87 | // w ill ensure that the file is ov erwritten if it alre ady exists . | |||||
| 88 | usin g (var fil e = new Fi leStream(e arlyboundF ile, FileM ode.Create )) | |||||
| 89 | { | |||||
| 90 | // Write t he XML to disk. | |||||
| 91 | earlyBound Serializer .WriteObje ct(file, c ontact); | |||||
| 92 | } | |||||
| 93 | ||||||
| 94 | Cons ole.WriteL ine( | |||||
| 95 | "The early -bound con tact insta nce has be en seriali zed to a f ile, {0}." , | |||||
| 96 | earlybound File); | |||||
| 97 | ||||||
| 98 | // C onvert the contact t o a late-b ound entit y instance and seria lize it to disk. | |||||
| 99 | var lateboundC ontact = c ontact.ToE ntity<Enti ty>(); | |||||
| 100 | Stri ng latebou ndFile = " Contact_la te_" + lat eboundCont act.Id.ToS tring("B") + ".xml"; | |||||
| 101 | ||||||
| 102 | var lateBoundS erializer = new Data ContractSe rializer(t ypeof(Enti ty)); | |||||
| 103 | // W rite the s erialized object to a file. | |||||
| 104 | usin g (var fil e = new Fi leStream(l ateboundFi le, FileMo de.Create) ) | |||||
| 105 | { | |||||
| 106 | lateBoundS erializer. WriteObjec t(file, la teboundCon tact); | |||||
| 107 | } | |||||
| 108 | ||||||
| 109 | Cons ole.WriteL ine( | |||||
| 110 | "The late- bound cont act instan ce has bee n serializ ed to a fi le, {0}.", | |||||
| 111 | lateboundF ile); | |||||
| 112 | ||||||
| 113 | #end region | |||||
| 114 | ||||||
| 115 | #reg ion De-ser ialize the Microsoft CRM conta ct from XM L | |||||
| 116 | ||||||
| 117 | Cont act deseri alizedCont act = null ; | |||||
| 118 | usin g (var fil e = new Fi leStream(e arlyboundF ile, FileM ode.Open)) | |||||
| 119 | { | |||||
| 120 | deserializ edContact = (Contact )earlyBoun dSerialize r.ReadObje ct(file); | |||||
| 121 | Console.Wr iteLine("T he contact has been de-seriali zed: {0} { 1}", | |||||
| 122 | deseri alizedCont act.FirstN ame, deser ializedCon tact.LastN ame); | |||||
| 123 | } | |||||
| 124 | ||||||
| 125 | #end region | |||||
| 126 | ||||||
| 127 | #reg ion Update contact i n Microsof t CRM | |||||
| 128 | ||||||
| 129 | // U pdate the contact in Microsoft CRM to pr ove that t he de-seri alization worked. | |||||
| 130 | dese rializedCo ntact.JobT itle = "Pl umber"; | |||||
| 131 | _ser viceProxy. Update(des erializedC ontact); | |||||
| 132 | ||||||
| 133 | Cons ole.WriteL ine("The c ontact was updated i n Microsof t CRM."); | |||||
| 134 | ||||||
| 135 | #end region | |||||
| 136 | ||||||
| 137 | Dele teRequired Records(pr omptToDele te); | |||||
| 138 | } | |||||
| 139 | } | |||||
| 140 | ||||||
| 141 | // / <summary > | |||||
| 142 | // / Creates a contact which is r equired fo r running this sampl e. | |||||
| 143 | // / </summar y> | |||||
| 144 | pr ivate void CreateReq uiredRecor ds() | |||||
| 145 | { | |||||
| 146 | // Creat e a contac t entity r ecord that will be s erialized into XML. | |||||
| 147 | var cont actCreate = new Cont act(); | |||||
| 148 | contactC reate.Firs tName = "T homas"; | |||||
| 149 | contactC reate.Last Name = "An dersen"; | |||||
| 150 | ||||||
| 151 | // Creat e the enti ty in Micr osoft CRM and get it s ID. | |||||
| 152 | _contact Id = _serv iceProxy.C reate(cont actCreate) ; | |||||
| 153 | ||||||
| 154 | Console. WriteLine( "The conta ct for the sample ha s been cre ated."); | |||||
| 155 | } | |||||
| 156 | ||||||
| 157 | // / <summary > | |||||
| 158 | // / Deletes the contac t record t hat was us ed in the sample if the user r esponds in the | |||||
| 159 | // / affirmat ive. | |||||
| 160 | // / </summar y> | |||||
| 161 | // / <param n ame="promp tToDelete" >whether o r not to d elete the contact</p aram> | |||||
| 162 | pr ivate void DeleteReq uiredRecor ds(bool pr omptToDele te) | |||||
| 163 | { | |||||
| 164 | var toBe Deleted = true; | |||||
| 165 | if (prom ptToDelete ) | |||||
| 166 | { | |||||
| 167 | // A sk the use r if the c reated ent ities shou ld be dele ted. | |||||
| 168 | Cons ole.Write( "\nDo you want these entity re cords dele ted? (y/n) [y]: "); | |||||
| 169 | Stri ng answer = Console. ReadLine() ; | |||||
| 170 | if ( answer.Sta rtsWith("y ") || | |||||
| 171 | answer.Sta rtsWith("Y ") || | |||||
| 172 | answer == String.Emp ty) | |||||
| 173 | { | |||||
| 174 | toBeDelete d = true; | |||||
| 175 | } | |||||
| 176 | else | |||||
| 177 | { | |||||
| 178 | toBeDelete d = false; | |||||
| 179 | } | |||||
| 180 | } | |||||
| 181 | ||||||
| 182 | if (toBe Deleted) | |||||
| 183 | { | |||||
| 184 | _ser viceProxy. Delete(Con tact.Entit yLogicalNa me, _conta ctId); | |||||
| 185 | Cons ole.WriteL ine("The c ontact has been dele ted."); | |||||
| 186 | } | |||||
| 187 | } | |||||
| 188 | ||||||
| 189 | #e ndregion H ow To Samp le Code | |||||
| 190 | ||||||
| 191 | #r egion Main method | |||||
| 192 | ||||||
| 193 | // / <summary > | |||||
| 194 | // / Standard Main() me thod used by most SD K samples. | |||||
| 195 | // / </summar y> | |||||
| 196 | // / <param n ame="args" ></param> | |||||
| 197 | st atic publi c void Mai n(string[] args) | |||||
| 198 | { | |||||
| 199 | try | |||||
| 200 | { | |||||
| 201 | // O btain the target org anization' s Web addr ess and cl ient logon | |||||
| 202 | // c redentials from the user. | |||||
| 203 | Serv erConnecti on serverC onnect = n ew ServerC onnection( ); | |||||
| 204 | Serv erConnecti on.Configu ration con fig = serv erConnect. GetServerC onfigurati on(); | |||||
| 205 | ||||||
| 206 | var app = new SerializeA ndDeserial ize(); | |||||
| 207 | app. Run(config , true); | |||||
| 208 | } | |||||
| 209 | catch (F aultExcept ion<Micros oft.Xrm.Sd k.Organiza tionServic eFault> ex ) | |||||
| 210 | { | |||||
| 211 | Cons ole.WriteL ine("The a pplication terminate d with an error."); | |||||
| 212 | Cons ole.WriteL ine("Times tamp: {0}" , ex.Detai l.Timestam p); | |||||
| 213 | Cons ole.WriteL ine("Code: {0}", ex. Detail.Err orCode); | |||||
| 214 | Cons ole.WriteL ine("Messa ge: {0}", ex.Detail. Message); | |||||
| 215 | Cons ole.WriteL ine("Plugi n Trace: { 0}", ex.De tail.Trace Text); | |||||
| 216 | Cons ole.WriteL ine("Inner Fault: {0 }", | |||||
| 217 | null == ex .Detail.In nerFault ? "No Inner Fault" : "Has Inner Fault"); | |||||
| 218 | } | |||||
| 219 | catch (S ystem.Time outExcepti on ex) | |||||
| 220 | { | |||||
| 221 | Cons ole.WriteL ine("The a pplication terminate d with an error."); | |||||
| 222 | Cons ole.WriteL ine("Messa ge: {0}", ex.Message ); | |||||
| 223 | Cons ole.WriteL ine("Stack Trace: {0 }", ex.Sta ckTrace); | |||||
| 224 | Cons ole.WriteL ine("Inner Fault: {0 }", | |||||
| 225 | null == ex .InnerExce ption.Mess age ? "No Inner Faul t" : ex.In nerExcepti on.Message ); | |||||
| 226 | } | |||||
| 227 | catch (S ystem.Exce ption ex) | |||||
| 228 | { | |||||
| 229 | Cons ole.WriteL ine("The a pplication terminate d with an error."); | |||||
| 230 | Cons ole.WriteL ine(ex.Mes sage); | |||||
| 231 | ||||||
| 232 | // D isplay the details o f the inne r exceptio n. | |||||
| 233 | if ( ex.InnerEx ception != null) | |||||
| 234 | { | |||||
| 235 | Console.Wr iteLine(ex .InnerExce ption.Mess age); | |||||
| 236 | ||||||
| 237 | FaultExcep tion<Micro soft.Xrm.S dk.Organiz ationServi ceFault> f e = ex.Inn erExceptio n | |||||
| 238 | as Fau ltExceptio n<Microsof t.Xrm.Sdk. Organizati onServiceF ault>; | |||||
| 239 | if (fe != null) | |||||
| 240 | { | |||||
| 241 | Consol e.WriteLin e("Timesta mp: {0}", fe.Detail. Timestamp) ; | |||||
| 242 | Consol e.WriteLin e("Code: { 0}", fe.De tail.Error Code); | |||||
| 243 | Consol e.WriteLin e("Message : {0}", fe .Detail.Me ssage); | |||||
| 244 | Consol e.WriteLin e("Plugin Trace: {0} ", fe.Deta il.TraceTe xt); | |||||
| 245 | Consol e.WriteLin e("Inner F ault: {0}" , | |||||
| 246 | nu ll == fe.D etail.Inne rFault ? " No Inner F ault" : "H as Inner F ault"); | |||||
| 247 | } | |||||
| 248 | } | |||||
| 249 | } | |||||
| 250 | // Addit ional exce ptions to catch: Sec urityToken Validation Exception, ExpiredSe curityToke nException , | |||||
| 251 | // Secur ityAccessD eniedExcep tion, Mess ageSecurit yException , and Secu rityNegoti ationExcep tion. | |||||
| 252 | ||||||
| 253 | finally | |||||
| 254 | { | |||||
| 255 | Cons ole.WriteL ine("Press <Enter> t o exit."); | |||||
| 256 | Cons ole.ReadLi ne(); | |||||
| 257 | } | |||||
| 258 | } | |||||
| 259 | ||||||
| 260 | #e ndregion M ain method | |||||
| 261 | } | |||||
| 262 | } | |||||
| 263 | // </snipp etSerializ eAndDeseri alize> |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.