Produced by Araxis Merge on 2/1/2017 2:56:54 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\QuickStart\WinForms | WinCRUDOperations.cs | Tue Dec 20 19:51:42 2016 UTC |
| 2 | Wed Feb 1 19:56:54 2017 UTC |
| Description | Between Files 1 and 2 |
|
|---|---|---|
| Text Blocks | Lines | |
| Unchanged | 0 | 0 |
| Changed | 0 | 0 |
| Inserted | 0 | 0 |
| Removed | 1 | 377 |
| 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 WinFormCRU DOperation s> | |||||
| 17 | using Syst em; | |||||
| 18 | using Syst em.Collect ions.Gener ic; | |||||
| 19 | using Syst em.Windows .Forms; | |||||
| 20 | using Syst em.Configu ration; | |||||
| 21 | using Syst em.Service Model; | |||||
| 22 | ||||||
| 23 | // These n amespaces are found in the Mic rosoft.Crm .Sdk.Proxy .dll assem bly | |||||
| 24 | // located in the SD K\bin fold er of the SDK downlo ad. | |||||
| 25 | using Micr osoft.Crm. Sdk.Messag es; | |||||
| 26 | ||||||
| 27 | // These n amespaces are found in the Mic rosoft.Xrm .Sdk.dll a ssembly | |||||
| 28 | // located in the SD K\bin fold er of the SDK downlo ad. | |||||
| 29 | using Micr osoft.Xrm. Sdk; | |||||
| 30 | using Micr osoft.Xrm. Sdk.Query; | |||||
| 31 | using Micr osoft.Xrm. Tooling.Co nnector; | |||||
| 32 | ||||||
| 33 | namespace Microsoft. Crm.Sdk.Sa mples | |||||
| 34 | { | |||||
| 35 | /// <s ummary> | |||||
| 36 | /// Th is Windows Forms app lication s ample uses the CrmCo nnection c lass found in the | |||||
| 37 | /// Mi crosoft.Xr m.Client n amespace t o connect to and aut henticate with the | |||||
| 38 | /// or ganization web servi ce. | |||||
| 39 | /// | |||||
| 40 | /// Ne xt, the sa mple demon strates ho w to do ba sic entity operation s like cre ate, | |||||
| 41 | /// re trieve, up date, and delete on account en tity. | |||||
| 42 | /// | |||||
| 43 | /// Pr ior to run ning this sample you must edit the app.c onfig file to add yo ur Microso ft | |||||
| 44 | /// CR M server a nd user lo gon inform ation. Sev eral examp les are pr ovided in the file.< /summary> | |||||
| 45 | /// <r emarks> | |||||
| 46 | /// At run-time, you will be given t he option to delete all the da tabase | |||||
| 47 | /// re cords crea ted by thi s program. | |||||
| 48 | /// | |||||
| 49 | /// No helper co de from Cr mServiceHe lpers.cs i s used in this sampl e.</remark s> | |||||
| 50 | /// <s ee cref="h ttp://msdn .microsoft .com/en-us /library/g g695810.as px"/> | |||||
| 51 | ||||||
| 52 | public partial c lass WinCR UDOperatio ns : Form | |||||
| 53 | { | |||||
| 54 | #r egion Clas s Level Me mbers | |||||
| 55 | ||||||
| 56 | pr ivate Guid _accountI d; | |||||
| 57 | pr ivate IOrg anizationS ervice _or gService; | |||||
| 58 | pr ivate bool _strexit = true; | |||||
| 59 | ||||||
| 60 | #e ndregion C lass Level Members | |||||
| 61 | ||||||
| 62 | // / <summary > | |||||
| 63 | // / Initiali ze the for m controls . | |||||
| 64 | // / </summar y> | |||||
| 65 | pu blic WinCR UDOperatio ns() | |||||
| 66 | { | |||||
| 67 | Initiali zeComponen t(); | |||||
| 68 | ||||||
| 69 | // Read the server configura tions from app.confi g. | |||||
| 70 | GetServi ceConfigur ation(); | |||||
| 71 | ||||||
| 72 | //Show t he form. | |||||
| 73 | this.Sho w(); | |||||
| 74 | } | |||||
| 75 | ||||||
| 76 | #r egion Publ ic Methods | |||||
| 77 | ||||||
| 78 | // / <summary > | |||||
| 79 | // / The Run( ) method f irst conne cts to the organizat ion servic e. Afterwa rds, | |||||
| 80 | // / basic cr eate, retr ieve, upda te, and de lete entit y operatio ns are per formed. | |||||
| 81 | // / </summar y> | |||||
| 82 | // / <param n ame="conne ctionStrin g">Provide s service connection informati on.</param > | |||||
| 83 | // / <param n ame="promp tforDelete ">When Tru e, the use r will be prompted t o delete a ll | |||||
| 84 | // / created entities.< /param> | |||||
| 85 | pu blic void Run(String connectio nString, b ool prompt forDelete) | |||||
| 86 | { | |||||
| 87 | try | |||||
| 88 | { | |||||
| 89 | // E stablish a connectio n to the o rganizatio n web serv ice. | |||||
| 90 | Prin t("Connect ing to the server .. ."); | |||||
| 91 | ||||||
| 92 | // C onnect to the CRM we b service using a co nnection s tring. | |||||
| 93 | CrmS erviceClie nt conn = new Xrm.To oling.Conn ector.CrmS erviceClie nt(connect ionString) ; | |||||
| 94 | ||||||
| 95 | // C ast the pr oxy client to the IO rganizatio nService i nterface. | |||||
| 96 | _org Service = (IOrganiza tionServic e)conn.Org anizationW ebProxyCli ent != nul l ? (IOrga nizationSe rvice)conn .Organizat ionWebProx yClient : (IOrganiza tionServic e)conn.Org anizationS erviceProx y; | |||||
| 97 | Prin t("connect ed"); | |||||
| 98 | ||||||
| 99 | // C reate any entity rec ords this sample req uires. | |||||
| 100 | Crea teRequired Records(); | |||||
| 101 | ||||||
| 102 | // O btain info rmation ab out the lo gged on us er from th e web serv ice. | |||||
| 103 | Guid userid = ((WhoAmIRe sponse)_or gService.E xecute(new WhoAmIReq uest())).U serId; | |||||
| 104 | Syst emUser sys temUser = (SystemUse r)_orgServ ice.Retrie ve("system user", use rid, | |||||
| 105 | new Column Set(new st ring[] { " firstname" , "lastnam e" })); | |||||
| 106 | Prin tln("Logge d on user is " + sys temUser.Fi rstName + " " + syst emUser.Las tName + ". "); | |||||
| 107 | ||||||
| 108 | // R etrieve th e version of Microso ft Dynamic s CRM. | |||||
| 109 | Retr ieveVersio nRequest v ersionRequ est = new RetrieveVe rsionReque st(); | |||||
| 110 | Retr ieveVersio nResponse versionRes ponse = | |||||
| 111 | (RetrieveV ersionResp onse)_orgS ervice.Exe cute(versi onRequest) ; | |||||
| 112 | Prin tln("Micro soft Dynam ics CRM ve rsion " + versionRes ponse.Vers ion + ".") ; | |||||
| 113 | ||||||
| 114 | // I nstantiate an accoun t object. Note the u se of opti on set enu merations defined in OptionSet s.cs. | |||||
| 115 | // R efer to th e Entity M etadata to pic in the SDK docum entation t o determin e which at tributes m ust | |||||
| 116 | // b e set for each entit y. | |||||
| 117 | Acco unt accoun t = new Ac count { Na me = "Four th Coffee" }; | |||||
| 118 | acco unt.Accoun tCategoryC ode = new OptionSetV alue((int) AccountAcc ountCatego ryCode.Pre ferredCust omer); | |||||
| 119 | acco unt.Custom erTypeCode = new Opt ionSetValu e((int)Acc ountCustom erTypeCode .Investor) ; | |||||
| 120 | ||||||
| 121 | // C reate an a ccount rec ord named Fourth Cof fee. | |||||
| 122 | _acc ountId = _ orgService .Create(ac count); | |||||
| 123 | ||||||
| 124 | Prin tln(accoun t.LogicalN ame + " " + account. Name + " c reated, ") ; | |||||
| 125 | ||||||
| 126 | // R etrieve se veral attr ibutes fro m the new account. | |||||
| 127 | Colu mnSet cols = new Col umnSet( | |||||
| 128 | new String [] { "name ", "addres s1_postalc ode", "las tusedincam paign" }); | |||||
| 129 | ||||||
| 130 | Acco unt retrie vedAccount = (Accoun t)_orgServ ice.Retrie ve("accoun t", _accou ntId, cols ); | |||||
| 131 | Prin t("retriev ed, "); | |||||
| 132 | ||||||
| 133 | // U pdate the postal cod e attribut e. | |||||
| 134 | retr ievedAccou nt.Address 1_PostalCo de = "9805 2"; | |||||
| 135 | ||||||
| 136 | // T he address 2 postal code was s et acciden tally, so set it to null. | |||||
| 137 | retr ievedAccou nt.Address 2_PostalCo de = null; | |||||
| 138 | ||||||
| 139 | // S hows use o f a Money value. | |||||
| 140 | retr ievedAccou nt.Revenue = new Mon ey(5000000 ); | |||||
| 141 | ||||||
| 142 | // S hows use o f a Boolea n value. | |||||
| 143 | retr ievedAccou nt.CreditO nHold = fa lse; | |||||
| 144 | ||||||
| 145 | // U pdate the account re cord. | |||||
| 146 | _org Service.Up date(retri evedAccoun t); | |||||
| 147 | Prin t("and upd ated."); | |||||
| 148 | ||||||
| 149 | // D elete any entity rec ords this sample cre ated. | |||||
| 150 | Dele teRequired Records(pr omptforDel ete); | |||||
| 151 | } | |||||
| 152 | ||||||
| 153 | // Catch any servi ce fault e xceptions that Micro soft Dynam ics CRM th rows. | |||||
| 154 | catch (F aultExcept ion<Micros oft.Xrm.Sd k.Organiza tionServic eFault>) | |||||
| 155 | { | |||||
| 156 | // Y ou can han dle an exc eption her e or pass it back to the calli ng method. | |||||
| 157 | thro w; | |||||
| 158 | } | |||||
| 159 | } | |||||
| 160 | ||||||
| 161 | // / <summary > | |||||
| 162 | // / Creates any entity records t his sample requires. | |||||
| 163 | // / </summar y> | |||||
| 164 | pu blic void CreateRequ iredRecord s() | |||||
| 165 | { | |||||
| 166 | // For t his sample , all requ ired entit ies are cr eated in t he Run() m ethod. | |||||
| 167 | } | |||||
| 168 | ||||||
| 169 | // / <summary > | |||||
| 170 | // / Deletes any entity records t hat were c reated for this samp le. | |||||
| 171 | // / <param n ame="promp t">Indicat es whether to prompt the user | |||||
| 172 | // / to delet e the reco rds create d in this sample.</p aram> | |||||
| 173 | // / </summar y> | |||||
| 174 | pu blic void DeleteRequ iredRecord s(bool pro mpt) | |||||
| 175 | { | |||||
| 176 | bool del eteRecords = true; | |||||
| 177 | ||||||
| 178 | if (prom pt) | |||||
| 179 | { | |||||
| 180 | Prin tln(""); | |||||
| 181 | Dial ogResult d ialogResul t = Messag eBox.Show( "Do you wa nt the cre ated entit y records deleted?", | |||||
| 182 | "Delete Re cords", Me ssageBoxBu ttons.YesN oCancel); | |||||
| 183 | if ( dialogResu lt == Dial ogResult.Y es || dial ogResult = = DialogRe sult.Cance l) | |||||
| 184 | { | |||||
| 185 | deleteReco rds = true ; | |||||
| 186 | } | |||||
| 187 | else | |||||
| 188 | deleteReco rds = fals e; | |||||
| 189 | } | |||||
| 190 | ||||||
| 191 | if (dele teRecords) | |||||
| 192 | { | |||||
| 193 | _org Service.De lete(Accou nt.EntityL ogicalName , _account Id); | |||||
| 194 | Prin tln("Entit y records have been deleted.") ; | |||||
| 195 | } | |||||
| 196 | } | |||||
| 197 | ||||||
| 198 | // / <summary > | |||||
| 199 | // / Displays a message string in the form with newli ne. | |||||
| 200 | // / </summar y> | |||||
| 201 | pu blic void Println(st ring _sPri ntlntext) | |||||
| 202 | { | |||||
| 203 | if (lblO utMsg.Text != string .Empty) | |||||
| 204 | lblO utMsg.Text = lblOutM sg.Text + "\n" + _sP rintlntext ; | |||||
| 205 | else | |||||
| 206 | lblO utMsg.Text = _sPrint lntext; | |||||
| 207 | ||||||
| 208 | lblOutMs g.Update() ; | |||||
| 209 | } | |||||
| 210 | ||||||
| 211 | // / <summary > | |||||
| 212 | // / Displays a message string in the form. | |||||
| 213 | // / </summar y> | |||||
| 214 | pu blic void Print(stri ng _sPrint lntext) | |||||
| 215 | { | |||||
| 216 | lblOutMs g.Text = l blOutMsg.T ext + _sPr intlntext; | |||||
| 217 | lblOutMs g.Update() ; | |||||
| 218 | } | |||||
| 219 | ||||||
| 220 | #e ndregion P ublic Meth ods | |||||
| 221 | ||||||
| 222 | #r egion Priv ate Method s | |||||
| 223 | ||||||
| 224 | // / <summary > | |||||
| 225 | // / Gets web service c onnection informatio n from the app.confi g file. | |||||
| 226 | // / If there is more t han one av ailable, p roviding a n option t o the user to select | |||||
| 227 | // / the desi red connec tion confi guration b y name. | |||||
| 228 | // / </summar y> | |||||
| 229 | // / <returns >A string containing web servi ce connect ion config uration in formation. </returns> | |||||
| 230 | pr ivate Stri ng GetServ iceConfigu ration() | |||||
| 231 | { | |||||
| 232 | // Get a vailable c onnection strings fr om app.con fig. | |||||
| 233 | int coun t = Config urationMan ager.Conne ctionStrin gs.Count; | |||||
| 234 | ||||||
| 235 | // Creat e a filter list of c onnection strings so that we h ave a list of valid | |||||
| 236 | // conne ction stri ngs for Mi crosoft Dy namics CRM only. | |||||
| 237 | List<Key ValuePair< String, St ring>> fil teredConne ctionStrin gs = | |||||
| 238 | new List<KeyVa luePair<St ring, Stri ng>>(); | |||||
| 239 | ||||||
| 240 | for (int a = 0; a < count; a ++) | |||||
| 241 | { | |||||
| 242 | if ( isValidCon nectionStr ing(Config urationMan ager.Conne ctionStrin gs[a].Conn ectionStri ng)) | |||||
| 243 | filteredCo nnectionSt rings.Add | |||||
| 244 | (new K eyValuePai r<string, string> | |||||
| 245 | (C onfigurati onManager. Connection Strings[a] .Name, | |||||
| 246 | Co nfiguratio nManager.C onnectionS trings[a]. Connection String)); | |||||
| 247 | } | |||||
| 248 | ||||||
| 249 | // No va lid connec tions stri ngs found. Write out an error message. | |||||
| 250 | if (filt eredConnec tionString s.Count == 0) | |||||
| 251 | { | |||||
| 252 | Prin tln("An ap p.config f ile contai ning at le ast one va lid Micros oft Dynami cs CRM " + | |||||
| 253 | "server co nnection c onfigurati on must ex ist in the run-time folder."); | |||||
| 254 | Prin tln("\nThe re are sev eral comme nted out e xample ser ver connec tion confi gurations in " + | |||||
| 255 | "the provi ded app.co nfig file. Uncomment one or mo re of them , modify t he configu ration acc ording " + | |||||
| 256 | "to your M icrosoft D ynamics CR M installa tion, and then re-ru n the samp le."); | |||||
| 257 | ||||||
| 258 | // D isable the Connect b utton. | |||||
| 259 | btnC onnect.Ena bled = fal se; | |||||
| 260 | retu rn null; | |||||
| 261 | } | |||||
| 262 | ||||||
| 263 | // If at least one valid con nection st ring is fo und, displ ay the lis t of valid connectio n strings. | |||||
| 264 | else | |||||
| 265 | { | |||||
| 266 | _str exit = fal se; | |||||
| 267 | for (int i = 0 ; i < filt eredConnec tionString s.Count; i ++) | |||||
| 268 | { | |||||
| 269 | cbxServerL ist.Items. Add(filter edConnecti onStrings[ i].Key); | |||||
| 270 | ||||||
| 271 | } | |||||
| 272 | cbxS erverList. SelectedIn dex = 0; | |||||
| 273 | } | |||||
| 274 | ||||||
| 275 | // Retur n a non-nu ll which i n this cas e is the f irst strin g in the l ist. | |||||
| 276 | return C onfigurati onManager. Connection Strings[0] .Connectio nString; | |||||
| 277 | } | |||||
| 278 | ||||||
| 279 | ||||||
| 280 | // / <summary > | |||||
| 281 | // / Verifies if a conn ection str ing is val id for Mic rosoft Dyn amics CRM. | |||||
| 282 | // / </summar y> | |||||
| 283 | // / <returns >True for a valid st ring, othe rwise Fals e.</return s> | |||||
| 284 | pr ivate stat ic Boolean isValidCo nnectionSt ring(Strin g connecti onString) | |||||
| 285 | { | |||||
| 286 | // At a minimum, a connectio n string m ust contai n one of t hese argum ents. | |||||
| 287 | if (conn ectionStri ng.Contain s("Url=") || | |||||
| 288 | conn ectionStri ng.Contain s("Server= ") || | |||||
| 289 | conn ectionStri ng.Contain s("Service Uri=")) | |||||
| 290 | retu rn true; | |||||
| 291 | ||||||
| 292 | return f alse; | |||||
| 293 | } | |||||
| 294 | ||||||
| 295 | ||||||
| 296 | // / <summary > | |||||
| 297 | // / Let the user choos e which co nnection s tring to u se. | |||||
| 298 | // / Gets the user sele cted web service co nnection from the a pp.config file. | |||||
| 299 | // / </summar y> | |||||
| 300 | // / <returns ></returns > | |||||
| 301 | pr ivate void btnConnec t_Click(ob ject sende r, EventAr gs e) | |||||
| 302 | { | |||||
| 303 | try | |||||
| 304 | { | |||||
| 305 | stri ng connect ionString = | |||||
| 306 | Configurat ionManager .Connectio nStrings[c bxServerLi st.Selecte dItem.ToSt ring()].Co nnectionSt ring; | |||||
| 307 | ||||||
| 308 | lblO utMsg.Text = string. Empty; | |||||
| 309 | ||||||
| 310 | if ( connectio nString != null) Run (connectio nString, t rue); | |||||
| 311 | } | |||||
| 312 | ||||||
| 313 | catch (F aultExcept ion<Micros oft.Xrm.Sd k.Organiza tionServic eFault> ex ) | |||||
| 314 | { | |||||
| 315 | Prin tln("The a pplication terminate d with an error."); | |||||
| 316 | Prin tln("Times tamp: " + ex.Detail. Timestamp) ; | |||||
| 317 | Prin tln("Code: " + ex.De tail.Error Code); | |||||
| 318 | Prin tln("Messa ge: " + ex .Detail.Me ssage); | |||||
| 319 | Prin tln("Trace : " + ex.D etail.Trac eText); | |||||
| 320 | Prin tln("Inner Fault: {0 }" + (null == ex.Det ail.InnerF ault ? "No Inner Fau lt" : "Has Inner Fau lt")); | |||||
| 321 | } | |||||
| 322 | catch (S ystem.Time outExcepti on ex) | |||||
| 323 | { | |||||
| 324 | Prin tln("The a pplication terminate d with an error."); | |||||
| 325 | Prin tln("Messa ge: " + ex .Message); | |||||
| 326 | Prin tln("Stack Trace: " + ex.Stack Trace); | |||||
| 327 | Prin tln("Inner Fault: " + (null == ex.InnerE xception.M essage ? " No Inner F ault" : ex .InnerExce ption.Mess age)); | |||||
| 328 | } | |||||
| 329 | catch (S ystem.Exce ption ex) | |||||
| 330 | { | |||||
| 331 | Prin tln("The a pplication terminate d with an error."); | |||||
| 332 | Prin tln(ex.Mes sage); | |||||
| 333 | ||||||
| 334 | // D isplay the details o f the inne r exceptio n. | |||||
| 335 | if ( ex.InnerEx ception != null) | |||||
| 336 | { | |||||
| 337 | Println(ex .InnerExce ption.Mess age); | |||||
| 338 | ||||||
| 339 | FaultExcep tion<Micro soft.Xrm.S dk.Organiz ationServi ceFault> f e = ex.Inn erExceptio n | |||||
| 340 | as Fau ltExceptio n<Microsof t.Xrm.Sdk. Organizati onServiceF ault>; | |||||
| 341 | if (fe != null) | |||||
| 342 | { | |||||
| 343 | Printl n("Timesta mp: " + fe .Detail.Ti mestamp); | |||||
| 344 | Printl n("Code: " + fe.Deta il.ErrorCo de); | |||||
| 345 | Printl n("Message : " + fe.D etail.Mess age); | |||||
| 346 | Printl n("Trace: " + fe.Det ail.TraceT ext); | |||||
| 347 | Printl n("Inner F ault: " + (null == f e.Detail.I nnerFault ? "No Inne r Fault" : "Has Inne r Fault")) ; | |||||
| 348 | } | |||||
| 349 | } | |||||
| 350 | } | |||||
| 351 | ||||||
| 352 | // Addit ional exce ptions to catch: Sec urityToken Validation Exception, ExpiredSe curityToke nException , | |||||
| 353 | // Secur ityAccessD eniedExcep tion, Mess ageSecurit yException , and Secu rityNegoti ationExcep tion. | |||||
| 354 | ||||||
| 355 | finally | |||||
| 356 | { | |||||
| 357 | Prin tln("Choos e [Exit] t o quit the applicati on."); | |||||
| 358 | } | |||||
| 359 | } | |||||
| 360 | ||||||
| 361 | ||||||
| 362 | // / <summary > | |||||
| 363 | // / Quit the applicati on. | |||||
| 364 | // / </summar y> | |||||
| 365 | pr ivate void btnExit_C lick(objec t sender, EventArgs e) | |||||
| 366 | { | |||||
| 367 | this.Hid e(); | |||||
| 368 | Dispose( ); | |||||
| 369 | } | |||||
| 370 | ||||||
| 371 | #e ndregion P rivate Met hods | |||||
| 372 | } | |||||
| 373 | } | |||||
| 374 | //</snippe tWinFormCR UDOperatio ns> | |||||
| 375 | ||||||
| 376 | ||||||
| 377 |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.