Produced by Araxis Merge on 2/1/2017 2:56:28 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\Service | SearchAndRetrieveArticles.cs | Tue Dec 20 19:51:43 2016 UTC |
| 2 | Wed Feb 1 19:56:28 2017 UTC |
| Description | Between Files 1 and 2 |
|
|---|---|---|
| Text Blocks | Lines | |
| Unchanged | 0 | 0 |
| Changed | 0 | 0 |
| Inserted | 0 | 0 |
| Removed | 1 | 592 |
| 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 SearchAndR etrieveArt icles> | |||||
| 17 | using Syst em; | |||||
| 18 | using Syst em.Linq; | |||||
| 19 | using Syst em.Service Model; | |||||
| 20 | ||||||
| 21 | // These n amespaces are found in the Mic rosoft.Xrm .Sdk.dll a ssembly | |||||
| 22 | // located in the SD K\bin fold er of the SDK downlo ad. | |||||
| 23 | using Micr osoft.Xrm. Sdk; | |||||
| 24 | using Micr osoft.Xrm. Sdk.Query; | |||||
| 25 | using Micr osoft.Xrm. Sdk.Client ; | |||||
| 26 | ||||||
| 27 | // This na mespace is found in Microsoft. Crm.Sdk.Pr oxy.dll as sembly | |||||
| 28 | // found i n the SDK\ bin folder . | |||||
| 29 | using Micr osoft.Crm. Sdk.Messag es; | |||||
| 30 | ||||||
| 31 | namespace Microsoft. Crm.Sdk.Sa mples | |||||
| 32 | { | |||||
| 33 | /// <s ummary> | |||||
| 34 | /// De monstrates how to se arch by bo dy, keywor d and titl e, and ret rieve arti cles by | |||||
| 35 | /// to pic incide nt subject and topic incident product. | |||||
| 36 | /// </ summary> | |||||
| 37 | /// <p aram name= "serverCon fig">Conta ins server connectio n informat ion.</para m> | |||||
| 38 | /// <p aram name= "promptfor Delete">Wh en True, t he user wi ll be prom pted to de lete all | |||||
| 39 | /// cr eated enti ties.</par am> | |||||
| 40 | public class Sea rchAndRetr ieveArticl es | |||||
| 41 | { | |||||
| 42 | #r egion Clas s Level Me mbers | |||||
| 43 | ||||||
| 44 | pr ivate Orga nizationSe rviceProxy _serviceP roxy; | |||||
| 45 | pr ivate Serv iceContext _context; | |||||
| 46 | ||||||
| 47 | pr ivate KbAr ticle[] _a rticles = new KbArti cle[3]; | |||||
| 48 | pr ivate Guid _subjectI d; | |||||
| 49 | pr ivate Inci dent _inci dent; | |||||
| 50 | pr ivate Acco unt _accou nt; | |||||
| 51 | pr ivate Prod uct _produ ct; | |||||
| 52 | pr ivate UoM _uom; | |||||
| 53 | pr ivate UoMS chedule _u omSchedule ; | |||||
| 54 | ||||||
| 55 | #e ndregion C lass Level Members | |||||
| 56 | ||||||
| 57 | #r egion How To Sample Code | |||||
| 58 | // / <summary > | |||||
| 59 | // / This met hod first creates sa mple artic les and pu blishes th em, then s earches | |||||
| 60 | // / for the articles b y body, ke yword and title. Fin ally, it r etrieves t he | |||||
| 61 | // / articles by top in cident sub ject and t op inciden t product. | |||||
| 62 | // / </summar y> | |||||
| 63 | // / <param n ame="serve rConfig">C ontains se rver conne ction info rmation.</ param> | |||||
| 64 | // / <param n ame="promp tforDelete ">When Tru e, the use r will be prompted t o delete a ll | |||||
| 65 | // / created entities.< /param> | |||||
| 66 | ||||||
| 67 | pu blic void Run(Server Connection .Configura tion serve rConfig, b ool prompt forDelete) | |||||
| 68 | { | |||||
| 69 | try | |||||
| 70 | { | |||||
| 71 | //<s nippetSear chAndRetri eveArticle s1> | |||||
| 72 | // C onnect to the Organi zation ser vice. | |||||
| 73 | // T he using s tatement a ssures tha t the serv ice proxy will be pr operly dis posed. | |||||
| 74 | 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 )) | |||||
| 75 | // U sing the S erviceCont ext class makes the queries ea sier | |||||
| 76 | usin g (_contex t = new Se rviceConte xt(_servic eProxy)) | |||||
| 77 | { | |||||
| 78 | // This st atement is required to enable early-boun d type sup port. | |||||
| 79 | _servicePr oxy.Enable ProxyTypes (); | |||||
| 80 | ||||||
| 81 | CreateRequ iredRecord s(); | |||||
| 82 | ||||||
| 83 | #region Se arch Knowl edge base by Body | |||||
| 84 | //<snippet SearchByBo dyKbArticl e> | |||||
| 85 | ||||||
| 86 | // Create the reques t | |||||
| 87 | SearchByBo dyKbArticl eRequest s earchByBod yRequest = | |||||
| 88 | new Se archByBody KbArticleR equest() | |||||
| 89 | { | |||||
| 90 | Subjec tId = _sub jectId, | |||||
| 91 | UseInf lection = true, // a llows for a differen t tense or | |||||
| 92 | // inf lection to be substi tuted for the search text | |||||
| 93 | Search Text = "co ntained", // will al so match o n 'contain s' | |||||
| 94 | QueryE xpression = new Quer yExpressio n() | |||||
| 95 | { | |||||
| 96 | Co lumnSet = new Column Set("artic lexml"), | |||||
| 97 | En tityName = KbArticle .EntityLog icalName | |||||
| 98 | } | |||||
| 99 | }; | |||||
| 100 | ||||||
| 101 | // Execute the reque st | |||||
| 102 | Console.Wr iteLine(" Searching for publi shed artic le with 'c ontained' in the bod y"); | |||||
| 103 | ||||||
| 104 | SearchByBo dyKbArticl eResponse seachByBod yResponse = | |||||
| 105 | (Searc hByBodyKbA rticleResp onse)_cont ext.Execut e(searchBy BodyReques t); | |||||
| 106 | ||||||
| 107 | // Check s uccess | |||||
| 108 | var retrie vedArticle Bodies = s eachByBody Response.E ntityColle ction.Enti ties | |||||
| 109 | .Selec t((entity) => ((KbAr ticle)enti ty).Articl eXml); | |||||
| 110 | ||||||
| 111 | if (retrie vedArticle Bodies.Cou nt() == 0) | |||||
| 112 | throw new Except ion("No ar ticles fou nd"); | |||||
| 113 | ||||||
| 114 | Console.Wr iteLine(" Results o f search ( article bo dies found ):"); | |||||
| 115 | foreach (v ar body in retrieved ArticleBod ies) | |||||
| 116 | Consol e.WriteLin e(body); | |||||
| 117 | //</snippe tSearchByB odyKbArtic le> | |||||
| 118 | ||||||
| 119 | #endregion | |||||
| 120 | ||||||
| 121 | #region Se arch knowl edge base by Keyword | |||||
| 122 | //<snippet SearchByKe ywordsKbAr ticle> | |||||
| 123 | ||||||
| 124 | // Create the reques t | |||||
| 125 | SearchByKe ywordsKbAr ticleReque st searchB yKeywordRe quest = | |||||
| 126 | new Se archByKeyw ordsKbArti cleRequest () | |||||
| 127 | { | |||||
| 128 | Subjec tId = _sub jectId, | |||||
| 129 | UseInf lection = true, | |||||
| 130 | Search Text = "Se arch", | |||||
| 131 | QueryE xpression = new Quer yExpressio n() | |||||
| 132 | { | |||||
| 133 | Co lumnSet = new Column Set("keywo rds"), | |||||
| 134 | En tityName = KbArticle .EntityLog icalName | |||||
| 135 | } | |||||
| 136 | }; | |||||
| 137 | ||||||
| 138 | // Execute the reque st | |||||
| 139 | Console.Wr iteLine(); | |||||
| 140 | Console.Wr iteLine(" Searching for publi shed artic le with 's earch' as a keyword" ); | |||||
| 141 | var search ByKeywordR esponse = | |||||
| 142 | (Searc hByKeyword sKbArticle Response)_ context.Ex ecute(sear chByKeywor dRequest); | |||||
| 143 | ||||||
| 144 | // Check s uccess | |||||
| 145 | var retrie vedArticle Keywords = searchByK eywordResp onse.Entit yCollectio n.Entities | |||||
| 146 | .Selec t((entity) => (KbArt icle)entit y); | |||||
| 147 | ||||||
| 148 | if (retrie vedArticle Keywords.C ount() == 0) | |||||
| 149 | throw new Except ion("No ar ticles fou nd"); | |||||
| 150 | ||||||
| 151 | Console.Wr iteLine(" Results o f search ( keywords f ound):"); | |||||
| 152 | foreach (v ar article in retrie vedArticle Keywords) | |||||
| 153 | Consol e.WriteLin e(article. KeyWords); | |||||
| 154 | //</snippe tSearchByK eywordsKbA rticle> | |||||
| 155 | ||||||
| 156 | #endregion | |||||
| 157 | ||||||
| 158 | #region Se arch knowl edge base by Title | |||||
| 159 | //<snippet SearchByTi tleKbArtic le> | |||||
| 160 | ||||||
| 161 | // create the reques t | |||||
| 162 | SearchByTi tleKbArtic leRequest searchByTi tleRequest = | |||||
| 163 | new Se archByTitl eKbArticle Request() | |||||
| 164 | { | |||||
| 165 | Subjec tId = _sub jectId, | |||||
| 166 | UseInf lection = false, | |||||
| 167 | Search Text = "co de", | |||||
| 168 | QueryE xpression = new Quer yExpressio n() | |||||
| 169 | { | |||||
| 170 | Co lumnSet = new Column Set("title "), | |||||
| 171 | En tityName = KbArticle .EntityLog icalName | |||||
| 172 | } | |||||
| 173 | }; | |||||
| 174 | ||||||
| 175 | // execute the reque st | |||||
| 176 | Console.Wr iteLine(); | |||||
| 177 | Console.Wr iteLine(" Searching for publi shed artic les with ' code' in t he title") ; | |||||
| 178 | var search ByTitleRes ponse = (S earchByTit leKbArticl eResponse) | |||||
| 179 | _conte xt.Execute (searchByT itleReques t); | |||||
| 180 | ||||||
| 181 | // check s uccess | |||||
| 182 | var retrie vedArticle s = search ByTitleRes ponse.Enti tyCollecti on.Entitie s | |||||
| 183 | .Selec t((entity) => (KbArt icle)entit y); | |||||
| 184 | Console.Wr iteLine(" Results o f search ( titles fou nd):"); | |||||
| 185 | foreach (v ar article in retrie vedArticle s) | |||||
| 186 | Consol e.WriteLin e(article. Title); | |||||
| 187 | //</snippe tSearchByT itleKbArti cle> | |||||
| 188 | ||||||
| 189 | #endregion | |||||
| 190 | ||||||
| 191 | #region Re trieve by top incide nt subject | |||||
| 192 | //<snippet RetrieveBy TopInciden tSubjectKb Article> | |||||
| 193 | ||||||
| 194 | // create the reques t | |||||
| 195 | var retrie veByTopInc identSubje ctRequest = | |||||
| 196 | new Re trieveByTo pIncidentS ubjectKbAr ticleReque st() | |||||
| 197 | { | |||||
| 198 | Subjec tId = _sub jectId | |||||
| 199 | }; | |||||
| 200 | ||||||
| 201 | // execute request | |||||
| 202 | Console.Wr iteLine(); | |||||
| 203 | Console.Wr iteLine(" Searching for the t op article s in subje ct 'Defaul t Subject' "); | |||||
| 204 | var retrie veByTopInc identSubje ctResponse = (Retrie veByTopInc identSubje ctKbArticl eResponse) | |||||
| 205 | _conte xt.Execute (retrieveB yTopIncide ntSubjectR equest); | |||||
| 206 | ||||||
| 207 | // check s uccess | |||||
| 208 | var articl es = retri eveByTopIn cidentSubj ectRespons e.EntityCo llection.E ntities.Se lect( | |||||
| 209 | (entit y) => (KbA rticle)ent ity); | |||||
| 210 | Console.Wr iteLine(" Top artic les in sub ject 'Defa ult Subjec t':"); | |||||
| 211 | foreach (v ar article in articl es) | |||||
| 212 | Consol e.WriteLin e(article. Title); | |||||
| 213 | //</snippe tRetrieveB yTopIncide ntSubjectK bArticle> | |||||
| 214 | ||||||
| 215 | #endregion | |||||
| 216 | ||||||
| 217 | #region Re trieve by top incide nt product | |||||
| 218 | //<snippet RetrieveBy TopInciden tProductKb Article> | |||||
| 219 | ||||||
| 220 | // create the reques t | |||||
| 221 | var retrie veByTopInc identProdu ctRequest = | |||||
| 222 | new Re trieveByTo pIncidentP roductKbAr ticleReque st() | |||||
| 223 | { | |||||
| 224 | Produc tId = _pro duct.Id | |||||
| 225 | }; | |||||
| 226 | ||||||
| 227 | // execute request | |||||
| 228 | Console.Wr iteLine(); | |||||
| 229 | Console.Wr iteLine(" Searching for the t op article s for prod uct 'Sampl e Product' "); | |||||
| 230 | var retrie veByTopInc identProdu ctResponse = (Retrie veByTopInc identProdu ctKbArticl eResponse) | |||||
| 231 | _conte xt.Execute (retrieveB yTopIncide ntProductR equest); | |||||
| 232 | ||||||
| 233 | // check s uccess | |||||
| 234 | articles = retrieveB yTopIncide ntProductR esponse.En tityCollec tion.Entit ies.Select ( | |||||
| 235 | (entit y) => (KbA rticle)ent ity); | |||||
| 236 | Console.Wr iteLine(" Top artic les for pr oduct 'Sam ple Produc t':"); | |||||
| 237 | foreach (v ar article in articl es) | |||||
| 238 | Consol e.WriteLin e(article. Title); | |||||
| 239 | //</snippe tRetrieveB yTopIncide ntProductK bArticle> | |||||
| 240 | ||||||
| 241 | #endregion | |||||
| 242 | ||||||
| 243 | DeleteRequ iredRecord s(promptfo rDelete); | |||||
| 244 | } | |||||
| 245 | //</ snippetSea rchAndRetr ieveArticl es1> | |||||
| 246 | } | |||||
| 247 | ||||||
| 248 | // Catch any servi ce fault e xceptions that Micro soft Dynam ics CRM th rows. | |||||
| 249 | catch (F aultExcept ion<Micros oft.Xrm.Sd k.Organiza tionServic eFault>) | |||||
| 250 | { | |||||
| 251 | // Y ou can han dle an exc eption her e or pass it back to the calli ng method. | |||||
| 252 | thro w; | |||||
| 253 | } | |||||
| 254 | } | |||||
| 255 | ||||||
| 256 | #r egion Publ ic methods | |||||
| 257 | ||||||
| 258 | // / <summary > | |||||
| 259 | // / Creates any entity records t hat this s ample requ ires. | |||||
| 260 | // / </summar y> | |||||
| 261 | pu blic void CreateRequ iredRecord s() | |||||
| 262 | { | |||||
| 263 | #region create kb articles | |||||
| 264 | ||||||
| 265 | Console. WriteLine( " Creatin g KB Artic les"); | |||||
| 266 | ||||||
| 267 | _subject Id = ( | |||||
| 268 | from subject i n _context .SubjectSe t | |||||
| 269 | wher e subject. Title == " Default Su bject" | |||||
| 270 | sele ct subject .Id | |||||
| 271 | ).Fir st(); | |||||
| 272 | ||||||
| 273 | var kbAr ticleTempl ateId = ( | |||||
| 274 | f rom articl eTemplate in _contex t.KbArticl eTemplateS et | |||||
| 275 | w here artic leTemplate .Title == "Standard KB Article " | |||||
| 276 | s elect arti cleTemplat e.Id | |||||
| 277 | ). FirstOrDef ault(); | |||||
| 278 | ||||||
| 279 | if (kbAr ticleTempl ateId != G uid.Empty) | |||||
| 280 | { | |||||
| 281 | // c reate a KB article | |||||
| 282 | _art icles[0] = new KbArt icle() | |||||
| 283 | { | |||||
| 284 | // set the article p roperties | |||||
| 285 | Title = "S earching t he knowled ge base", | |||||
| 286 | ArticleXml = @" | |||||
| 287 | <art icledata> | |||||
| 288 | <section i d='0'> | |||||
| 289 | <conte nt><![CDAT A[This is a sample a rticle abo ut searchi ng the kno wledge bas e.]]></con tent> | |||||
| 290 | </section> | |||||
| 291 | <section i d='1'> | |||||
| 292 | <conte nt><![CDAT A[Knowledg e bases co ntain info rmation us eful for v arious peo ple.]]></c ontent> | |||||
| 293 | </section> | |||||
| 294 | </ar ticledata> ", | |||||
| 295 | // use the built-in "Standard KB Article " template | |||||
| 296 | KbArticleT emplateId = new Enti tyReferenc e(KbArticl eTemplate. EntityLogi calName, | |||||
| 297 | kbArti cleTemplat eId), | |||||
| 298 | // use the default s ubject | |||||
| 299 | SubjectId = new Enti tyReferenc e(Subject. EntityLogi calName, _ subjectId) , | |||||
| 300 | KeyWords = "Searchin g Knowledg e base" | |||||
| 301 | }; | |||||
| 302 | _con text.AddOb ject(_arti cles[0]); | |||||
| 303 | ||||||
| 304 | _art icles[1] = new KbArt icle() | |||||
| 305 | { | |||||
| 306 | Title = "W hat's in a knowledge base", | |||||
| 307 | ArticleXml = @" | |||||
| 308 | <a rticledata > | |||||
| 309 | <section id='0'> | |||||
| 310 | <con tent><![CD ATA[This i s a sample article a bout what would be i n a knowle dge base.] ]></conten t> | |||||
| 311 | </sectio n> | |||||
| 312 | <section id='1'> | |||||
| 313 | <con tent><![CD ATA[This s ection con tains more informati on.]]></co ntent> | |||||
| 314 | </sectio n> | |||||
| 315 | </ articledat a>", | |||||
| 316 | KbArticleT emplateId = new Enti tyReferenc e(KbArticl eTemplate. EntityLogi calName, | |||||
| 317 | kbArtic leTemplate Id), | |||||
| 318 | SubjectId = new Enti tyReferenc e(Subject. EntityLogi calName, _ subjectId) , | |||||
| 319 | KeyWords = "Knowledg e base" | |||||
| 320 | }; | |||||
| 321 | _con text.AddOb ject(_arti cles[1]); | |||||
| 322 | ||||||
| 323 | _art icles[2] = new KbArt icle() | |||||
| 324 | { | |||||
| 325 | Title = "S earching t he knowled ge base fr om code", | |||||
| 326 | ArticleXml = @" | |||||
| 327 | <a rticledata > | |||||
| 328 | <section id='0'> | |||||
| 329 | <con tent><![CD ATA[This a rticle cov ers search ing the kn owledge ba se from co de.]]></co ntent> | |||||
| 330 | </sectio n> | |||||
| 331 | <section id='1'> | |||||
| 332 | <con tent><![CD ATA[This s ection con tains more informati on.]]></co ntent> | |||||
| 333 | </sectio n> | |||||
| 334 | </ articledat a>", | |||||
| 335 | KbArticleT emplateId = new Enti tyReferenc e(KbArticl eTemplate. EntityLogi calName, | |||||
| 336 | kbArtic leTemplate Id), | |||||
| 337 | SubjectId = new Enti tyReferenc e(Subject. EntityLogi calName, _ subjectId) , | |||||
| 338 | KeyWords = "Knowledg e base cod e" | |||||
| 339 | }; | |||||
| 340 | _con text.AddOb ject(_arti cles[2]); | |||||
| 341 | _con text.SaveC hanges(); | |||||
| 342 | } | |||||
| 343 | else | |||||
| 344 | { | |||||
| 345 | thro w new Argu mentExcept ion("Stand ard Articl e Template s are miss ing"); | |||||
| 346 | } | |||||
| 347 | #endregi on | |||||
| 348 | ||||||
| 349 | #region Submit the articles | |||||
| 350 | ||||||
| 351 | Console. WriteLine( " Submitt ing the ar ticles"); | |||||
| 352 | ||||||
| 353 | foreach (var artic le in _art icles) | |||||
| 354 | { | |||||
| 355 | _con text.Execu te(new Set StateReque st | |||||
| 356 | { | |||||
| 357 | EntityMoni ker = arti cle.ToEnti tyReferenc e(), | |||||
| 358 | State = ne w OptionSe tValue((in t)KbArticl eState.Una pproved), | |||||
| 359 | Status = n ew OptionS etValue((i nt)kbartic le_statusc ode.Unappr oved) | |||||
| 360 | }); | |||||
| 361 | } | |||||
| 362 | ||||||
| 363 | #endregi on | |||||
| 364 | ||||||
| 365 | #region Approve an d Publish the articl e | |||||
| 366 | ||||||
| 367 | Console. WriteLine( " Publish ing articl es"); | |||||
| 368 | ||||||
| 369 | foreach (var artic le in _art icles) | |||||
| 370 | { | |||||
| 371 | _con text.Execu te(new Set StateReque st | |||||
| 372 | { | |||||
| 373 | EntityMoni ker = arti cle.ToEnti tyReferenc e(), | |||||
| 374 | State = ne w OptionSe tValue((in t)KbArticl eState.Pub lished), | |||||
| 375 | Status = n ew OptionS etValue((i nt)kbartic le_statusc ode.Publis hed) | |||||
| 376 | }); | |||||
| 377 | } | |||||
| 378 | ||||||
| 379 | #endregi on | |||||
| 380 | ||||||
| 381 | #region Waiting fo r publishi ng to fini sh | |||||
| 382 | ||||||
| 383 | // Wait 20 seconds to ensure that data will be a vailable | |||||
| 384 | // Full- text index ing | |||||
| 385 | Console. WriteLine( " Waiting 20 second s to ensur e indexing has compl eted on th e new reco rds."); | |||||
| 386 | System.T hreading.T hread.Slee p(20000); | |||||
| 387 | Console. WriteLine( ); | |||||
| 388 | ||||||
| 389 | #endregi on | |||||
| 390 | ||||||
| 391 | #region Add cases to KbArtic les | |||||
| 392 | ||||||
| 393 | // Creat e UoM | |||||
| 394 | _uomSche dule = new UoMSchedu le() | |||||
| 395 | { | |||||
| 396 | Name = "Sample unit grou p", | |||||
| 397 | Base UoMName = "Sample ba se unit" | |||||
| 398 | }; | |||||
| 399 | _context .AddObject (_uomSched ule); | |||||
| 400 | _context .SaveChang es(); | |||||
| 401 | ||||||
| 402 | _uom = ( from uom i n _context .UoMSet | |||||
| 403 | where uom. Name == _u omSchedule .BaseUoMNa me | |||||
| 404 | select uom ).First(); | |||||
| 405 | ||||||
| 406 | Console. WriteLine( " Creatin g an accou nt and inc idents for the KB ar ticles"); | |||||
| 407 | var whoa mi = (WhoA mIResponse )_context. Execute(ne w WhoAmIRe quest()); | |||||
| 408 | ||||||
| 409 | _account = new Acc ount() | |||||
| 410 | { | |||||
| 411 | Name = "Coho W inery", | |||||
| 412 | }; | |||||
| 413 | _context .AddObject (_account) ; | |||||
| 414 | _context .SaveChang es(); | |||||
| 415 | ||||||
| 416 | _product = new Pro duct() | |||||
| 417 | { | |||||
| 418 | Name = "Sample Product", | |||||
| 419 | Prod uctNumber = "0", | |||||
| 420 | Prod uctStructu re = new O ptionSetVa lue(1), | |||||
| 421 | Defa ultUoMSche duleId = _ uomSchedul e.ToEntity Reference( ), | |||||
| 422 | Defa ultUoMId = _uom.ToEn tityRefere nce() | |||||
| 423 | }; | |||||
| 424 | ||||||
| 425 | _context .AddObject (_product) ; | |||||
| 426 | _context .SaveChang es(); | |||||
| 427 | ||||||
| 428 | // Publi sh Product | |||||
| 429 | SetState Request pu blishReque st = new S etStateReq uest | |||||
| 430 | { | |||||
| 431 | Enti tyMoniker = new Enti tyReferenc e(Product. EntityLogi calName, _ product.Id ), | |||||
| 432 | Stat e = new Op tionSetVal ue((int)Pr oductState .Active), | |||||
| 433 | Stat us = new O ptionSetVa lue(1) | |||||
| 434 | }; | |||||
| 435 | _context .Execute(p ublishRequ est); | |||||
| 436 | ||||||
| 437 | _inciden t = new In cident() | |||||
| 438 | { | |||||
| 439 | Titl e = "A sam ple incide nt", | |||||
| 440 | Owne rId = new EntityRefe rence(Syst emUser.Ent ityLogical Name, whoa mi.UserId) , | |||||
| 441 | KbAr ticleId = _articles[ 0].ToEntit yReference (), | |||||
| 442 | Cust omerId = _ account.To EntityRefe rence(), | |||||
| 443 | Subj ectId = ne w EntityRe ference(Su bject.Enti tyLogicalN ame, _subj ectId), | |||||
| 444 | Prod uctId = _p roduct.ToE ntityRefer ence() | |||||
| 445 | }; | |||||
| 446 | _context .AddObject (_incident ); | |||||
| 447 | _context .SaveChang es(); | |||||
| 448 | ||||||
| 449 | #endregi on | |||||
| 450 | } | |||||
| 451 | ||||||
| 452 | // / <summary > | |||||
| 453 | // / Deletes any entity records t hat were c reated for this samp le. | |||||
| 454 | // / <param n ame="promp t">Indicat es whether to prompt the user | |||||
| 455 | // / to delet e the reco rds create d in this sample.</p aram> | |||||
| 456 | // / </summar y> | |||||
| 457 | pu blic void DeleteRequ iredRecord s(bool pro mpt) | |||||
| 458 | { | |||||
| 459 | bool toB eDeleted = true; | |||||
| 460 | ||||||
| 461 | if (prom pt) | |||||
| 462 | { | |||||
| 463 | // A sk the use r if the c reated ent ities shou ld be dele ted. | |||||
| 464 | Cons ole.Write( "\nDo you want these entity re cords dele ted? (y/n) [y]: "); | |||||
| 465 | Stri ng answer = Console. ReadLine() ; | |||||
| 466 | if ( answer.Sta rtsWith("y ") || | |||||
| 467 | answer.Sta rtsWith("Y ") || | |||||
| 468 | answer == String.Emp ty) | |||||
| 469 | { | |||||
| 470 | toBeDelete d = true; | |||||
| 471 | } | |||||
| 472 | else | |||||
| 473 | { | |||||
| 474 | toBeDelete d = false; | |||||
| 475 | } | |||||
| 476 | } | |||||
| 477 | ||||||
| 478 | if (toBe Deleted) | |||||
| 479 | { | |||||
| 480 | #reg ion Delete incidents , accounts and units of measur e | |||||
| 481 | ||||||
| 482 | _ser viceProxy. Delete(Inc ident.Enti tyLogicalN ame, _inci dent.Id); | |||||
| 483 | ||||||
| 484 | _ser viceProxy. Delete(Pro duct.Entit yLogicalNa me, _produ ct.Id); | |||||
| 485 | ||||||
| 486 | _ser viceProxy. Delete(Acc ount.Entit yLogicalNa me, _accou nt.Id); | |||||
| 487 | ||||||
| 488 | _ser viceProxy. Delete(UoM Schedule.E ntityLogic alName, _u omSchedule .Id); | |||||
| 489 | ||||||
| 490 | #end region | |||||
| 491 | ||||||
| 492 | #reg ion Unpubl ish articl es | |||||
| 493 | ||||||
| 494 | fore ach (var a rticle in _articles) | |||||
| 495 | { | |||||
| 496 | _servicePr oxy.Execut e(new SetS tateReques t | |||||
| 497 | { | |||||
| 498 | Entity Moniker = article.To EntityRefe rence(), | |||||
| 499 | Status = new Opt ionSetValu e((int)kba rticle_sta tuscode.Un approved), | |||||
| 500 | State = new Opti onSetValue ((int)KbAr ticleState .Unapprove d) | |||||
| 501 | }); | |||||
| 502 | } | |||||
| 503 | ||||||
| 504 | #end region | |||||
| 505 | ||||||
| 506 | #reg ion Delete articles | |||||
| 507 | ||||||
| 508 | fore ach (var a rticle in _articles) | |||||
| 509 | _servicePr oxy.Delete (KbArticle .EntityLog icalName, article.Id ); | |||||
| 510 | ||||||
| 511 | #end region | |||||
| 512 | ||||||
| 513 | Cons ole.WriteL ine("Entit y records have been deleted.") ; | |||||
| 514 | } | |||||
| 515 | } | |||||
| 516 | #e ndregion P ublic Meth ods | |||||
| 517 | ||||||
| 518 | #e ndregion H ow To Samp le Code | |||||
| 519 | ||||||
| 520 | #r egion Main method | |||||
| 521 | ||||||
| 522 | // / <summary > | |||||
| 523 | // / Standard Main() me thod used by most SD K samples. | |||||
| 524 | // / </summar y> | |||||
| 525 | // / <param n ame="args" ></param> | |||||
| 526 | st atic publi c void Mai n(string[] args) | |||||
| 527 | { | |||||
| 528 | try | |||||
| 529 | { | |||||
| 530 | // O btain the target org anization' s Web addr ess and cl ient logon | |||||
| 531 | // c redentials from the user. | |||||
| 532 | Serv erConnecti on serverC onnect = n ew ServerC onnection( ); | |||||
| 533 | Serv erConnecti on.Configu ration con fig = serv erConnect. GetServerC onfigurati on(); | |||||
| 534 | ||||||
| 535 | Sear chAndRetri eveArticle s app = ne w SearchAn dRetrieveA rticles(); | |||||
| 536 | app. Run(config , true); | |||||
| 537 | } | |||||
| 538 | catch (F aultExcept ion<Micros oft.Xrm.Sd k.Organiza tionServic eFault> ex ) | |||||
| 539 | { | |||||
| 540 | Cons ole.WriteL ine("The a pplication terminate d with an error."); | |||||
| 541 | Cons ole.WriteL ine("Times tamp: {0}" , ex.Detai l.Timestam p); | |||||
| 542 | Cons ole.WriteL ine("Code: {0}", ex. Detail.Err orCode); | |||||
| 543 | Cons ole.WriteL ine("Messa ge: {0}", ex.Detail. Message); | |||||
| 544 | Cons ole.WriteL ine("Plugi n Trace: { 0}", ex.De tail.Trace Text); | |||||
| 545 | Cons ole.WriteL ine("Inner Fault: {0 }", | |||||
| 546 | null == ex .Detail.In nerFault ? "No Inner Fault" : "Has Inner Fault"); | |||||
| 547 | } | |||||
| 548 | catch (S ystem.Time outExcepti on ex) | |||||
| 549 | { | |||||
| 550 | Cons ole.WriteL ine("The a pplication terminate d with an error."); | |||||
| 551 | Cons ole.WriteL ine("Messa ge: {0}", ex.Message ); | |||||
| 552 | Cons ole.WriteL ine("Stack Trace: {0 }", ex.Sta ckTrace); | |||||
| 553 | Cons ole.WriteL ine("Inner Fault: {0 }", | |||||
| 554 | null == ex .InnerExce ption.Mess age ? "No Inner Faul t" : ex.In nerExcepti on.Message ); | |||||
| 555 | } | |||||
| 556 | catch (S ystem.Exce ption ex) | |||||
| 557 | { | |||||
| 558 | Cons ole.WriteL ine("The a pplication terminate d with an error."); | |||||
| 559 | Cons ole.WriteL ine(ex.Mes sage); | |||||
| 560 | ||||||
| 561 | // D isplay the details o f the inne r exceptio n. | |||||
| 562 | if ( ex.InnerEx ception != null) | |||||
| 563 | { | |||||
| 564 | Console.Wr iteLine(ex .InnerExce ption.Mess age); | |||||
| 565 | ||||||
| 566 | FaultExcep tion<Micro soft.Xrm.S dk.Organiz ationServi ceFault> f e = ex.Inn erExceptio n | |||||
| 567 | as Fau ltExceptio n<Microsof t.Xrm.Sdk. Organizati onServiceF ault>; | |||||
| 568 | if (fe != null) | |||||
| 569 | { | |||||
| 570 | Consol e.WriteLin e("Timesta mp: {0}", fe.Detail. Timestamp) ; | |||||
| 571 | Consol e.WriteLin e("Code: { 0}", fe.De tail.Error Code); | |||||
| 572 | Consol e.WriteLin e("Message : {0}", fe .Detail.Me ssage); | |||||
| 573 | Consol e.WriteLin e("Plugin Trace: {0} ", fe.Deta il.TraceTe xt); | |||||
| 574 | Consol e.WriteLin e("Inner F ault: {0}" , | |||||
| 575 | nu ll == fe.D etail.Inne rFault ? " No Inner F ault" : "H as Inner F ault"); | |||||
| 576 | } | |||||
| 577 | } | |||||
| 578 | } | |||||
| 579 | // Addit ional exce ptions to catch: Sec urityToken Validation Exception, ExpiredSe curityToke nException , | |||||
| 580 | // Secur ityAccessD eniedExcep tion, Mess ageSecurit yException , and Secu rityNegoti ationExcep tion. | |||||
| 581 | ||||||
| 582 | finally | |||||
| 583 | { | |||||
| 584 | Cons ole.WriteL ine("Press <Enter> t o exit."); | |||||
| 585 | Cons ole.ReadLi ne(); | |||||
| 586 | } | |||||
| 587 | } | |||||
| 588 | ||||||
| 589 | #e ndregion M ain method | |||||
| 590 | } | |||||
| 591 | } | |||||
| 592 | //</snippe tSearchAnd RetrieveAr ticles> |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.