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 | UserQueryAndSavedQuery.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 | 402 |
| 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 UserQueryA ndSavedQue ry> | |||||
| 17 | using Syst em; | |||||
| 18 | using Syst em.Collect ions.Gener ic; | |||||
| 19 | using Syst em.IO; | |||||
| 20 | using Syst em.Service Model; | |||||
| 21 | using Syst em.Text; | |||||
| 22 | using Syst em.Xml; | |||||
| 23 | ||||||
| 24 | // These n amespaces are found in the Mic rosoft.Xrm .Sdk.dll a ssembly | |||||
| 25 | // located in the SD K\bin fold er of the SDK downlo ad. | |||||
| 26 | using Micr osoft.Xrm. Sdk.Client ; | |||||
| 27 | ||||||
| 28 | // This na mespace is found in Microsoft. Crm.Sdk.Pr oxy.dll as sembly | |||||
| 29 | // found i n the SDK\ bin folder . | |||||
| 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 ex ecute save d and user queries b y id, and how to val idate | |||||
| 36 | /// a saved quer y. | |||||
| 37 | /// </ summary> | |||||
| 38 | /// <p aram name= "serverCon fig">Conta ins server connectio n informat ion.</para m> | |||||
| 39 | /// <p aram name= "promptfor Delete">Wh en True, t he user wi ll be prom pted to de lete all | |||||
| 40 | /// cr eated enti ties.</par am> | |||||
| 41 | public class Use rQueryAndS avedQuery | |||||
| 42 | { | |||||
| 43 | #r egion Clas s Level Me mbers | |||||
| 44 | ||||||
| 45 | pr ivate Orga nizationSe rviceProxy _serviceP roxy; | |||||
| 46 | pr ivate List <Account> _accounts = new List <Account>( ); | |||||
| 47 | pr ivate Save dQuery _sa vedQuery; | |||||
| 48 | pr ivate User Query _use rQuery; | |||||
| 49 | ||||||
| 50 | #e ndregion C lass Level Members | |||||
| 51 | ||||||
| 52 | #r egion How To Sample Code | |||||
| 53 | // / <summary > | |||||
| 54 | // / This met hod first creates a series of Accounts t o query ov er, a user query | |||||
| 55 | // / that ret rieves the names of all Accoun ts with a name of 'C oho Winery ' and | |||||
| 56 | // / a system query tha t retrieve s all Acco unt names. Then it v alidates t he system | |||||
| 57 | // / query, e xecutes th e system q uery and d isplays th e results, and final ly | |||||
| 58 | // / executes the user query and displays t he results . | |||||
| 59 | // / </summar y> | |||||
| 60 | // / <param n ame="serve rConfig">C ontains se rver conne ction info rmation.</ param> | |||||
| 61 | // / <param n ame="promp tforDelete ">When Tru e, the use r will be prompted t o delete a ll | |||||
| 62 | // / created entities.< /param> | |||||
| 63 | ||||||
| 64 | pu blic void Run(Server Connection .Configura tion serve rConfig, b ool prompt forDelete) | |||||
| 65 | { | |||||
| 66 | try | |||||
| 67 | { | |||||
| 68 | //<s nippetUser QueryAndSa vedQuery1> | |||||
| 69 | // C onnect to the Organi zation ser vice. | |||||
| 70 | // T he using s tatement a ssures tha t the serv ice proxy will be pr operly dis posed. | |||||
| 71 | usin g (_servic eProxy = n ew Organiz ationServi ceProxy(se rverConfig .Organizat ionUri, se rverConfig .HomeRealm Uri,server Config.Cre dentials, serverConf ig.DeviceC redentials )) | |||||
| 72 | { | |||||
| 73 | // This st atement is required to enable early-boun d type sup port. | |||||
| 74 | _servicePr oxy.Enable ProxyTypes (); | |||||
| 75 | ||||||
| 76 | CreateRequ iredRecord s(); | |||||
| 77 | ||||||
| 78 | #region Va lidate sav ed query | |||||
| 79 | ||||||
| 80 | // Create the reques t | |||||
| 81 | ValidateSa vedQueryRe quest vali dateReques t = new Va lidateSave dQueryRequ est() | |||||
| 82 | { | |||||
| 83 | FetchX ml = _save dQuery.Fet chXml, | |||||
| 84 | QueryT ype = _sav edQuery.Qu eryType.Va lue | |||||
| 85 | }; | |||||
| 86 | ||||||
| 87 | // Send th e request | |||||
| 88 | Console.Wr iteLine(" Validatin g Saved Qu ery"); | |||||
| 89 | try | |||||
| 90 | { | |||||
| 91 | // exe cuting the request w ill throw an excepti on if the fetch xml is invalid | |||||
| 92 | var va lidateResp onse = (Va lidateSave dQueryResp onse)_serv iceProxy.E xecute(val idateReque st); | |||||
| 93 | Consol e.WriteLin e(" Saved Query val idated suc cessfully" ); | |||||
| 94 | } | |||||
| 95 | catch (Exc eption) | |||||
| 96 | { | |||||
| 97 | Consol e.WriteLin e(" Inval id Saved Q uery"); | |||||
| 98 | throw; | |||||
| 99 | } | |||||
| 100 | ||||||
| 101 | #endregion | |||||
| 102 | ||||||
| 103 | #region Ex ecute save d query | |||||
| 104 | ||||||
| 105 | // Create the reques t | |||||
| 106 | ExecuteByI dSavedQuer yRequest e xecuteSave dQueryRequ est = new ExecuteByI dSavedQuer yRequest() | |||||
| 107 | { | |||||
| 108 | Entity Id = _save dQuery.Id | |||||
| 109 | }; | |||||
| 110 | ||||||
| 111 | // Execute the reque st | |||||
| 112 | Console.Wr iteLine(" Executing Saved Que ry"); | |||||
| 113 | ExecuteByI dSavedQuer yResponse executeSav edQueryRes ponse = | |||||
| 114 | (Execu teByIdSave dQueryResp onse)_serv iceProxy.E xecute(exe cuteSavedQ ueryReques t); | |||||
| 115 | ||||||
| 116 | // Check r esults | |||||
| 117 | if (String .IsNullOrE mpty(execu teSavedQue ryResponse .String)) | |||||
| 118 | throw new Except ion("Saved Query did not retur n any resu lts"); | |||||
| 119 | ||||||
| 120 | PrintResul ts(execute SavedQuery Response.S tring); | |||||
| 121 | #endregion | |||||
| 122 | ||||||
| 123 | #region Ex ecute user query | |||||
| 124 | ||||||
| 125 | // Create the reques t | |||||
| 126 | ExecuteByI dUserQuery Request ex ecuteUserQ uery = new ExecuteBy IdUserQuer yRequest() | |||||
| 127 | { | |||||
| 128 | Entity Id = _user Query.ToEn tityRefere nce() | |||||
| 129 | }; | |||||
| 130 | ||||||
| 131 | // Send th e request | |||||
| 132 | Console.Wr iteLine(" Executing User Quer y"); | |||||
| 133 | ExecuteByI dUserQuery Response e xecuteUser QueryRespo nse = | |||||
| 134 | (Execu teByIdUser QueryRespo nse)_servi ceProxy.Ex ecute(exec uteUserQue ry); | |||||
| 135 | if (String .IsNullOrE mpty(execu teUserQuer yResponse. String)) | |||||
| 136 | throw new Except ion("User Query did not return any resul ts"); | |||||
| 137 | ||||||
| 138 | // validat e results | |||||
| 139 | PrintResul ts(execute UserQueryR esponse.St ring); | |||||
| 140 | ||||||
| 141 | #endregion | |||||
| 142 | ||||||
| 143 | DeleteRequ iredRecord s(promptfo rDelete); | |||||
| 144 | } | |||||
| 145 | //</ snippetUse rQueryAndS avedQuery1 > | |||||
| 146 | } | |||||
| 147 | ||||||
| 148 | // Catch any servi ce fault e xceptions that Micro soft Dynam ics CRM th rows. | |||||
| 149 | catch (F aultExcept ion<Micros oft.Xrm.Sd k.Organiza tionServic eFault>) | |||||
| 150 | { | |||||
| 151 | // Y ou can han dle an exc eption her e or pass it back to the calli ng method. | |||||
| 152 | thro w; | |||||
| 153 | } | |||||
| 154 | } | |||||
| 155 | ||||||
| 156 | #r egion Publ ic methods | |||||
| 157 | ||||||
| 158 | // / <summary > | |||||
| 159 | // / Creates any entity records t hat this s ample requ ires. | |||||
| 160 | // / </summar y> | |||||
| 161 | pu blic void CreateRequ iredRecord s() | |||||
| 162 | { | |||||
| 163 | #region Create Acc ounts to q uery over | |||||
| 164 | ||||||
| 165 | Console. WriteLine( " Creatin g some sam ple accoun ts"); | |||||
| 166 | ||||||
| 167 | Account account = new Accoun t() | |||||
| 168 | { | |||||
| 169 | Name = "Coho V ineyard" | |||||
| 170 | }; | |||||
| 171 | account. Id = _serv iceProxy.C reate(acco unt); | |||||
| 172 | _account s.Add(acco unt); | |||||
| 173 | Console. WriteLine( " Creat ed Account {0}", acc ount.Name) ; | |||||
| 174 | ||||||
| 175 | account = new Acco unt() | |||||
| 176 | { | |||||
| 177 | Name = "Coho W inery" | |||||
| 178 | }; | |||||
| 179 | account. Id = _serv iceProxy.C reate(acco unt); | |||||
| 180 | _account s.Add(acco unt); | |||||
| 181 | Console. WriteLine( " Creat ed Account {0}", acc ount.Name) ; | |||||
| 182 | ||||||
| 183 | account = new Acco unt() | |||||
| 184 | { | |||||
| 185 | Name = "Coho V ineyard & Winery" | |||||
| 186 | }; | |||||
| 187 | account. Id = _serv iceProxy.C reate(acco unt); | |||||
| 188 | _account s.Add(acco unt); | |||||
| 189 | Console. WriteLine( " Creat ed Account {0}", acc ount.Name) ; | |||||
| 190 | ||||||
| 191 | #endregi on | |||||
| 192 | ||||||
| 193 | #region Create a S aved Query | |||||
| 194 | ||||||
| 195 | Console. WriteLine( " Creatin g a Saved Query that retrieves all Accou nt ids"); | |||||
| 196 | ||||||
| 197 | _savedQu ery = new SavedQuery () | |||||
| 198 | { | |||||
| 199 | Name = "Fetch all Accoun t ids", | |||||
| 200 | Retu rnedTypeCo de = Accou nt.EntityL ogicalName , | |||||
| 201 | Fetc hXml = @" | |||||
| 202 | <fetch map ping='logi cal'> | |||||
| 203 | <entit y name='ac count'> | |||||
| 204 | <a ttribute n ame='name' /> | |||||
| 205 | </enti ty> | |||||
| 206 | </fetch>", | |||||
| 207 | Quer yType = 0, | |||||
| 208 | ||||||
| 209 | }; | |||||
| 210 | _savedQu ery.Id = _ servicePro xy.Create( _savedQuer y); | |||||
| 211 | ||||||
| 212 | #endregi on | |||||
| 213 | ||||||
| 214 | #region Create a U ser Query | |||||
| 215 | ||||||
| 216 | Console. WriteLine( | |||||
| 217 | " C reating a User Query that retr ieves all Account id s for Acco unts with name 'Coho Winery'") ; | |||||
| 218 | ||||||
| 219 | _userQue ry = new U serQuery() | |||||
| 220 | { | |||||
| 221 | Name = "Fetch Coho Winer y", | |||||
| 222 | Retu rnedTypeCo de = Accou nt.EntityL ogicalName , | |||||
| 223 | Fetc hXml = @" | |||||
| 224 | <fetch map ping='logi cal'> | |||||
| 225 | <e ntity name ='account' > | |||||
| 226 | <att ribute nam e='name' / > | |||||
| 227 | <fil ter> | |||||
| 228 | <condi tion attri bute='name ' operator ='eq' valu e='Coho Wi nery' /> | |||||
| 229 | </fi lter> | |||||
| 230 | </ entity> | |||||
| 231 | </fetch>", | |||||
| 232 | Quer yType = 0 | |||||
| 233 | }; | |||||
| 234 | _userQue ry.Id = _s erviceProx y.Create(_ userQuery) ; | |||||
| 235 | ||||||
| 236 | #endregi on | |||||
| 237 | } | |||||
| 238 | ||||||
| 239 | // / <summary > | |||||
| 240 | // / Deletes any entity records t hat were c reated for this samp le. | |||||
| 241 | // / <param n ame="promp t">Indicat es whether to prompt the user | |||||
| 242 | // / to delet e the reco rds create d in this sample.</p aram> | |||||
| 243 | // / </summar y> | |||||
| 244 | pu blic void DeleteRequ iredRecord s(bool pro mpt) | |||||
| 245 | { | |||||
| 246 | bool toB eDeleted = true; | |||||
| 247 | ||||||
| 248 | if (prom pt) | |||||
| 249 | { | |||||
| 250 | // A sk the use r if the c reated ent ities shou ld be dele ted. | |||||
| 251 | Cons ole.Write( "\nDo you want these entity re cords dele ted? (y/n) [y]: "); | |||||
| 252 | Stri ng answer = Console. ReadLine() ; | |||||
| 253 | if ( answer.Sta rtsWith("y ") || | |||||
| 254 | answer.Sta rtsWith("Y ") || | |||||
| 255 | answer == String.Emp ty) | |||||
| 256 | { | |||||
| 257 | toBeDelete d = true; | |||||
| 258 | } | |||||
| 259 | else | |||||
| 260 | { | |||||
| 261 | toBeDelete d = false; | |||||
| 262 | } | |||||
| 263 | } | |||||
| 264 | ||||||
| 265 | if (toBe Deleted) | |||||
| 266 | { | |||||
| 267 | _ser viceProxy. Delete(Sav edQuery.En tityLogica lName, | |||||
| 268 | _savedQuer y.Id); | |||||
| 269 | ||||||
| 270 | _ser viceProxy. Delete(Use rQuery.Ent ityLogical Name, | |||||
| 271 | _userQuery .Id); | |||||
| 272 | ||||||
| 273 | fore ach (Accou nt a in _a ccounts) | |||||
| 274 | _servicePr oxy.Delete (Account.E ntityLogic alName, a. Id); | |||||
| 275 | ||||||
| 276 | Cons ole.WriteL ine("Entit y records have been deleted.") ; | |||||
| 277 | } | |||||
| 278 | } | |||||
| 279 | #e ndregion P ublic Meth ods | |||||
| 280 | ||||||
| 281 | #r egion Help er methods | |||||
| 282 | ||||||
| 283 | pr ivate void PrintResu lts(String response) | |||||
| 284 | { | |||||
| 285 | // Using XmlReader to format output | |||||
| 286 | StringBu ilder outp ut = new S tringBuild er(); | |||||
| 287 | using (X mlReader r eader = Xm lReader.Cr eate(new S tringReade r(response ))) | |||||
| 288 | { | |||||
| 289 | XmlW riterSetti ngs settin gs = new X mlWriterSe ttings | |||||
| 290 | { | |||||
| 291 | Indent = t rue, | |||||
| 292 | OmitXmlDec laration = true | |||||
| 293 | }; | |||||
| 294 | usin g (XmlWrit er writer = XmlWrite r.Create(o utput, set tings)) | |||||
| 295 | { | |||||
| 296 | // Parse t he file an d display each of th e nodes. | |||||
| 297 | while (rea der.Read() ) | |||||
| 298 | { | |||||
| 299 | switch (reader.N odeType) | |||||
| 300 | { | |||||
| 301 | ca se XmlNode Type.Eleme nt: | |||||
| 302 | writer.W riteStartE lement(rea der.Name); | |||||
| 303 | break; | |||||
| 304 | ca se XmlNode Type.Text: | |||||
| 305 | writer.W riteString (reader.Va lue); | |||||
| 306 | break; | |||||
| 307 | ca se XmlNode Type.XmlDe claration: | |||||
| 308 | ca se XmlNode Type.Proce ssingInstr uction: | |||||
| 309 | writer.W riteProces singInstru ction(read er.Name, r eader.Valu e); | |||||
| 310 | break; | |||||
| 311 | ca se XmlNode Type.Comme nt: | |||||
| 312 | writer.W riteCommen t(reader.V alue); | |||||
| 313 | break; | |||||
| 314 | ca se XmlNode Type.EndEl ement: | |||||
| 315 | writer.W riteFullEn dElement() ; | |||||
| 316 | break; | |||||
| 317 | } | |||||
| 318 | } | |||||
| 319 | } | |||||
| 320 | } | |||||
| 321 | ||||||
| 322 | Console. WriteLine( " Result of query:\ r\n {0}", output.ToS tring()); | |||||
| 323 | Console. WriteLine( ); | |||||
| 324 | } | |||||
| 325 | ||||||
| 326 | #e ndregion | |||||
| 327 | ||||||
| 328 | #e ndregion H ow To Samp le Code | |||||
| 329 | ||||||
| 330 | #r egion Main method | |||||
| 331 | ||||||
| 332 | // / <summary > | |||||
| 333 | // / Standard Main() me thod used by most SD K samples. | |||||
| 334 | // / </summar y> | |||||
| 335 | // / <param n ame="args" ></param> | |||||
| 336 | st atic publi c void Mai n(string[] args) | |||||
| 337 | { | |||||
| 338 | try | |||||
| 339 | { | |||||
| 340 | // O btain the target org anization' s Web addr ess and cl ient logon | |||||
| 341 | // c redentials from the user. | |||||
| 342 | Serv erConnecti on serverC onnect = n ew ServerC onnection( ); | |||||
| 343 | Serv erConnecti on.Configu ration con fig = serv erConnect. GetServerC onfigurati on(); | |||||
| 344 | ||||||
| 345 | User QueryAndSa vedQuery a pp = new U serQueryAn dSavedQuer y(); | |||||
| 346 | app. Run(config , true); | |||||
| 347 | } | |||||
| 348 | catch (F aultExcept ion<Micros oft.Xrm.Sd k.Organiza tionServic eFault> ex ) | |||||
| 349 | { | |||||
| 350 | Cons ole.WriteL ine("The a pplication terminate d with an error."); | |||||
| 351 | Cons ole.WriteL ine("Times tamp: {0}" , ex.Detai l.Timestam p); | |||||
| 352 | Cons ole.WriteL ine("Code: {0}", ex. Detail.Err orCode); | |||||
| 353 | Cons ole.WriteL ine("Messa ge: {0}", ex.Detail. Message); | |||||
| 354 | Cons ole.WriteL ine("Plugi n Trace: { 0}", ex.De tail.Trace Text); | |||||
| 355 | Cons ole.WriteL ine("Inner Fault: {0 }", | |||||
| 356 | null == ex .Detail.In nerFault ? "No Inner Fault" : "Has Inner Fault"); | |||||
| 357 | } | |||||
| 358 | catch (S ystem.Time outExcepti on ex) | |||||
| 359 | { | |||||
| 360 | Cons ole.WriteL ine("The a pplication terminate d with an error."); | |||||
| 361 | Cons ole.WriteL ine("Messa ge: {0}", ex.Message ); | |||||
| 362 | Cons ole.WriteL ine("Stack Trace: {0 }", ex.Sta ckTrace); | |||||
| 363 | Cons ole.WriteL ine("Inner Fault: {0 }", | |||||
| 364 | null == ex .InnerExce ption.Mess age ? "No Inner Faul t" : ex.In nerExcepti on.Message ); | |||||
| 365 | } | |||||
| 366 | catch (S ystem.Exce ption ex) | |||||
| 367 | { | |||||
| 368 | Cons ole.WriteL ine("The a pplication terminate d with an error."); | |||||
| 369 | Cons ole.WriteL ine(ex.Mes sage); | |||||
| 370 | ||||||
| 371 | // D isplay the details o f the inne r exceptio n. | |||||
| 372 | if ( ex.InnerEx ception != null) | |||||
| 373 | { | |||||
| 374 | Console.Wr iteLine(ex .InnerExce ption.Mess age); | |||||
| 375 | ||||||
| 376 | FaultExcep tion<Micro soft.Xrm.S dk.Organiz ationServi ceFault> f e = ex.Inn erExceptio n | |||||
| 377 | as Fau ltExceptio n<Microsof t.Xrm.Sdk. Organizati onServiceF ault>; | |||||
| 378 | if (fe != null) | |||||
| 379 | { | |||||
| 380 | Consol e.WriteLin e("Timesta mp: {0}", fe.Detail. Timestamp) ; | |||||
| 381 | Consol e.WriteLin e("Code: { 0}", fe.De tail.Error Code); | |||||
| 382 | Consol e.WriteLin e("Message : {0}", fe .Detail.Me ssage); | |||||
| 383 | Consol e.WriteLin e("Plugin Trace: {0} ", fe.Deta il.TraceTe xt); | |||||
| 384 | Consol e.WriteLin e("Inner F ault: {0}" , | |||||
| 385 | nu ll == fe.D etail.Inne rFault ? " No Inner F ault" : "H as Inner F ault"); | |||||
| 386 | } | |||||
| 387 | } | |||||
| 388 | } | |||||
| 389 | // Addit ional exce ptions to catch: Sec urityToken Validation Exception, ExpiredSe curityToke nException , | |||||
| 390 | // Secur ityAccessD eniedExcep tion, Mess ageSecurit yException , and Secu rityNegoti ationExcep tion. | |||||
| 391 | ||||||
| 392 | finally | |||||
| 393 | { | |||||
| 394 | Cons ole.WriteL ine("Press <Enter> t o exit."); | |||||
| 395 | Cons ole.ReadLi ne(); | |||||
| 396 | } | |||||
| 397 | } | |||||
| 398 | ||||||
| 399 | #e ndregion M ain method | |||||
| 400 | } | |||||
| 401 | } | |||||
| 402 | //</snippe tUserQuery AndSavedQu ery> |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.