Produced by Araxis Merge on 2/1/2017 2:56:55 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\Solutions | WorkWithSolutions.cs | Tue Dec 20 19:51:43 2016 UTC |
| 2 | Wed Feb 1 19:56:55 2017 UTC |
| Description | Between Files 1 and 2 |
|
|---|---|---|
| Text Blocks | Lines | |
| Unchanged | 0 | 0 |
| Changed | 0 | 0 |
| Inserted | 0 | 0 |
| Removed | 1 | 609 |
| 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 | // ======= ========== ========== ========== ========== ========== ========== == | |||||
| 3 | // | |||||
| 4 | // This f ile is par t of the M icrosoft D ynamics CR M SDK code samples. | |||||
| 5 | // | |||||
| 6 | // Copyri ght (C) Mi crosoft Co rporation. All righ ts reserve d. | |||||
| 7 | // | |||||
| 8 | // This s ource code is intend ed only as a supplem ent to Mic rosoft | |||||
| 9 | // Develo pment Tool s and/or o n-line doc umentation . See the se other | |||||
| 10 | // materi als for de tailed inf ormation r egarding M icrosoft c ode sample s. | |||||
| 11 | // | |||||
| 12 | // THIS C ODE AND IN FORMATION ARE PROVID ED "AS IS" WITHOUT W ARRANTY OF ANY | |||||
| 13 | // KIND, EITHER EXP RESSED OR IMPLIED, I NCLUDING B UT NOT LIM ITED TO TH E | |||||
| 14 | // IMPLIE D WARRANTI ES OF MERC HANTABILIT Y AND/OR F ITNESS FOR A | |||||
| 15 | // PARTIC ULAR PURPO SE. | |||||
| 16 | // | |||||
| 17 | // ======= ========== ========== ========== ========== ========== ========== == | |||||
| 18 | //<snippet WorkWithSo lutions> | |||||
| 19 | using Syst em; | |||||
| 20 | using Syst em.Collect ions.Gener ic; | |||||
| 21 | using Syst em.Linq; | |||||
| 22 | using Syst em.Service Model; | |||||
| 23 | using Syst em.Service Model.Desc ription; | |||||
| 24 | using Syst em.IO; | |||||
| 25 | ||||||
| 26 | // These n amespaces are found in the Mic rosoft.Xrm .Sdk.dll a ssembly | |||||
| 27 | // found i n the SDK\ bin folder . | |||||
| 28 | using Micr osoft.Xrm. Sdk; | |||||
| 29 | using Micr osoft.Xrm. Sdk.Client ; | |||||
| 30 | using Micr osoft.Xrm. Sdk.Metada ta; | |||||
| 31 | using Micr osoft.Xrm. Sdk.Query; | |||||
| 32 | using Micr osoft.Xrm. Sdk.Discov ery; | |||||
| 33 | using Micr osoft.Xrm. Sdk.Messag es; | |||||
| 34 | ||||||
| 35 | // This na mespace is found in Microsoft. Crm.Sdk.Pr oxy.dll as sembly | |||||
| 36 | // found i n the SDK\ bin folder . | |||||
| 37 | using Micr osoft.Crm. Sdk.Messag es; | |||||
| 38 | ||||||
| 39 | ||||||
| 40 | namespace Microsoft. Crm.Sdk.Sa mples | |||||
| 41 | { | |||||
| 42 | ||||||
| 43 | /// <s ummary> | |||||
| 44 | /// De monstrates common ta sks perfor med using Microsoft Dynamics C RM Solutio ns | |||||
| 45 | /// </ summary> | |||||
| 46 | public class Wor kWithSolut ions | |||||
| 47 | { | |||||
| 48 | #r egion Clas s Level Me mbers | |||||
| 49 | ||||||
| 50 | // / <summary > | |||||
| 51 | // / Stores t he organiz ation serv ice proxy. | |||||
| 52 | // / </summar y> | |||||
| 53 | Or ganization ServicePro xy _servic eProxy; | |||||
| 54 | ||||||
| 55 | pr ivate Guid _crmSdkPu blisherId; | |||||
| 56 | pr ivate Syst em.String _customiza tionPrefix ; | |||||
| 57 | pr ivate Guid _solution sSampleSol utionId; | |||||
| 58 | Sy stem.Strin g ManagedS olutionLoc ation = @" C:\temp\Ma nagedSolut ionForImpo rtExample. zip"; | |||||
| 59 | St ring outpu tDir = @"C :\temp\"; | |||||
| 60 | ||||||
| 61 | ||||||
| 62 | // Specify w hich langu age code t o use in t he sample. If you ar e using a language | |||||
| 63 | // other tha n US Engli sh, you wi ll need to modify th is value a ccordingly . | |||||
| 64 | // See http: //msdn.mic rosoft.com /en-us/lib rary/0h88f ahh.aspx | |||||
| 65 | pr ivate cons t int _lan guageCode = 1033; | |||||
| 66 | ||||||
| 67 | ||||||
| 68 | #e ndregion C lass Level Members | |||||
| 69 | ||||||
| 70 | #r egion How To Sample Code | |||||
| 71 | // / <summary > | |||||
| 72 | // / Shows ho w to perfo rm the fol lowing tas ks with so lutions: | |||||
| 73 | // / - Create a Publish er | |||||
| 74 | // / - Retrie ve the Def ault Publi sher | |||||
| 75 | // / - Create a Solutio n | |||||
| 76 | // / - Retrie ve a Solut ion | |||||
| 77 | // / - Add an existing Solution C omponent | |||||
| 78 | // / - Remove a Solutio n Componen t | |||||
| 79 | // / - Export or Packag e a Soluti on | |||||
| 80 | // / - Instal l or Upgra de a solut ion | |||||
| 81 | // / - Delete a Solutio n | |||||
| 82 | // / </summar y> | |||||
| 83 | // / <param n ame="serve rConfig">C ontains se rver conne ction info rmation.</ param> | |||||
| 84 | // / <param n ame="promp tForDelete ">When Tru e, the use r will be prompted t o delete a ll | |||||
| 85 | // / created entities.< /param> | |||||
| 86 | pu blic void Run(Server Connection .Configura tion serve rConfig, b ool prompt ForDelete) | |||||
| 87 | { | |||||
| 88 | try | |||||
| 89 | { | |||||
| 90 | ||||||
| 91 | // C onnect to the Organi zation ser vice. | |||||
| 92 | // T he using s tatement a ssures tha t the serv ice proxy will be pr operly dis posed. | |||||
| 93 | 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 )) | |||||
| 94 | { | |||||
| 95 | // This st atement is required to enable early-boun d type sup port. | |||||
| 96 | _servicePr oxy.Enable ProxyTypes (); | |||||
| 97 | ||||||
| 98 | // Call th e method t o create a ny data th at this sa mple requi res. | |||||
| 99 | CreateRequ iredRecord s(); | |||||
| 100 | //<snippet WorkWithSo lutions1> | |||||
| 101 | ||||||
| 102 | ||||||
| 103 | //Define a new publi sher | |||||
| 104 | Publisher _crmSdkPub lisher = n ew Publish er | |||||
| 105 | { | |||||
| 106 | Unique Name = "sd ksamples", | |||||
| 107 | Friend lyName = " Microsoft CRM SDK Sa mples", | |||||
| 108 | Suppor tingWebsit eUrl = "ht tp://msdn. microsoft. com/en-us/ dynamics/c rm/default .aspx", | |||||
| 109 | Custom izationPre fix = "sam ple", | |||||
| 110 | EMailA ddress = " someone@mi crosoft.co m", | |||||
| 111 | Descri ption = "T his publis her was cr eated with samples f rom the Mi crosoft Dy namics CRM SDK" | |||||
| 112 | }; | |||||
| 113 | ||||||
| 114 | //Does pub lisher alr eady exist ? | |||||
| 115 | QueryExpre ssion quer ySDKSample Publisher = new Quer yExpressio n | |||||
| 116 | { | |||||
| 117 | Entity Name = Pub lisher.Ent ityLogical Name, | |||||
| 118 | Column Set = new ColumnSet( "publisher id", "cust omizationp refix"), | |||||
| 119 | Criter ia = new F ilterExpre ssion() | |||||
| 120 | }; | |||||
| 121 | ||||||
| 122 | querySDKSa mplePublis her.Criter ia.AddCond ition("uni quename", ConditionO perator.Eq ual, _crmS dkPublishe r.UniqueNa me); | |||||
| 123 | EntityColl ection que rySDKSampl ePublisher Results = _servicePr oxy.Retrie veMultiple (querySDKS amplePubli sher); | |||||
| 124 | Publisher SDKSampleP ublisherRe sults = nu ll; | |||||
| 125 | ||||||
| 126 | //If it al ready exis ts, use it | |||||
| 127 | if (queryS DKSamplePu blisherRes ults.Entit ies.Count > 0) | |||||
| 128 | { | |||||
| 129 | SDKSam plePublish erResults = (Publish er)querySD KSamplePub lisherResu lts.Entiti es[0]; | |||||
| 130 | _crmSd kPublisher Id = (Guid )SDKSample PublisherR esults.Pub lisherId; | |||||
| 131 | _custo mizationPr efix = SDK SamplePubl isherResul ts.Customi zationPref ix; | |||||
| 132 | } | |||||
| 133 | //If it do esn't exis t, create it | |||||
| 134 | if (SDKSam plePublish erResults == null) | |||||
| 135 | { | |||||
| 136 | _crmSd kPublisher Id = _serv iceProxy.C reate(_crm SdkPublish er); | |||||
| 137 | Consol e.WriteLin e(String.F ormat("Cre ated publi sher: {0}. ", _crmSdk Publisher. FriendlyNa me)); | |||||
| 138 | _custo mizationPr efix = _cr mSdkPublis her.Custom izationPre fix; | |||||
| 139 | } | |||||
| 140 | ||||||
| 141 | ||||||
| 142 | ||||||
| 143 | //</snippe tWorkWithS olutions1> | |||||
| 144 | ||||||
| 145 | //<snippet WorkWithSo lutions2> | |||||
| 146 | // Retriev e the Defa ult Publis her | |||||
| 147 | ||||||
| 148 | //The defa ult publis her has a constant G UID value; | |||||
| 149 | Guid Defau ltPublishe rId = new Guid("{d21 aab71-79e7 -11dd-8874 -00188b01e 34f}"); | |||||
| 150 | ||||||
| 151 | Publisher DefaultPub lisher = ( Publisher) _servicePr oxy.Retrie ve(Publish er.EntityL ogicalName , DefaultP ublisherId , new Colu mnSet(new string[] { "friendlyn ame" })); | |||||
| 152 | ||||||
| 153 | EntityRefe rence Defa ultPublish erReferenc e = new En tityRefere nce | |||||
| 154 | { | |||||
| 155 | Id = D efaultPubl isher.Id, | |||||
| 156 | Logica lName = Pu blisher.En tityLogica lName, | |||||
| 157 | Name = DefaultPu blisher.Fr iendlyName | |||||
| 158 | }; | |||||
| 159 | Console.Wr iteLine("R etrieved t he {0}.", DefaultPub lisherRefe rence.Name ); | |||||
| 160 | //</snippe tWorkWithS olutions2> | |||||
| 161 | ||||||
| 162 | //<snippet WorkWithSo lutions3> | |||||
| 163 | // Create a Solution | |||||
| 164 | //Define a solution | |||||
| 165 | Solution s olution = new Soluti on | |||||
| 166 | { | |||||
| 167 | Unique Name = "sa mplesoluti on", | |||||
| 168 | Friend lyName = " Sample Sol ution", | |||||
| 169 | Publis herId = ne w EntityRe ference(Pu blisher.En tityLogica lName, _cr mSdkPublis herId), | |||||
| 170 | Descri ption = "T his soluti on was cre ated by th e WorkWith Solutions sample cod e in the M icrosoft D ynamics CR M SDK samp les.", | |||||
| 171 | Versio n = "1.0" | |||||
| 172 | }; | |||||
| 173 | ||||||
| 174 | //Check wh ether it a lready exi sts | |||||
| 175 | QueryExpre ssion quer yCheckForS ampleSolut ion = new QueryExpre ssion | |||||
| 176 | { | |||||
| 177 | Entity Name = Sol ution.Enti tyLogicalN ame, | |||||
| 178 | Column Set = new ColumnSet( ), | |||||
| 179 | Criter ia = new F ilterExpre ssion() | |||||
| 180 | }; | |||||
| 181 | queryCheck ForSampleS olution.Cr iteria.Add Condition( "uniquenam e", Condit ionOperato r.Equal, s olution.Un iqueName); | |||||
| 182 | ||||||
| 183 | //Create t he solutio n if it do es not alr eady exist . | |||||
| 184 | EntityColl ection que rySampleSo lutionResu lts = _ser viceProxy. RetrieveMu ltiple(que ryCheckFor SampleSolu tion); | |||||
| 185 | Solution S ampleSolut ionResults = null; | |||||
| 186 | if (queryS ampleSolut ionResults .Entities. Count > 0) | |||||
| 187 | { | |||||
| 188 | Sample SolutionRe sults = (S olution)qu erySampleS olutionRes ults.Entit ies[0]; | |||||
| 189 | _solut ionsSample SolutionId = (Guid)S ampleSolut ionResults .SolutionI d; | |||||
| 190 | } | |||||
| 191 | if (Sample SolutionRe sults == n ull) | |||||
| 192 | { | |||||
| 193 | _solut ionsSample SolutionId = _servic eProxy.Cre ate(soluti on); | |||||
| 194 | } | |||||
| 195 | //</snippe tWorkWithS olutions3> | |||||
| 196 | ||||||
| 197 | //<snippet WorkWithSo lutions4> | |||||
| 198 | // Retriev e a soluti on | |||||
| 199 | String sol utionUniqu eName = "s amplesolut ion"; | |||||
| 200 | QueryExpre ssion quer ySampleSol ution = ne w QueryExp ression | |||||
| 201 | { | |||||
| 202 | Entity Name = Sol ution.Enti tyLogicalN ame, | |||||
| 203 | Column Set = new ColumnSet( new string [] { "publ isherid", "installed on", "vers ion", "ver sionnumber ", "friend lyname" }) , | |||||
| 204 | Criter ia = new F ilterExpre ssion() | |||||
| 205 | }; | |||||
| 206 | ||||||
| 207 | querySampl eSolution. Criteria.A ddConditio n("uniquen ame", Cond itionOpera tor.Equal, solutionU niqueName) ; | |||||
| 208 | Solution S ampleSolut ion = (Sol ution)_ser viceProxy. RetrieveMu ltiple(que rySampleSo lution).En tities[0]; | |||||
| 209 | //</snippe tWorkWithS olutions4> | |||||
| 210 | ||||||
| 211 | //<snippet WorkWithSo lutions5> | |||||
| 212 | // Add an existing S olution Co mponent | |||||
| 213 | //Add the Account en tity to th e solution | |||||
| 214 | RetrieveEn tityReques t retrieve ForAddAcco untRequest = new Ret rieveEntit yRequest() | |||||
| 215 | { | |||||
| 216 | Logica lName = Ac count.Enti tyLogicalN ame | |||||
| 217 | }; | |||||
| 218 | RetrieveEn tityRespon se retriev eForAddAcc ountRespon se = (Retr ieveEntity Response)_ servicePro xy.Execute (retrieveF orAddAccou ntRequest) ; | |||||
| 219 | AddSolutio nComponent Request ad dReq = new AddSoluti onComponen tRequest() | |||||
| 220 | { | |||||
| 221 | Compon entType = (int)compo nenttype.E ntity, | |||||
| 222 | Compon entId = (G uid)retrie veForAddAc countRespo nse.Entity Metadata.M etadataId, | |||||
| 223 | Soluti onUniqueNa me = solut ion.Unique Name | |||||
| 224 | }; | |||||
| 225 | _servicePr oxy.Execut e(addReq); | |||||
| 226 | //</snippe tWorkWithS olutions5> | |||||
| 227 | ||||||
| 228 | //<snippet WorkWithSo lutions6> | |||||
| 229 | // Remove a Solution Component | |||||
| 230 | //Remove t he Account entity fr om the sol ution | |||||
| 231 | RetrieveEn tityReques t retrieve ForRemoveA ccountRequ est = new RetrieveEn tityReques t() | |||||
| 232 | { | |||||
| 233 | Logica lName = Ac count.Enti tyLogicalN ame | |||||
| 234 | }; | |||||
| 235 | RetrieveEn tityRespon se retriev eForRemove AccountRes ponse = (R etrieveEnt ityRespons e)_service Proxy.Exec ute(retrie veForRemov eAccountRe quest); | |||||
| 236 | ||||||
| 237 | RemoveSolu tionCompon entRequest removeReq = new Rem oveSolutio nComponent Request() | |||||
| 238 | { | |||||
| 239 | Compon entId = (G uid)retrie veForRemov eAccountRe sponse.Ent ityMetadat a.Metadata Id, | |||||
| 240 | Compon entType = (int)compo nenttype.E ntity, | |||||
| 241 | Soluti onUniqueNa me = solut ion.Unique Name | |||||
| 242 | }; | |||||
| 243 | _servicePr oxy.Execut e(removeRe q); | |||||
| 244 | //</snippe tWorkWithS olutions6> | |||||
| 245 | ||||||
| 246 | //<snippet WorkWithSo lutions7> | |||||
| 247 | // Export or package a solutio n | |||||
| 248 | //Export a n a soluti on | |||||
| 249 | ||||||
| 250 | ExportSolu tionReques t exportSo lutionRequ est = new ExportSolu tionReques t(); | |||||
| 251 | exportSolu tionReques t.Managed = false; | |||||
| 252 | exportSolu tionReques t.Solution Name = sol ution.Uniq ueName; | |||||
| 253 | ||||||
| 254 | ExportSolu tionRespon se exportS olutionRes ponse = (E xportSolut ionRespons e)_service Proxy.Exec ute(export SolutionRe quest); | |||||
| 255 | ||||||
| 256 | byte[] exp ortXml = e xportSolut ionRespons e.ExportSo lutionFile ; | |||||
| 257 | string fil ename = so lution.Uni queName + ".zip"; | |||||
| 258 | File.Write AllBytes(o utputDir + filename, exportXml ); | |||||
| 259 | ||||||
| 260 | Console.Wr iteLine("S olution ex ported to {0}.", out putDir + f ilename); | |||||
| 261 | //</snippe tWorkWithS olutions7> | |||||
| 262 | ||||||
| 263 | //<snippet WorkWithSo lutions8> | |||||
| 264 | // Install or Upgrad e a Soluti on | |||||
| 265 | ||||||
| 266 | byte[] fil eBytes = F ile.ReadAl lBytes(Man agedSoluti onLocation ); | |||||
| 267 | ||||||
| 268 | ImportSolu tionReques t impSolRe q = new Im portSoluti onRequest( ) | |||||
| 269 | { | |||||
| 270 | Custom izationFil e = fileBy tes | |||||
| 271 | }; | |||||
| 272 | ||||||
| 273 | _servicePr oxy.Execut e(impSolRe q); | |||||
| 274 | ||||||
| 275 | Console.Wr iteLine("I mported So lution fro m {0}", Ma nagedSolut ionLocatio n); | |||||
| 276 | //</snippe tWorkWithS olutions8> | |||||
| 277 | ||||||
| 278 | ||||||
| 279 | //<snippet WorkWithSo lutions9> | |||||
| 280 | // Monitor import su ccess | |||||
| 281 | byte[] fil eBytesWith Monitoring = File.Re adAllBytes (ManagedSo lutionLoca tion); | |||||
| 282 | ||||||
| 283 | ImportSolu tionReques t impSolRe qWithMonit oring = ne w ImportSo lutionRequ est() | |||||
| 284 | { | |||||
| 285 | Custom izationFil e = fileBy tes, | |||||
| 286 | Import JobId = Gu id.NewGuid () | |||||
| 287 | }; | |||||
| 288 | ||||||
| 289 | _servicePr oxy.Execut e(impSolRe qWithMonit oring); | |||||
| 290 | Console.Wr iteLine("I mported So lution wit h Monitori ng from {0 }", Manage dSolutionL ocation); | |||||
| 291 | ||||||
| 292 | ImportJob job = (Imp ortJob)_se rviceProxy .Retrieve( ImportJob. EntityLogi calName, i mpSolReqWi thMonitori ng.ImportJ obId, new ColumnSet( new System .String[] { "data", "solutionn ame" })); | |||||
| 293 | ||||||
| 294 | ||||||
| 295 | System.Xml .XmlDocume nt doc = n ew System. Xml.XmlDoc ument(); | |||||
| 296 | doc.LoadXm l(job.Data ); | |||||
| 297 | ||||||
| 298 | String Imp ortedSolut ionName = doc.Select SingleNode ("//soluti onManifest /UniqueNam e").InnerT ext; | |||||
| 299 | String Sol utionImpor tResult = doc.Select SingleNode ("//soluti onManifest /result/@r esult").Va lue; | |||||
| 300 | ||||||
| 301 | Console.Wr iteLine("R eport from the Impor tJob data" ); | |||||
| 302 | Console.Wr iteLine("S olution Un ique name: {0}", Imp ortedSolut ionName); | |||||
| 303 | Console.Wr iteLine("S olution Im port Resul t: {0}", S olutionImp ortResult) ; | |||||
| 304 | Console.Wr iteLine("" ); | |||||
| 305 | ||||||
| 306 | // This co de display s the resu lts for Gl obal Optio n sets ins talled as part of a solution. | |||||
| 307 | ||||||
| 308 | System.Xml .XmlNodeLi st optionS ets = doc. SelectNode s("//optio nSets/opti onSet"); | |||||
| 309 | foreach (S ystem.Xml. XmlNode no de in opti onSets) | |||||
| 310 | { | |||||
| 311 | string OptionSet Name = nod e.Attribut es["Locali zedName"]. Value; | |||||
| 312 | string result = node.First Child.Attr ibutes["re sult"].Val ue; | |||||
| 313 | ||||||
| 314 | if (re sult == "s uccess") | |||||
| 315 | { | |||||
| 316 | Co nsole.Writ eLine("{0} result: { 1}",Option SetName, r esult); | |||||
| 317 | } | |||||
| 318 | else | |||||
| 319 | { | |||||
| 320 | st ring error Code = nod e.FirstChi ld.Attribu tes["error code"].Val ue; | |||||
| 321 | st ring error Text = nod e.FirstChi ld.Attribu tes["error text"].Val ue; | |||||
| 322 | ||||||
| 323 | Co nsole.Writ eLine("{0} result: { 1} Code: { 2} Descrip tion: {3}" ,OptionSet Name, resu lt, errorC ode, error Text); | |||||
| 324 | } | |||||
| 325 | } | |||||
| 326 | ||||||
| 327 | //</snippe tWorkWithS olutions9> | |||||
| 328 | ||||||
| 329 | //<snippet WorkWithSo lutions10> | |||||
| 330 | // Delete a solution | |||||
| 331 | ||||||
| 332 | QueryExpre ssion quer yImportedS olution = new QueryE xpression | |||||
| 333 | { | |||||
| 334 | Entity Name = Sol ution.Enti tyLogicalN ame, | |||||
| 335 | Column Set = new ColumnSet( new string [] { "solu tionid", " friendlyna me" }), | |||||
| 336 | Criter ia = new F ilterExpre ssion() | |||||
| 337 | }; | |||||
| 338 | ||||||
| 339 | ||||||
| 340 | queryImpor tedSolutio n.Criteria .AddCondit ion("uniqu ename", Co nditionOpe rator.Equa l, Importe dSolutionN ame); | |||||
| 341 | ||||||
| 342 | Solution I mportedSol ution = (S olution)_s erviceProx y.Retrieve Multiple(q ueryImport edSolution ).Entities [0]; | |||||
| 343 | ||||||
| 344 | _servicePr oxy.Delete (Solution. EntityLogi calName, ( Guid)Impor tedSolutio n.Solution Id); | |||||
| 345 | ||||||
| 346 | Console.Wr iteLine("D eleted the {0} solut ion.", Imp ortedSolut ion.Friend lyName); | |||||
| 347 | ||||||
| 348 | //</snippe tWorkWithS olutions10 > | |||||
| 349 | ||||||
| 350 | ||||||
| 351 | ||||||
| 352 | DeleteRequ iredRecord s(promptFo rDelete); | |||||
| 353 | } | |||||
| 354 | } | |||||
| 355 | ||||||
| 356 | // Catch any servi ce fault e xceptions that Micro soft Dynam ics CRM th rows. | |||||
| 357 | catch (F aultExcept ion<Micros oft.Xrm.Sd k.Organiza tionServic eFault>) | |||||
| 358 | { | |||||
| 359 | // Y ou can han dle an exc eption her e or pass it back to the calli ng method. | |||||
| 360 | thro w; | |||||
| 361 | } | |||||
| 362 | } | |||||
| 363 | ||||||
| 364 | ||||||
| 365 | ||||||
| 366 | // / <summary > | |||||
| 367 | // / This met hod create s any enti ty records that this sample re quires. | |||||
| 368 | // / </summar y> | |||||
| 369 | pu blic void CreateRequ iredRecord s() | |||||
| 370 | { | |||||
| 371 | // Creat e a manage d solution for the I nstall or upgrade a solution s ample | |||||
| 372 | ||||||
| 373 | Guid _te mpPublishe rId = new Guid(); | |||||
| 374 | System.S tring _tem pCustomiza tionPrefix = "new"; | |||||
| 375 | Guid _te mpSolution sSampleSol utionId = new Guid() ; | |||||
| 376 | System.S tring _Tem pGlobalOpt ionSetName = "_TempS ampleGloba lOptionSet Name"; | |||||
| 377 | Boolean _publisher Created = false; | |||||
| 378 | Boolean _solutionC reated = f alse; | |||||
| 379 | ||||||
| 380 | ||||||
| 381 | //Define a new pub lisher | |||||
| 382 | Publishe r _crmSdkP ublisher = new Publi sher | |||||
| 383 | { | |||||
| 384 | Uniq ueName = " sdksamples ", | |||||
| 385 | Frie ndlyName = "Microsof t CRM SDK Samples", | |||||
| 386 | Supp ortingWebs iteUrl = " http://msd n.microsof t.com/en-u s/dynamics /crm/defau lt.aspx", | |||||
| 387 | Cust omizationP refix = "s ample", | |||||
| 388 | EMai lAddress = "someone@ microsoft. com", | |||||
| 389 | Desc ription = "This publ isher was created wi th samples from the Microsoft Dynamics C RM SDK" | |||||
| 390 | }; | |||||
| 391 | ||||||
| 392 | //Does p ublisher a lready exi st? | |||||
| 393 | QueryExp ression qu erySDKSamp lePublishe r = new Qu eryExpress ion | |||||
| 394 | { | |||||
| 395 | Enti tyName = P ublisher.E ntityLogic alName, | |||||
| 396 | Colu mnSet = ne w ColumnSe t("publish erid", "cu stomizatio nprefix"), | |||||
| 397 | Crit eria = new FilterExp ression() | |||||
| 398 | }; | |||||
| 399 | ||||||
| 400 | querySDK SamplePubl isher.Crit eria.AddCo ndition("u niquename" , Conditio nOperator. Equal, _cr mSdkPublis her.Unique Name); | |||||
| 401 | EntityCo llection q uerySDKSam plePublish erResults = _service Proxy.Retr ieveMultip le(querySD KSamplePub lisher); | |||||
| 402 | Publishe r SDKSampl ePublisher Results = null; | |||||
| 403 | ||||||
| 404 | //If it already ex ists, use it | |||||
| 405 | if (quer ySDKSample PublisherR esults.Ent ities.Coun t > 0) | |||||
| 406 | { | |||||
| 407 | SDKS amplePubli sherResult s = (Publi sher)query SDKSampleP ublisherRe sults.Enti ties[0]; | |||||
| 408 | _tem pPublisher Id = (Guid )SDKSample PublisherR esults.Pub lisherId; | |||||
| 409 | _tem pCustomiza tionPrefix = SDKSamp lePublishe rResults.C ustomizati onPrefix; | |||||
| 410 | } | |||||
| 411 | //If it doesn't ex ist, creat e it | |||||
| 412 | if (SDKS amplePubli sherResult s == null) | |||||
| 413 | { | |||||
| 414 | _tem pPublisher Id = _serv iceProxy.C reate(_crm SdkPublish er); | |||||
| 415 | _tem pCustomiza tionPrefix = _crmSdk Publisher. Customizat ionPrefix; | |||||
| 416 | _pub lisherCrea ted = true ; | |||||
| 417 | } | |||||
| 418 | ||||||
| 419 | //Create a Solutio n | |||||
| 420 | //Define a solutio n | |||||
| 421 | Solution solution = new Solu tion | |||||
| 422 | { | |||||
| 423 | Uniq ueName = " samplesolu tionforImp ort", | |||||
| 424 | Frie ndlyName = "Sample S olution fo r Import", | |||||
| 425 | Publ isherId = new Entity Reference( Publisher. EntityLogi calName, _ tempPublis herId), | |||||
| 426 | Desc ription = "This solu tion was c reated by the WorkWi thSolution s sample c ode in the Microsoft Dynamics CRM SDK sa mples.", | |||||
| 427 | Vers ion = "1.0 " | |||||
| 428 | }; | |||||
| 429 | ||||||
| 430 | //Check whether it already e xists | |||||
| 431 | QueryExp ression qu erySampleS olution = new QueryE xpression | |||||
| 432 | { | |||||
| 433 | Enti tyName = S olution.En tityLogica lName, | |||||
| 434 | Colu mnSet = ne w ColumnSe t(), | |||||
| 435 | Crit eria = new FilterExp ression() | |||||
| 436 | }; | |||||
| 437 | querySam pleSolutio n.Criteria .AddCondit ion("uniqu ename", Co nditionOpe rator.Equa l, solutio n.UniqueNa me); | |||||
| 438 | ||||||
| 439 | EntityCo llection q uerySample SolutionRe sults = _s erviceProx y.Retrieve Multiple(q uerySample Solution); | |||||
| 440 | Solution SampleSol utionResul ts = null; | |||||
| 441 | if (quer ySampleSol utionResul ts.Entitie s.Count > 0) | |||||
| 442 | { | |||||
| 443 | Samp leSolution Results = (Solution) querySampl eSolutionR esults.Ent ities[0]; | |||||
| 444 | _tem pSolutions SampleSolu tionId = ( Guid)Sampl eSolutionR esults.Sol utionId; | |||||
| 445 | } | |||||
| 446 | if (Samp leSolution Results == null) | |||||
| 447 | { | |||||
| 448 | _tem pSolutions SampleSolu tionId = _ servicePro xy.Create( solution); | |||||
| 449 | _sol utionCreat ed = true; | |||||
| 450 | } | |||||
| 451 | ||||||
| 452 | // Add a solution Component | |||||
| 453 | OptionSe tMetadata optionSetM etadata = new Option SetMetadat a() | |||||
| 454 | { | |||||
| 455 | Name = _tempCu stomizatio nPrefix + _TempGloba lOptionSet Name, | |||||
| 456 | Disp layName = new Label( "Example O ption Set" , _languag eCode), | |||||
| 457 | IsGl obal = tru e, | |||||
| 458 | Opti onSetType = OptionSe tType.Pick list, | |||||
| 459 | Opti ons = | |||||
| 460 | { | |||||
| 461 | new Op tionMetada ta(new Lab el("Option A", _lang uageCode), null), | |||||
| 462 | new Op tionMetada ta(new Lab el("Option B", _lang uageCode), null ) | |||||
| 463 | } | |||||
| 464 | }; | |||||
| 465 | CreateOp tionSetReq uest creat eOptionSet Request = new Create OptionSetR equest | |||||
| 466 | { | |||||
| 467 | Opti onSet = op tionSetMet adata, | |||||
| 468 | Solu tionUnique Name = sol ution.Uniq ueName | |||||
| 469 | ||||||
| 470 | }; | |||||
| 471 | ||||||
| 472 | ||||||
| 473 | _service Proxy.Exec ute(create OptionSetR equest); | |||||
| 474 | ||||||
| 475 | //Export an a solu tion | |||||
| 476 | ||||||
| 477 | ||||||
| 478 | ExportSo lutionRequ est export SolutionRe quest = ne w ExportSo lutionRequ est(); | |||||
| 479 | exportSo lutionRequ est.Manage d = true; | |||||
| 480 | exportSo lutionRequ est.Soluti onName = s olution.Un iqueName; | |||||
| 481 | ||||||
| 482 | ExportSo lutionResp onse expor tSolutionR esponse = (ExportSol utionRespo nse)_servi ceProxy.Ex ecute(expo rtSolution Request); | |||||
| 483 | ||||||
| 484 | byte[] e xportXml = exportSol utionRespo nse.Export SolutionFi le; | |||||
| 485 | System.I O.Director y.CreateDi rectory(ou tputDir); | |||||
| 486 | File.Wri teAllBytes (ManagedSo lutionLoca tion, expo rtXml); | |||||
| 487 | ||||||
| 488 | // Delet e the solu tion and t he compone nts so it can be ins talled. | |||||
| 489 | ||||||
| 490 | DeleteOp tionSetReq uest delOp tSetReq = new Delete OptionSetR equest { N ame = (_te mpCustomiz ationPrefi x + _TempG lobalOptio nSetName). ToLower() }; | |||||
| 491 | _service Proxy.Exec ute(delOpt SetReq); | |||||
| 492 | ||||||
| 493 | if (_sol utionCreat ed) | |||||
| 494 | { | |||||
| 495 | _ser viceProxy. Delete(Sol ution.Enti tyLogicalN ame, _temp SolutionsS ampleSolut ionId); | |||||
| 496 | } | |||||
| 497 | ||||||
| 498 | if (_pub lisherCrea ted) | |||||
| 499 | { | |||||
| 500 | _ser viceProxy. Delete(Pub lisher.Ent ityLogical Name, _tem pPublisher Id); | |||||
| 501 | } | |||||
| 502 | ||||||
| 503 | Console. WriteLine( "Managed S olution cr eated and copied to {0}", Mana gedSolutio nLocation) ; | |||||
| 504 | ||||||
| 505 | } | |||||
| 506 | ||||||
| 507 | // / <summary > | |||||
| 508 | // / Deletes any entity records t hat were c reated for this samp le. | |||||
| 509 | // / <param n ame="promp t">Indicat es whether to prompt the user to delete the record s created in this sa mple.</par am> | |||||
| 510 | // / </summar y> | |||||
| 511 | pu blic void DeleteRequ iredRecord s(bool pro mpt) | |||||
| 512 | { | |||||
| 513 | bool del eteRecords = true; | |||||
| 514 | ||||||
| 515 | if (prom pt) | |||||
| 516 | { | |||||
| 517 | Cons ole.WriteL ine("\nDo you want t hese entit y records deleted? ( y/n)"); | |||||
| 518 | Stri ng answer = Console. ReadLine() ; | |||||
| 519 | ||||||
| 520 | dele teRecords = (answer. StartsWith ("y") || a nswer.Star tsWith("Y" )); | |||||
| 521 | } | |||||
| 522 | ||||||
| 523 | if (dele teRecords) | |||||
| 524 | { | |||||
| 525 | _ser viceProxy. Delete(Sol ution.Enti tyLogicalN ame, _solu tionsSampl eSolutionI d); | |||||
| 526 | _ser viceProxy. Delete(Pub lisher.Ent ityLogical Name, _crm SdkPublish erId); | |||||
| 527 | // R emove the managed so lution cre ated by th e create r equired fi elds code. | |||||
| 528 | File .Delete(Ma nagedSolut ionLocatio n); | |||||
| 529 | ||||||
| 530 | ||||||
| 531 | Cons ole.WriteL ine("Entit y records have been deleted.") ; | |||||
| 532 | } | |||||
| 533 | } | |||||
| 534 | ||||||
| 535 | #e ndregion H ow To Samp le Code | |||||
| 536 | ||||||
| 537 | #r egion Main | |||||
| 538 | // / <summary > | |||||
| 539 | // / Standard Main() me thod used by most SD K samples. | |||||
| 540 | // / </summar y> | |||||
| 541 | // / <param n ame="args" ></param> | |||||
| 542 | st atic publi c void Mai n(string[] args) | |||||
| 543 | { | |||||
| 544 | try | |||||
| 545 | { | |||||
| 546 | // O btain the target org anization' s Web addr ess and cl ient logon | |||||
| 547 | // c redentials from the user. | |||||
| 548 | Serv erConnecti on serverC onnect = n ew ServerC onnection( ); | |||||
| 549 | Serv erConnecti on.Configu ration con fig = serv erConnect. GetServerC onfigurati on(); | |||||
| 550 | ||||||
| 551 | Work WithSoluti ons app = new WorkWi thSolution s(); | |||||
| 552 | app. Run(config , true); | |||||
| 553 | } | |||||
| 554 | catch (F aultExcept ion<Micros oft.Xrm.Sd k.Organiza tionServic eFault> ex ) | |||||
| 555 | { | |||||
| 556 | Cons ole.WriteL ine("The a pplication terminate d with an error."); | |||||
| 557 | Cons ole.WriteL ine("Times tamp: {0}" , ex.Detai l.Timestam p); | |||||
| 558 | Cons ole.WriteL ine("Code: {0}", ex. Detail.Err orCode); | |||||
| 559 | Cons ole.WriteL ine("Messa ge: {0}", ex.Detail. Message); | |||||
| 560 | Cons ole.WriteL ine("Plugi n Trace: { 0}", ex.De tail.Trace Text); | |||||
| 561 | Cons ole.WriteL ine("Inner Fault: {0 }", | |||||
| 562 | null == ex .Detail.In nerFault ? "No Inner Fault" : "Has Inner Fault"); | |||||
| 563 | } | |||||
| 564 | catch (S ystem.Time outExcepti on ex) | |||||
| 565 | { | |||||
| 566 | Cons ole.WriteL ine("The a pplication terminate d with an error."); | |||||
| 567 | Cons ole.WriteL ine("Messa ge: {0}", ex.Message ); | |||||
| 568 | Cons ole.WriteL ine("Stack Trace: {0 }", ex.Sta ckTrace); | |||||
| 569 | Cons ole.WriteL ine("Inner Fault: {0 }", | |||||
| 570 | null == ex .InnerExce ption.Mess age ? "No Inner Faul t" : ex.In nerExcepti on.Message ); | |||||
| 571 | } | |||||
| 572 | catch (S ystem.Exce ption ex) | |||||
| 573 | { | |||||
| 574 | Cons ole.WriteL ine("The a pplication terminate d with an error."); | |||||
| 575 | Cons ole.WriteL ine(ex.Mes sage); | |||||
| 576 | ||||||
| 577 | // D isplay the details o f the inne r exceptio n. | |||||
| 578 | if ( ex.InnerEx ception != null) | |||||
| 579 | { | |||||
| 580 | Console.Wr iteLine(ex .InnerExce ption.Mess age); | |||||
| 581 | ||||||
| 582 | FaultExcep tion<Micro soft.Xrm.S dk.Organiz ationServi ceFault> f e = ex.Inn erExceptio n | |||||
| 583 | as Fau ltExceptio n<Microsof t.Xrm.Sdk. Organizati onServiceF ault>; | |||||
| 584 | if (fe != null) | |||||
| 585 | { | |||||
| 586 | Consol e.WriteLin e("Timesta mp: {0}", fe.Detail. Timestamp) ; | |||||
| 587 | Consol e.WriteLin e("Code: { 0}", fe.De tail.Error Code); | |||||
| 588 | Consol e.WriteLin e("Message : {0}", fe .Detail.Me ssage); | |||||
| 589 | Consol e.WriteLin e("Plugin Trace: {0} ", fe.Deta il.TraceTe xt); | |||||
| 590 | Consol e.WriteLin e("Inner F ault: {0}" , | |||||
| 591 | nu ll == fe.D etail.Inne rFault ? " No Inner F ault" : "H as Inner F ault"); | |||||
| 592 | } | |||||
| 593 | } | |||||
| 594 | } | |||||
| 595 | // Addit ional exce ptions to catch: Sec urityToken Validation Exception, ExpiredSe curityToke nException , | |||||
| 596 | // Secur ityAccessD eniedExcep tion, Mess ageSecurit yException , and Secu rityNegoti ationExcep tion. | |||||
| 597 | ||||||
| 598 | finally | |||||
| 599 | { | |||||
| 600 | Cons ole.WriteL ine("Press <Enter> t o exit."); | |||||
| 601 | Cons ole.ReadLi ne(); | |||||
| 602 | } | |||||
| 603 | ||||||
| 604 | } | |||||
| 605 | #e ndregion M ain | |||||
| 606 | ||||||
| 607 | } | |||||
| 608 | } | |||||
| 609 | //</snippe tWorkWithS olutions> |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.