Produced by Araxis Merge on 2/1/2017 2:56:34 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 | FetchAggregationAndGroupBy.cs | Tue Dec 20 19:51:46 2016 UTC |
| 2 | Wed Feb 1 19:56:34 2017 UTC |
| Description | Between Files 1 and 2 |
|
|---|---|---|
| Text Blocks | Lines | |
| Unchanged | 0 | 0 |
| Changed | 0 | 0 |
| Inserted | 0 | 0 |
| Removed | 1 | 794 |
| 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 | // This f ile is par t of the M icrosoft D ynamics CR M SDK code samples. | |||||
| 4 | // | |||||
| 5 | // Copyri ght (C) Mi crosoft Co rporation. All righ ts reserve d. | |||||
| 6 | // | |||||
| 7 | // This s ource code is intend ed only as a supplem ent to Mic rosoft | |||||
| 8 | // Develo pment Tool s and/or o n-line doc umentation . See the se other | |||||
| 9 | // materi als for de tailed inf ormation r egarding M icrosoft c ode sample s. | |||||
| 10 | // | |||||
| 11 | // THIS C ODE AND IN FORMATION ARE PROVID ED "AS IS" WITHOUT W ARRANTY OF ANY | |||||
| 12 | // KIND, EITHER EXP RESSED OR IMPLIED, I NCLUDING B UT NOT LIM ITED TO TH E | |||||
| 13 | // IMPLIE D WARRANTI ES OF MERC HANTABILIT Y AND/OR F ITNESS FOR A | |||||
| 14 | // PARTIC ULAR PURPO SE. | |||||
| 15 | // | |||||
| 16 | // ======= ========== ========== ========== ========== ========== ========== == | |||||
| 17 | //<snippet FetchAggre gationAndG roupBy> | |||||
| 18 | using Syst em; | |||||
| 19 | using Syst em.Service Model; | |||||
| 20 | using Syst em.Service Model.Desc ription; | |||||
| 21 | using Syst em.Linq; | |||||
| 22 | using Syst em.Threadi ng; | |||||
| 23 | using Syst em.Xml.Lin q; | |||||
| 24 | ||||||
| 25 | // These n amespaces are found in the Mic rosoft.Xrm .Sdk.dll a ssembly | |||||
| 26 | // found i n the SDK\ bin folder . | |||||
| 27 | using Micr osoft.Xrm. Sdk; | |||||
| 28 | using Micr osoft.Xrm. Sdk.Client ; | |||||
| 29 | using Micr osoft.Xrm. Sdk.Query; | |||||
| 30 | using Micr osoft.Xrm. Sdk.Discov ery; | |||||
| 31 | ||||||
| 32 | // This na mespace is found in Microsoft. Crm.Sdk.Pr oxy.dll as sembly | |||||
| 33 | // found i n the SDK\ bin folder . | |||||
| 34 | using Micr osoft.Crm. Sdk.Messag es; | |||||
| 35 | ||||||
| 36 | ||||||
| 37 | namespace Microsoft. Crm.Sdk.Sa mples | |||||
| 38 | { | |||||
| 39 | /// <s ummary> | |||||
| 40 | /// De monstrates how to fe tch aggreg ate record data grou ped by att ributes. | |||||
| 41 | /// If you want to run thi s sample r epeatedly, you have the option to | |||||
| 42 | /// Op tionally d elete all the record s created at the end of execut ion. | |||||
| 43 | /// </ summary> | |||||
| 44 | pu blic class FetchAggr egationAnd GroupBy | |||||
| 45 | { | |||||
| 46 | #r egion Clas s Level Me mbers | |||||
| 47 | ||||||
| 48 | // / <summary > | |||||
| 49 | // / Stores t he organiz ation serv ice proxy. | |||||
| 50 | // / </summar y> | |||||
| 51 | pr ivate Orga nizationSe rviceProxy _serviceP roxy; | |||||
| 52 | pr ivate IOrg anizationS ervice _se rvice; | |||||
| 53 | ||||||
| 54 | // Define th e IDs need ed for thi s sample. | |||||
| 55 | pu blic Guid _accountId ; | |||||
| 56 | pu blic Guid[ ] _setupOp portunitiy Ids; | |||||
| 57 | ||||||
| 58 | #e ndregion C lass Level Members | |||||
| 59 | ||||||
| 60 | #r egion How To Sample Code | |||||
| 61 | // / <summary > | |||||
| 62 | // / Create a nd configu re the org anization service pr oxy. | |||||
| 63 | // / Create a n account record. | |||||
| 64 | // / Create 3 opportuni ty records associate to the ac count reco rd. | |||||
| 65 | // / Mark all 3 opportu nity recor ds as won. | |||||
| 66 | // / Fetch ag gregate re cord data grouped by attribute s. | |||||
| 67 | // / Optional ly delete all the re cords crea ted at the end of ex ecution. | |||||
| 68 | // / </summar y> | |||||
| 69 | // / <param n ame="serve rConfig">C ontains se rver conne ction info rmation.</ param> | |||||
| 70 | // / <param n ame="promp tForDelete ">When Tru e, the use r will be prompted t o delete a ll | |||||
| 71 | // / created entities.< /param> | |||||
| 72 | pu blic void Run(Server Connection .Configura tion serve rConfig, b ool prompt ForDelete) | |||||
| 73 | { | |||||
| 74 | try | |||||
| 75 | { | |||||
| 76 | ||||||
| 77 | // C onnect to the Organi zation ser vice. | |||||
| 78 | // T he using s tatement a ssures tha t the serv ice proxy will be pr operly dis posed. | |||||
| 79 | 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 )) | |||||
| 80 | { | |||||
| 81 | // This st atement is required to enable early-boun d type sup port. | |||||
| 82 | _servicePr oxy.Enable ProxyTypes (); | |||||
| 83 | _service = (IOrganiz ationServi ce)_servic eProxy; | |||||
| 84 | ||||||
| 85 | // Call th e method t o create a ny data th at this sa mple requi res. | |||||
| 86 | CreateRequ iredRecord s(); | |||||
| 87 | ||||||
| 88 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 89 | // Fe tchXML estimate dvalue_avg Aggreg ate 1 | |||||
| 90 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 91 | //<snippet FetchAggre gationAndG roupBy1> | |||||
| 92 | // Fetch t he average of estima tedvalue f or all opp ortunities . This is the equiv alent of | |||||
| 93 | // SELECT AVG(estima tedvalue) AS estimat edvalue_av g ... in S QL. | |||||
| 94 | System.Con sole.Write Line("==== ========== ========== ======="); | |||||
| 95 | string est imatedvalu e_avg = @" | |||||
| 96 | <fetch dis tinct='fal se' mappin g='logical ' aggregat e='true'> | |||||
| 97 | <entit y name='op portunity' > | |||||
| 98 | <at tribute na me='estima tedvalue' alias='est imatedvalu e_avg' agg regate='av g' /> | |||||
| 99 | </enti ty> | |||||
| 100 | </fetch>"; | |||||
| 101 | ||||||
| 102 | EntityColl ection est imatedvalu e_avg_resu lt = _serv iceProxy.R etrieveMul tiple(new FetchExpre ssion(esti matedvalue _avg)); | |||||
| 103 | ||||||
| 104 | foreach (v ar c in es timatedval ue_avg_res ult.Entiti es) | |||||
| 105 | { | |||||
| 106 | decima l aggregat e1 = ((Mon ey)((Alias edValue)c[ "estimated value_avg" ]).Value). Value; | |||||
| 107 | System .Console.W riteLine(" Average es timated va lue: " + a ggregate1) ; | |||||
| 108 | ||||||
| 109 | } | |||||
| 110 | System.Con sole.Write Line("==== ========== ========== ======="); | |||||
| 111 | //</snippe tFetchAggr egationAnd GroupBy1> | |||||
| 112 | //<snippet FetchAggre gationAndG roupBy2> | |||||
| 113 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 114 | // Fe tchXML opportun ity_count Aggregat e 2 | |||||
| 115 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 116 | // Fetch t he count o f all oppo rtunities. This is the equiva lent of | |||||
| 117 | // SELECT COUNT(*) A S opportun ity_count ... in SQL . | |||||
| 118 | string opp ortunity_c ount = @" | |||||
| 119 | <fetch dis tinct='fal se' mappin g='logical ' aggregat e='true'> | |||||
| 120 | <entit y name='op portunity' > | |||||
| 121 | <at tribute na me='name' alias='opp ortunity_c ount' aggr egate='cou nt'/> | |||||
| 122 | </enti ty> | |||||
| 123 | </fetch>"; | |||||
| 124 | ||||||
| 125 | EntityColl ection opp ortunity_c ount_resul t = _servi ceProxy.Re trieveMult iple(new F etchExpres sion(oppor tunity_cou nt)); | |||||
| 126 | ||||||
| 127 | foreach (v ar c in op portunity_ count_resu lt.Entitie s) | |||||
| 128 | { | |||||
| 129 | Int32 aggregate2 = (Int32) ((AliasedV alue)c["op portunity_ count"]).V alue; | |||||
| 130 | System .Console.W riteLine(" Count of a ll opportu nities: " + aggregat e2); | |||||
| 131 | ||||||
| 132 | } | |||||
| 133 | System.Con sole.Write Line("==== ========== ========== ======="); | |||||
| 134 | //</snippe tFetchAggr egationAnd GroupBy2> | |||||
| 135 | //<snippet FetchAggre gationAndG roupBy3> | |||||
| 136 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 137 | // Fe tchXML opportun ity_colcou nt Aggre gate 3 | |||||
| 138 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 139 | // Fetch t he count o f all oppo rtunities. This is the equiva lent of | |||||
| 140 | // SELECT COUNT(name ) AS oppor tunity_cou nt ... in SQL. | |||||
| 141 | string opp ortunity_c olcount = @" | |||||
| 142 | <fetch dis tinct='fal se' mappin g='logical ' aggregat e='true'> | |||||
| 143 | <entit y name='op portunity' > | |||||
| 144 | <at tribute na me='name' alias='opp ortunity_c olcount' a ggregate=' countcolum n'/> | |||||
| 145 | </enti ty> | |||||
| 146 | </fetch>"; | |||||
| 147 | ||||||
| 148 | EntityColl ection opp ortunity_c olcount_re sult = _se rviceProxy .RetrieveM ultiple(ne w FetchExp ression(op portunity_ colcount)) ; | |||||
| 149 | ||||||
| 150 | foreach (v ar c in op portunity_ colcount_r esult.Enti ties) | |||||
| 151 | { | |||||
| 152 | Int32 aggregate3 = (Int32) ((AliasedV alue)c["op portunity_ colcount"] ).Value; | |||||
| 153 | System .Console.W riteLine(" Column cou nt of all opportunit ies: " + a ggregate3) ; | |||||
| 154 | ||||||
| 155 | } | |||||
| 156 | System.Con sole.Write Line("==== ========== ========== ======="); | |||||
| 157 | //</snippe tFetchAggr egationAnd GroupBy3> | |||||
| 158 | //<snippet FetchAggre gationAndG roupBy4> | |||||
| 159 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 160 | // Fe tchXML opportun ity_distco unt Aggr egate 4 | |||||
| 161 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 162 | // Fetch t he count o f distinct names for opportuni ties. Thi s is the e quivalent of | |||||
| 163 | // SELECT COUNT(DIST INCT name) AS opport unity_coun t ... in S QL. | |||||
| 164 | string opp ortunity_d istcount = @" | |||||
| 165 | <fetch dis tinct='fal se' mappin g='logical ' aggregat e='true'> | |||||
| 166 | <entit y name='op portunity' > | |||||
| 167 | <at tribute na me='name' alias='opp ortunity_d istcount' aggregate= 'countcolu mn' distin ct='true'/ > | |||||
| 168 | </enti ty> | |||||
| 169 | </fetch>"; | |||||
| 170 | ||||||
| 171 | EntityColl ection opp ortunity_d istcount_r esult = _s erviceProx y.Retrieve Multiple(n ew FetchEx pression(o pportunity _distcount )); | |||||
| 172 | ||||||
| 173 | foreach (v ar c in op portunity_ distcount_ result.Ent ities) | |||||
| 174 | { | |||||
| 175 | Int32 aggregate4 = (Int32) ((AliasedV alue)c["op portunity_ distcount" ]).Value; | |||||
| 176 | System .Console.W riteLine(" Distinct n ame count of all opp ortunities : " + aggr egate4); | |||||
| 177 | ||||||
| 178 | } | |||||
| 179 | System.Con sole.Write Line("==== ========== ========== ======="); | |||||
| 180 | //</snippe tFetchAggr egationAnd GroupBy4> | |||||
| 181 | //<snippet FetchAggre gationAndG roupBy5> | |||||
| 182 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 183 | // Fe tchXML estimate dvalue_max Aggrega te 5 | |||||
| 184 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 185 | // Fetch t he maximum estimated value of a ll opportu nities. T his is the equivalen t of | |||||
| 186 | // SELECT MAX(estima tedvalue) AS estimat edvalue_ma x ... in S QL. | |||||
| 187 | string est imatedvalu e_max = @" | |||||
| 188 | <fetch dis tinct='fal se' mappin g='logical ' aggregat e='true'> | |||||
| 189 | <entit y name='op portunity' > | |||||
| 190 | <at tribute na me='estima tedvalue' alias='est imatedvalu e_max' agg regate='ma x' /> | |||||
| 191 | </enti ty> | |||||
| 192 | </fetch>"; | |||||
| 193 | ||||||
| 194 | EntityColl ection est imatedvalu e_max_resu lt = _serv iceProxy.R etrieveMul tiple(new FetchExpre ssion(esti matedvalue _max)); | |||||
| 195 | ||||||
| 196 | foreach (v ar c in es timatedval ue_max_res ult.Entiti es) | |||||
| 197 | { | |||||
| 198 | decima l aggregat e5 = ((Mon ey)((Alias edValue)c[ "estimated value_max" ]).Value). Value; | |||||
| 199 | System .Console.W riteLine(" Max estima ted value of all opp ortunities : " + aggr egate5); | |||||
| 200 | ||||||
| 201 | } | |||||
| 202 | System.Con sole.Write Line("==== ========== ========== ======="); | |||||
| 203 | //</snippe tFetchAggr egationAnd GroupBy5> | |||||
| 204 | //<snippet FetchAggre gationAndG roupBy6> | |||||
| 205 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 206 | // Fe tchXML estimate dvalue_min Aggrega te 6 | |||||
| 207 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 208 | // Fetch t he minimum estimated value of a ll opportu nities. T his is the equivalen t of | |||||
| 209 | // SELECT MIN(estima tedvalue) AS estimat edvalue_mi n ... in S QL. | |||||
| 210 | string est imatedvalu e_min = @" | |||||
| 211 | <fetch dis tinct='fal se' mappin g='logical ' aggregat e='true'> | |||||
| 212 | <entit y name='op portunity' > | |||||
| 213 | <at tribute na me='estima tedvalue' alias='est imatedvalu e_min' agg regate='mi n' /> | |||||
| 214 | </enti ty> | |||||
| 215 | </fetch>"; | |||||
| 216 | ||||||
| 217 | EntityColl ection est imatedvalu e_min_resu lt = _serv iceProxy.R etrieveMul tiple(new FetchExpre ssion(esti matedvalue _min)); | |||||
| 218 | ||||||
| 219 | foreach (v ar c in es timatedval ue_min_res ult.Entiti es) | |||||
| 220 | { | |||||
| 221 | decima l aggregat e6 = ((Mon ey)((Alias edValue)c[ "estimated value_min" ]).Value). Value; | |||||
| 222 | System .Console.W riteLine(" Minimum es timated va lue of all opportuni ties: " + aggregate6 ); | |||||
| 223 | ||||||
| 224 | } | |||||
| 225 | System.Con sole.Write Line("==== ========== ========== ======="); | |||||
| 226 | //</snippe tFetchAggr egationAnd GroupBy6> | |||||
| 227 | //<snippet FetchAggre gationAndG roupBy7> | |||||
| 228 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 229 | // Fe tchXML estimate dvalue_sum Aggrega te 7 | |||||
| 230 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 231 | // Fetch t he sum of estimatedv alue for a ll opportu nities. T his is the equivalen t of | |||||
| 232 | // SELECT SUM(estima tedvalue) AS estimat edvalue_su m ... in S QL. | |||||
| 233 | string est imatedvalu e_sum = @" | |||||
| 234 | <fetch dis tinct='fal se' mappin g='logical ' aggregat e='true'> | |||||
| 235 | <entit y name='op portunity' > | |||||
| 236 | <at tribute na me='estima tedvalue' alias='est imatedvalu e_sum' agg regate='su m' /> | |||||
| 237 | </enti ty> | |||||
| 238 | </fetch>"; | |||||
| 239 | ||||||
| 240 | EntityColl ection est imatedvalu e_sum_resu lt = _serv iceProxy.R etrieveMul tiple(new FetchExpre ssion(esti matedvalue _sum)); | |||||
| 241 | ||||||
| 242 | foreach (v ar c in es timatedval ue_sum_res ult.Entiti es) | |||||
| 243 | { | |||||
| 244 | decima l aggregat e7 = ((Mon ey)((Alias edValue)c[ "estimated value_sum" ]).Value). Value; | |||||
| 245 | System .Console.W riteLine(" Sum of est imated val ue of all opportunit ies: " + a ggregate7) ; | |||||
| 246 | ||||||
| 247 | } | |||||
| 248 | System.Con sole.Write Line("==== ========== ========== ======="); | |||||
| 249 | //</snippe tFetchAggr egationAnd GroupBy7> | |||||
| 250 | //<snippet FetchAggre gationAndG roupBy8> | |||||
| 251 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 252 | // Fe tchXML estimate dvalue_avg , estimate dvalue_sum Aggrega te 8 | |||||
| 253 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 254 | // Fetch m ultiple ag gregate va lues withi n a single query. | |||||
| 255 | string est imatedvalu e_avg2 = @ " | |||||
| 256 | <fetch dis tinct='fal se' mappin g='logical ' aggregat e='true'> | |||||
| 257 | <entit y name='op portunity' > | |||||
| 258 | <at tribute na me='opport unityid' a lias='oppo rtunity_co unt' aggre gate='coun t'/> | |||||
| 259 | <at tribute na me='estima tedvalue' alias='est imatedvalu e_sum' agg regate='su m'/> | |||||
| 260 | <at tribute na me='estima tedvalue' alias='est imatedvalu e_avg' agg regate='av g'/> | |||||
| 261 | </enti ty> | |||||
| 262 | </fetch>"; | |||||
| 263 | ||||||
| 264 | EntityColl ection est imatedvalu e_avg2_res ult = _ser viceProxy. RetrieveMu ltiple(new FetchExpr ession(est imatedvalu e_avg2)); | |||||
| 265 | ||||||
| 266 | foreach (v ar c in es timatedval ue_avg2_re sult.Entit ies) | |||||
| 267 | { | |||||
| 268 | Int32 aggregate8 a = (Int32 )((Aliased Value)c["o pportunity _count"]). Value; | |||||
| 269 | System .Console.W riteLine(" Count of a ll opportu nities: " + aggregat e8a); | |||||
| 270 | decima l aggregat e8b = ((Mo ney)((Alia sedValue)c ["estimate dvalue_sum "]).Value) .Value; | |||||
| 271 | System .Console.W riteLine(" Sum of est imated val ue of all opportunit ies: " + a ggregate8b ); | |||||
| 272 | decima l aggregat e8c = ((Mo ney)((Alia sedValue)c ["estimate dvalue_avg "]).Value) .Value; | |||||
| 273 | System .Console.W riteLine(" Average of estimated value of all opport unities: " + aggrega te8c); | |||||
| 274 | ||||||
| 275 | } | |||||
| 276 | System.Con sole.Write Line("==== ========== ========== ======="); | |||||
| 277 | //</snippe tFetchAggr egationAnd GroupBy8> | |||||
| 278 | //<snippet FetchAggre gationAndG roupBy9> | |||||
| 279 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 280 | // Fe tchXML groupby1 Aggrega te 9 | |||||
| 281 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 282 | // Fetch a list of u sers with a count of all the o pportuniti es they ow n using gr oupby. | |||||
| 283 | string gro upby1 = @" | |||||
| 284 | <fetch dis tinct='fal se' mappin g='logical ' aggregat e='true'> | |||||
| 285 | <entit y name='op portunity' > | |||||
| 286 | <at tribute na me='name' alias='opp ortunity_c ount' aggr egate='cou ntcolumn' /> | |||||
| 287 | <at tribute na me='owneri d' alias=' ownerid' g roupby='tr ue' /> | |||||
| 288 | </enti ty> | |||||
| 289 | </fetch>"; | |||||
| 290 | ||||||
| 291 | EntityColl ection gro upby1_resu lt = _serv iceProxy.R etrieveMul tiple(new FetchExpre ssion(grou pby1)); | |||||
| 292 | ||||||
| 293 | foreach (v ar c in gr oupby1_res ult.Entiti es) | |||||
| 294 | { | |||||
| 295 | Int32 aggregate9 a = (Int32 )((Aliased Value)c["o pportunity _count"]). Value; | |||||
| 296 | System .Console.W riteLine(" Count of a ll opportu nities: " + aggregat e9a + "\n" ); | |||||
| 297 | string aggregate 9b = ((Ent ityReferen ce)((Alias edValue)c[ "ownerid"] ).Value).N ame; | |||||
| 298 | System .Console.W riteLine(" Owner: " + aggregate 9b); | |||||
| 299 | string aggregate 9c = (stri ng)((Alias edValue)c[ "ownerid_o wneridyomi name"]).Va lue; | |||||
| 300 | System .Console.W riteLine(" Owner: " + aggregate 9c); | |||||
| 301 | string aggregate 9d = (stri ng)((Alias edValue)c[ "ownerid_o wneridyomi name"]).Va lue; | |||||
| 302 | System .Console.W riteLine(" Owner: " + aggregate 9d); | |||||
| 303 | } | |||||
| 304 | System.Con sole.Write Line("==== ========== ========== ======="); | |||||
| 305 | //</snippe tFetchAggr egationAnd GroupBy9> | |||||
| 306 | //<snippet FetchAggre gationAndG roupBy10> | |||||
| 307 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 308 | // Fe tchXML groupby2 Aggrega te 10 | |||||
| 309 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 310 | // Fetch t he number of opportu nities eac h manager' s direct r eports | |||||
| 311 | // own usi ng a group by within a link-ent ity. | |||||
| 312 | string gro upby2 = @" | |||||
| 313 | <fetch dis tinct='fal se' mappin g='logical ' aggregat e='true'> | |||||
| 314 | <entit y name='op portunity' > | |||||
| 315 | <at tribute na me='name' alias='opp ortunity_c ount' aggr egate='cou ntcolumn' /> | |||||
| 316 | <li nk-entity name='syst emuser' fr om='system userid' to ='ownerid' > | |||||
| 317 | <attribut e name='pa rentsystem userid' al ias='manag erid' grou pby='true' /> | |||||
| 318 | </l ink-entity > | |||||
| 319 | </enti ty> | |||||
| 320 | </fetch>"; | |||||
| 321 | ||||||
| 322 | EntityColl ection gro upby2_resu lt = _serv iceProxy.R etrieveMul tiple(new FetchExpre ssion(grou pby2)); | |||||
| 323 | ||||||
| 324 | foreach (v ar c in gr oupby2_res ult.Entiti es) | |||||
| 325 | { | |||||
| 326 | ||||||
| 327 | int? aggregate 10a = (int ?)((Aliase dValue)c[" opportunit y_count"]) .Value; | |||||
| 328 | Syst em.Console .WriteLine ("Count of all oppor tunities: " + aggreg ate10a + " \n"); | |||||
| 329 | } | |||||
| 330 | System.Con sole.Write Line("==== ========== ========== ======="); | |||||
| 331 | //</snippe tFetchAggr egationAnd GroupBy10> | |||||
| 332 | //<snippet FetchAggre gationAndG roupBy11> | |||||
| 333 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 334 | // Fe tchXML byyear Aggregate 11 | |||||
| 335 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 336 | // Fetch a ggregate i nformation about the opportuni ties that have | |||||
| 337 | // been wo n by year. | |||||
| 338 | string byy ear = @" | |||||
| 339 | <fetch dis tinct='fal se' mappin g='logical ' aggregat e='true'> | |||||
| 340 | <entit y name='op portunity' > | |||||
| 341 | <at tribute na me='opport unityid' a lias='oppo rtunity_co unt' aggre gate='coun t'/> | |||||
| 342 | <at tribute na me='estima tedvalue' alias='est imatedvalu e_sum' agg regate='su m'/> | |||||
| 343 | <at tribute na me='estima tedvalue' alias='est imatedvalu e_avg' agg regate='av g'/> | |||||
| 344 | <at tribute na me='actual closedate' groupby=' true' date grouping=' year' alia s='year' / > | |||||
| 345 | <fi lter type= 'and'> | |||||
| 346 | <conditio n attribut e='stateco de' operat or='eq' va lue='Won' /> | |||||
| 347 | </f ilter> | |||||
| 348 | </enti ty> | |||||
| 349 | </fetch>"; | |||||
| 350 | ||||||
| 351 | EntityColl ection byy ear_result = _servic eProxy.Ret rieveMulti ple(new Fe tchExpress ion(byyear )); | |||||
| 352 | ||||||
| 353 | foreach (v ar c in by year_resul t.Entities ) | |||||
| 354 | { | |||||
| 355 | Int32 aggregate1 1 = (Int32 )((Aliased Value)c["y ear"]).Val ue; | |||||
| 356 | System .Console.W riteLine(" Year: " + aggregate1 1); | |||||
| 357 | Int32 aggregate1 1a = (Int3 2)((Aliase dValue)c[" opportunit y_count"]) .Value; | |||||
| 358 | System .Console.W riteLine(" Count of a ll opportu nities: " + aggregat e11a); | |||||
| 359 | decima l aggregat e11b = ((M oney)((Ali asedValue) c["estimat edvalue_su m"]).Value ).Value; | |||||
| 360 | System .Console.W riteLine(" Sum of est imated val ue of all opportunit ies: " + a ggregate11 b); | |||||
| 361 | decima l aggregat e11c = ((M oney)((Ali asedValue) c["estimat edvalue_av g"]).Value ).Value; | |||||
| 362 | System .Console.W riteLine(" Average of estimated value of all opport unities: " + aggrega te11c); | |||||
| 363 | System .Console.W riteLine(" ---------- ---------- ---------- ---------- ------"); | |||||
| 364 | } | |||||
| 365 | System.Con sole.Write Line("==== ========== ========== ======="); | |||||
| 366 | //</snippe tFetchAggr egationAnd GroupBy11> | |||||
| 367 | //<snippet FetchAggre gationAndG roupBy12> | |||||
| 368 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 369 | // Fe tchXML byquarte r Aggreg ate 12 | |||||
| 370 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 371 | / / Fetch ag gregate in formation about the opportunit ies that h ave | |||||
| 372 | // been wo n by quart er.(return s 1-4) | |||||
| 373 | string byq uarter = @ " | |||||
| 374 | <fetch dis tinct='fal se' mappin g='logical ' aggregat e='true'> | |||||
| 375 | <entit y name='op portunity' > | |||||
| 376 | <at tribute na me='opport unityid' a lias='oppo rtunity_co unt' aggre gate='coun t'/> | |||||
| 377 | <at tribute na me='estima tedvalue' alias='est imatedvalu e_sum' agg regate='su m'/> | |||||
| 378 | <at tribute na me='estima tedvalue' alias='est imatedvalu e_avg' agg regate='av g'/> | |||||
| 379 | <at tribute na me='actual closedate' groupby=' true' date grouping=' quarter' a lias='quar ter' /> | |||||
| 380 | <fi lter type= 'and'> | |||||
| 381 | <conditio n attribut e='stateco de' operat or='eq' va lue='Won' /> | |||||
| 382 | </f ilter> | |||||
| 383 | </enti ty> | |||||
| 384 | </fetch>"; | |||||
| 385 | ||||||
| 386 | EntityColl ection byq uarter_res ult = _ser viceProxy. RetrieveMu ltiple(new FetchExpr ession(byq uarter)); | |||||
| 387 | ||||||
| 388 | foreach (v ar c in by quarter_re sult.Entit ies) | |||||
| 389 | { | |||||
| 390 | Int32 aggregate1 2 = (Int32 )((Aliased Value)c["q uarter"]). Value; | |||||
| 391 | System .Console.W riteLine(" Quarter: " + aggrega te12); | |||||
| 392 | Int32 aggregate1 2a = (Int3 2)((Aliase dValue)c[" opportunit y_count"]) .Value; | |||||
| 393 | System .Console.W riteLine(" Count of a ll opportu nities: " + aggregat e12a); | |||||
| 394 | decima l aggregat e12b = ((M oney)((Ali asedValue) c["estimat edvalue_su m"]).Value ).Value; | |||||
| 395 | System .Console.W riteLine(" Sum of est imated val ue of all opportunit ies: " + a ggregate12 b); | |||||
| 396 | decima l aggregat e12c = ((M oney)((Ali asedValue) c["estimat edvalue_av g"]).Value ).Value; | |||||
| 397 | System .Console.W riteLine(" Average of estimated value of all opport unities: " + aggrega te12c); | |||||
| 398 | System .Console.W riteLine(" ---------- ---------- ---------- ---------- ------"); | |||||
| 399 | } | |||||
| 400 | System.Con sole.Write Line("==== ========== ========== ======="); | |||||
| 401 | //</snippe tFetchAggr egationAnd GroupBy12> | |||||
| 402 | //<snippet FetchAggre gationAndG roupBy13> | |||||
| 403 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 404 | // Fe tchXML bymonth Aggregat e 13 | |||||
| 405 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 406 | // Fetch a ggregate i nformation about the opportuni ties that have | |||||
| 407 | // been wo n by month . (returns 1-12) | |||||
| 408 | string bym onth = @" | |||||
| 409 | <fetch dis tinct='fal se' mappin g='logical ' aggregat e='true'> | |||||
| 410 | <entit y name='op portunity' > | |||||
| 411 | <at tribute na me='opport unityid' a lias='oppo rtunity_co unt' aggre gate='coun t'/> | |||||
| 412 | <at tribute na me='estima tedvalue' alias='est imatedvalu e_sum' agg regate='su m'/> | |||||
| 413 | <at tribute na me='estima tedvalue' alias='est imatedvalu e_avg' agg regate='av g'/> | |||||
| 414 | <at tribute na me='actual closedate' groupby=' true' date grouping=' month' ali as='month' /> | |||||
| 415 | <fi lter type= 'and'> | |||||
| 416 | <conditio n attribut e='stateco de' operat or='eq' va lue='Won' /> | |||||
| 417 | </f ilter> | |||||
| 418 | </enti ty> | |||||
| 419 | </fetch>"; | |||||
| 420 | ||||||
| 421 | EntityColl ection bym onth_resul t = _servi ceProxy.Re trieveMult iple(new F etchExpres sion(bymon th)); | |||||
| 422 | ||||||
| 423 | foreach (v ar c in by month_resu lt.Entitie s) | |||||
| 424 | { | |||||
| 425 | Int32 aggregate1 3 = (Int32 )((Aliased Value)c["m onth"]).Va lue; | |||||
| 426 | System .Console.W riteLine(" Month: " + aggregate 13); | |||||
| 427 | Int32 aggregate1 3a = (Int3 2)((Aliase dValue)c[" opportunit y_count"]) .Value; | |||||
| 428 | System .Console.W riteLine(" Count of a ll opportu nities: " + aggregat e13a); | |||||
| 429 | decima l aggregat e13b = ((M oney)((Ali asedValue) c["estimat edvalue_su m"]).Value ).Value; | |||||
| 430 | System .Console.W riteLine(" Sum of est imated val ue of all opportunit ies: " + a ggregate13 b); | |||||
| 431 | decima l aggregat e13c = ((M oney)((Ali asedValue) c["estimat edvalue_av g"]).Value ).Value; | |||||
| 432 | System .Console.W riteLine(" Average of estimated value of all opport unities: " + aggrega te13c); | |||||
| 433 | System .Console.W riteLine(" ---------- ---------- ---------- ---------- ------"); | |||||
| 434 | } | |||||
| 435 | System.Con sole.Write Line("==== ========== ========== ======="); | |||||
| 436 | //</snippe tFetchAggr egationAnd GroupBy13> | |||||
| 437 | //<snippet FetchAggre gationAndG roupBy14> | |||||
| 438 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 439 | // Fe tchXML byweek Aggregate 14 | |||||
| 440 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 441 | // Fetch a ggregate i nformation about the opportuni ties that have | |||||
| 442 | // been wo n by week. (Returns 1-52) | |||||
| 443 | string byw eek = @" | |||||
| 444 | <fetch dis tinct='fal se' mappin g='logical ' aggregat e='true'> | |||||
| 445 | <entit y name='op portunity' > | |||||
| 446 | <at tribute na me='opport unityid' a lias='oppo rtunity_co unt' aggre gate='coun t'/> | |||||
| 447 | <at tribute na me='estima tedvalue' alias='est imatedvalu e_sum' agg regate='su m'/> | |||||
| 448 | <at tribute na me='estima tedvalue' alias='est imatedvalu e_avg' agg regate='av g'/> | |||||
| 449 | <at tribute na me='actual closedate' groupby=' true' date grouping=' week' alia s='week' / > | |||||
| 450 | <fi lter type= 'and'> | |||||
| 451 | <conditio n attribut e='stateco de' operat or='eq' va lue='Won' /> | |||||
| 452 | </f ilter> | |||||
| 453 | </enti ty> | |||||
| 454 | </fetch>"; | |||||
| 455 | ||||||
| 456 | EntityColl ection byw eek_result = _servic eProxy.Ret rieveMulti ple(new Fe tchExpress ion(byweek )); | |||||
| 457 | ||||||
| 458 | foreach (v ar c in by week_resul t.Entities ) | |||||
| 459 | { | |||||
| 460 | Int32 aggregate1 4 = (Int32 )((Aliased Value)c["w eek"]).Val ue; | |||||
| 461 | System .Console.W riteLine(" Week: " + aggregate1 4); | |||||
| 462 | Int32 aggregate1 4a = (Int3 2)((Aliase dValue)c[" opportunit y_count"]) .Value; | |||||
| 463 | System .Console.W riteLine(" Count of a ll opportu nities: " + aggregat e14a); | |||||
| 464 | decima l aggregat e14b = ((M oney)((Ali asedValue) c["estimat edvalue_su m"]).Value ).Value; | |||||
| 465 | System .Console.W riteLine(" Sum of est imated val ue of all opportunit ies: " + a ggregate14 b); | |||||
| 466 | decima l aggregat e14c = ((M oney)((Ali asedValue) c["estimat edvalue_av g"]).Value ).Value; | |||||
| 467 | System .Console.W riteLine(" Average of estimated value of all opport unities: " + aggrega te14c); | |||||
| 468 | System .Console.W riteLine(" ---------- ---------- ---------- ---------- ------"); | |||||
| 469 | } | |||||
| 470 | System.Con sole.Write Line("==== ========== ========== ======="); | |||||
| 471 | //</snippe tFetchAggr egationAnd GroupBy14> | |||||
| 472 | //<snippet FetchAggre gationAndG roupBy15> | |||||
| 473 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 474 | // Fe tchXML byday Aggregate 15 | |||||
| 475 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 476 | // Fetch a ggregate i nformation about the opportuni ties that have | |||||
| 477 | // been wo n by day. (Returns 1 -31) | |||||
| 478 | string byd ay = @" | |||||
| 479 | <fetch dis tinct='fal se' mappin g='logical ' aggregat e='true'> | |||||
| 480 | <entit y name='op portunity' > | |||||
| 481 | <at tribute na me='opport unityid' a lias='oppo rtunity_co unt' aggre gate='coun t'/> | |||||
| 482 | <at tribute na me='estima tedvalue' alias='est imatedvalu e_sum' agg regate='su m'/> | |||||
| 483 | <at tribute na me='estima tedvalue' alias='est imatedvalu e_avg' agg regate='av g'/> | |||||
| 484 | <at tribute na me='actual closedate' groupby=' true' date grouping=' day' alias ='day' /> | |||||
| 485 | <fi lter type= 'and'> | |||||
| 486 | <conditio n attribut e='stateco de' operat or='eq' va lue='Won' /> | |||||
| 487 | </f ilter> | |||||
| 488 | </enti ty> | |||||
| 489 | </fetch>"; | |||||
| 490 | ||||||
| 491 | EntityColl ection byd ay_result = _service Proxy.Retr ieveMultip le(new Fet chExpressi on(byday)) ; | |||||
| 492 | ||||||
| 493 | foreach (v ar c in by day_result .Entities) | |||||
| 494 | { | |||||
| 495 | Int32 aggregate1 5 = (Int32 )((Aliased Value)c["d ay"]).Valu e; | |||||
| 496 | System .Console.W riteLine(" Day: " + a ggregate15 ); | |||||
| 497 | Int32 aggregate1 5a = (Int3 2)((Aliase dValue)c[" opportunit y_count"]) .Value; | |||||
| 498 | System .Console.W riteLine(" Count of a ll opportu nities: " + aggregat e15a); | |||||
| 499 | decima l aggregat e15b = ((M oney)((Ali asedValue) c["estimat edvalue_su m"]).Value ).Value; | |||||
| 500 | System .Console.W riteLine(" Sum of est imated val ue of all opportunit ies: " + a ggregate15 b); | |||||
| 501 | decima l aggregat e15c = ((M oney)((Ali asedValue) c["estimat edvalue_av g"]).Value ).Value; | |||||
| 502 | System .Console.W riteLine(" Average of estimated value of all opport unities: " + aggrega te15c); | |||||
| 503 | System .Console.W riteLine(" ---------- ---------- ---------- ---------- ------"); | |||||
| 504 | } | |||||
| 505 | System.Con sole.Write Line("==== ========== ========== ======="); | |||||
| 506 | //</snippe tFetchAggr egationAnd GroupBy15> | |||||
| 507 | //<snippet FetchAggre gationAndG roupBy16> | |||||
| 508 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 509 | // Fe tchXML byyrqtr Aggregat e 16 | |||||
| 510 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 511 | // Fetch a ggregate i nformation about the opportuni ties that have | |||||
| 512 | // been wo n by year and quarte r. | |||||
| 513 | string byy rqtr = @" | |||||
| 514 | <fetch dis tinct='fal se' mappin g='logical ' aggregat e='true'> | |||||
| 515 | <entit y name='op portunity' > | |||||
| 516 | <at tribute na me='opport unityid' a lias='oppo rtunity_co unt' aggre gate='coun t'/> | |||||
| 517 | <at tribute na me='estima tedvalue' alias='est imatedvalu e_sum' agg regate='su m'/> | |||||
| 518 | <at tribute na me='estima tedvalue' alias='est imatedvalu e_avg' agg regate='av g'/> | |||||
| 519 | <at tribute na me='actual closedate' groupby=' true' date grouping=' quarter' a lias='quar ter' /> | |||||
| 520 | <at tribute na me='actual closedate' groupby=' true' date grouping=' year' alia s='year' / > | |||||
| 521 | <fi lter type= 'and'> | |||||
| 522 | <conditio n attribut e='stateco de' operat or='eq' va lue='Won' /> | |||||
| 523 | </f ilter> | |||||
| 524 | </enti ty> | |||||
| 525 | </fetch>"; | |||||
| 526 | ||||||
| 527 | EntityColl ection byy rqtr_resul t = _servi ceProxy.Re trieveMult iple(new F etchExpres sion(byyrq tr)); | |||||
| 528 | ||||||
| 529 | foreach (v ar c in by yrqtr_resu lt.Entitie s) | |||||
| 530 | { | |||||
| 531 | Int32 aggregate1 6d = (Int3 2)((Aliase dValue)c[" year"]).Va lue; | |||||
| 532 | System .Console.W riteLine(" Year: " + aggregate1 6d); | |||||
| 533 | Int32 aggregate1 6 = (Int32 )((Aliased Value)c["q uarter"]). Value; | |||||
| 534 | System .Console.W riteLine(" Quarter: " + aggrega te16); | |||||
| 535 | Int32 aggregate1 6a = (Int3 2)((Aliase dValue)c[" opportunit y_count"]) .Value; | |||||
| 536 | System .Console.W riteLine(" Count of a ll opportu nities: " + aggregat e16a); | |||||
| 537 | decima l aggregat e16b = ((M oney)((Ali asedValue) c["estimat edvalue_su m"]).Value ).Value; | |||||
| 538 | System .Console.W riteLine(" Sum of est imated val ue of all opportunit ies: " + a ggregate16 b); | |||||
| 539 | decima l aggregat e16c = ((M oney)((Ali asedValue) c["estimat edvalue_av g"]).Value ).Value; | |||||
| 540 | System .Console.W riteLine(" Average of estimated value of all opport unities: " + aggrega te16c); | |||||
| 541 | System .Console.W riteLine(" ---------- ---------- ---------- ---------- ------"); | |||||
| 542 | } | |||||
| 543 | System.Con sole.Write Line("==== ========== ========== ======="); | |||||
| 544 | //</snippe tFetchAggr egationAnd GroupBy16> | |||||
| 545 | //<snippet FetchAggre gationAndG roupBy17> | |||||
| 546 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 547 | // Fe tchXML byyrqtr2 Aggrega te 17 | |||||
| 548 | // ******* ********** ********** ********** ********** ********** ********** ********** ********** ********** ********** ****** | |||||
| 549 | // Specify the resul t order fo r the prev ious sampl e. Order by year, t hen quarte r. | |||||
| 550 | string byy rqtr2 = @" | |||||
| 551 | <fetch dis tinct='fal se' mappin g='logical ' aggregat e='true'> | |||||
| 552 | <entit y name='op portunity' > | |||||
| 553 | <at tribute na me='opport unityid' a lias='oppo rtunity_co unt' aggre gate='coun t'/> | |||||
| 554 | <at tribute na me='estima tedvalue' alias='est imatedvalu e_sum' agg regate='su m'/> | |||||
| 555 | <at tribute na me='estima tedvalue' alias='est imatedvalu e_avg' agg regate='av g'/> | |||||
| 556 | <at tribute na me='actual closedate' groupby=' true' date grouping=' quarter' a lias='quar ter' /> | |||||
| 557 | <at tribute na me='actual closedate' groupby=' true' date grouping=' year' alia s='year' / > | |||||
| 558 | <or der alias= 'year' des cending='f alse' /> | |||||
| 559 | <or der alias= 'quarter' descending ='false' / > | |||||
| 560 | <fi lter type= 'and'> | |||||
| 561 | <conditio n attribut e='stateco de' operat or='eq' va lue='Won' /> | |||||
| 562 | </f ilter> | |||||
| 563 | </enti ty> | |||||
| 564 | </fetch>"; | |||||
| 565 | ||||||
| 566 | EntityColl ection byy rqtr2_resu lt = _serv iceProxy.R etrieveMul tiple(new FetchExpre ssion(byyr qtr2)); | |||||
| 567 | ||||||
| 568 | foreach (v ar c in by yrqtr2_res ult.Entiti es) | |||||
| 569 | { | |||||
| 570 | Int32 aggregate1 7 = (Int32 )((Aliased Value)c["q uarter"]). Value; | |||||
| 571 | System .Console.W riteLine(" Quarter: " + aggrega te17); | |||||
| 572 | Int32 aggregate1 7d = (Int3 2)((Aliase dValue)c[" year"]).Va lue; | |||||
| 573 | System .Console.W riteLine(" Year: " + aggregate1 7d); | |||||
| 574 | Int32 aggregate1 7a = (Int3 2)((Aliase dValue)c[" opportunit y_count"]) .Value; | |||||
| 575 | System .Console.W riteLine(" Count of a ll opportu nities: " + aggregat e17a); | |||||
| 576 | decima l aggregat e17b = ((M oney)((Ali asedValue) c["estimat edvalue_su m"]).Value ).Value; | |||||
| 577 | System .Console.W riteLine(" Sum of est imated val ue of all opportunit ies: " + a ggregate17 b); | |||||
| 578 | decima l aggregat e17c = ((M oney)((Ali asedValue) c["estimat edvalue_av g"]).Value ).Value; | |||||
| 579 | System .Console.W riteLine(" Average of estimated value of all opport unities: " + aggrega te17c); | |||||
| 580 | System .Console.W riteLine(" ---------- ---------- ---------- ---------- ------"); | |||||
| 581 | } | |||||
| 582 | System.Con sole.Write Line("==== ========== ========== ======="); | |||||
| 583 | ||||||
| 584 | //</snippe tFetchAggr egationAnd GroupBy17> | |||||
| 585 | ||||||
| 586 | Console.Wr iteLine("R etrieved a ggregate r ecord data ."); | |||||
| 587 | ||||||
| 588 | DeleteRequ iredRecord s(promptFo rDelete); | |||||
| 589 | ||||||
| 590 | } | |||||
| 591 | } | |||||
| 592 | ||||||
| 593 | // Catch any servi ce fault e xceptions that Micro soft Dynam ics CRM th rows. | |||||
| 594 | catch (F aultExcept ion<Micros oft.Xrm.Sd k.Organiza tionServic eFault>) | |||||
| 595 | { | |||||
| 596 | // Y ou can han dle an exc eption her e or pass it back to the calli ng method. | |||||
| 597 | thro w; | |||||
| 598 | } | |||||
| 599 | return; | |||||
| 600 | } | |||||
| 601 | ||||||
| 602 | // / <summary > | |||||
| 603 | // / This met hod create s any enti ty records that this sample re quires. | |||||
| 604 | // / Create a sample ac count reco rd. | |||||
| 605 | // / Create 3 opportuni ty records associate to the ac count reco rd. | |||||
| 606 | // / Mark all 3 opportu nity recor ds as won. | |||||
| 607 | // / </summar y> | |||||
| 608 | pu blic void CreateRequ iredRecord s() | |||||
| 609 | { | |||||
| 610 | ||||||
| 611 | var Oppo rtunitySta tusCodes = new | |||||
| 612 | { | |||||
| 613 | Won = 3 | |||||
| 614 | }; | |||||
| 615 | ||||||
| 616 | // Insta ntiate a a ccount ent ity record and set i ts propert y values. | |||||
| 617 | // See t he Entity Metadata t opic in th e SDK docu mentation | |||||
| 618 | // to de termine wh ich attrib utes must be set for each enti ty. | |||||
| 619 | Account setupAccou nt = new A ccount | |||||
| 620 | { | |||||
| 621 | Name = "Exampl e Account" | |||||
| 622 | }; | |||||
| 623 | ||||||
| 624 | _account Id = _serv ice.Create (setupAcco unt); | |||||
| 625 | ||||||
| 626 | Console. WriteLine( "Created { 0}.", setu pAccount.N ame); | |||||
| 627 | ||||||
| 628 | ||||||
| 629 | EntityRe ference se tupCustome r = new En tityRefere nce | |||||
| 630 | { | |||||
| 631 | Logi calName = Account.En tityLogica lName, | |||||
| 632 | Id = _accountI d | |||||
| 633 | }; | |||||
| 634 | ||||||
| 635 | // Creat e 3 sample opportuni ties. | |||||
| 636 | // Insta ntiate opp ortunity e ntity reco rds and se t there pr operty val ues. | |||||
| 637 | // See t he Entity Metadata t opic in th e SDK docu mentation | |||||
| 638 | // to de termine wh ich attrib utes must be set for each enti ty. | |||||
| 639 | Opportun ity[] setu pOpportuni ties = new [] | |||||
| 640 | { | |||||
| 641 | new Opport unity | |||||
| 642 | { | |||||
| 643 | Name = "Sample O pp 1", | |||||
| 644 | Estima tedValue = new Money {Value = 120000.00m }, | |||||
| 645 | Custom erId = set upCustomer , | |||||
| 646 | }, | |||||
| 647 | new Opport unity | |||||
| 648 | { | |||||
| 649 | Name = "Sample O pp With Du plicate Na me", | |||||
| 650 | Estima tedValue = new Money { Value = 240000.00 m}, | |||||
| 651 | Custom erId = set upCustomer , | |||||
| 652 | }, | |||||
| 653 | new Opport unity | |||||
| 654 | { | |||||
| 655 | Name = "Sample O pp With Du plicate Na me", | |||||
| 656 | Estima tedValue = new Money { Value = 360000.00m }, | |||||
| 657 | Custom erId = set upCustomer , | |||||
| 658 | }, | |||||
| 659 | }; | |||||
| 660 | ||||||
| 661 | _setupOp portunitiy Ids = (fro m opp in s etupOpport unities | |||||
| 662 | select _service.C reate(opp) ).ToArray( ); | |||||
| 663 | ||||||
| 664 | // "Win" each of t he opportu nities | |||||
| 665 | foreach (Guid oppI d in _setu pOpportuni tiyIds) | |||||
| 666 | { | |||||
| 667 | WinO pportunity Request wi nRequest = new WinOp portunityR equest | |||||
| 668 | { | |||||
| 669 | Opportunit yClose = n ew Opportu nityClose | |||||
| 670 | { | |||||
| 671 | Opport unityId = new Entity Reference | |||||
| 672 | { | |||||
| 673 | Lo gicalName = "opportu nity", | |||||
| 674 | Id = oppId | |||||
| 675 | }, | |||||
| 676 | ||||||
| 677 | // Mar k these en tities as won in the year 2008 to | |||||
| 678 | // hav e some tes table resu lts. | |||||
| 679 | Actual End = new DateTime(2 009, 11, 1 , 12, 0, 0 ) | |||||
| 680 | }, | |||||
| 681 | Status = n ew OptionS etValue(Op portunityS tatusCodes .Won) | |||||
| 682 | }; | |||||
| 683 | _ser vice.Execu te(winRequ est); | |||||
| 684 | } | |||||
| 685 | ||||||
| 686 | Console. WriteLine( "Created { 0} sample opportunit y records and update d as won f or this sa mple.", _s etupOpport unitiyIds. Length); | |||||
| 687 | ||||||
| 688 | return; | |||||
| 689 | } | |||||
| 690 | ||||||
| 691 | // / <summary > | |||||
| 692 | // / Deletes any entity records t hat were c reated for this samp le. | |||||
| 693 | // / <param n ame="promp t">Indicat es whether to prompt the user to delete the record s created in this sa mple.</par am> | |||||
| 694 | // / </summar y> | |||||
| 695 | pu blic void DeleteRequ iredRecord s(bool pro mpt) | |||||
| 696 | { | |||||
| 697 | bool del eteRecords = true; | |||||
| 698 | ||||||
| 699 | if (prom pt) | |||||
| 700 | { | |||||
| 701 | Cons ole.WriteL ine("\nDo you want t hese entit y records deleted? ( y/n)"); | |||||
| 702 | Stri ng answer = Console. ReadLine() ; | |||||
| 703 | ||||||
| 704 | dele teRecords = (answer. StartsWith ("y") || a nswer.Star tsWith("Y" )); | |||||
| 705 | } | |||||
| 706 | ||||||
| 707 | if (dele teRecords) | |||||
| 708 | { | |||||
| 709 | fore ach (Guid oppId in _ setupOppor tunitiyIds ) | |||||
| 710 | { | |||||
| 711 | _service.D elete("opp ortunity", oppId); | |||||
| 712 | } | |||||
| 713 | ||||||
| 714 | _ser vice.Delet e("account ", _accoun tId); | |||||
| 715 | ||||||
| 716 | Cons ole.WriteL ine("Entit y records have been deleted.") ; | |||||
| 717 | } | |||||
| 718 | } | |||||
| 719 | ||||||
| 720 | #e ndregion H ow To Samp le Code | |||||
| 721 | ||||||
| 722 | #r egion Main | |||||
| 723 | ||||||
| 724 | // / <summary > | |||||
| 725 | // / Standard Main() me thod used by most SD K samples. | |||||
| 726 | // / </summar y> | |||||
| 727 | // / <param n ame="args" ></param> | |||||
| 728 | st atic publi c void Mai n(string[] args) | |||||
| 729 | { | |||||
| 730 | try | |||||
| 731 | { | |||||
| 732 | // O btain the target org anization' s Web addr ess and cl ient logon | |||||
| 733 | // c redentials from the user. | |||||
| 734 | Serv erConnecti on serverC onnect = n ew ServerC onnection( ); | |||||
| 735 | Serv erConnecti on.Configu ration con fig = serv erConnect. GetServerC onfigurati on(); | |||||
| 736 | ||||||
| 737 | Fetc hAggregati onAndGroup By app = n ew FetchAg gregationA ndGroupBy( ); | |||||
| 738 | app. Run(config , true); | |||||
| 739 | } | |||||
| 740 | catch (F aultExcept ion<Micros oft.Xrm.Sd k.Organiza tionServic eFault> ex ) | |||||
| 741 | { | |||||
| 742 | Cons ole.WriteL ine("The a pplication terminate d with an error."); | |||||
| 743 | Cons ole.WriteL ine("Times tamp: {0}" , ex.Detai l.Timestam p); | |||||
| 744 | Cons ole.WriteL ine("Code: {0}", ex. Detail.Err orCode); | |||||
| 745 | Cons ole.WriteL ine("Messa ge: {0}", ex.Detail. Message); | |||||
| 746 | Cons ole.WriteL ine("Plugi n Trace: { 0}", ex.De tail.Trace Text); | |||||
| 747 | Cons ole.WriteL ine("Inner Fault: {0 }", | |||||
| 748 | null == ex .Detail.In nerFault ? "No Inner Fault" : "Has Inner Fault"); | |||||
| 749 | } | |||||
| 750 | catch (S ystem.Time outExcepti on ex) | |||||
| 751 | { | |||||
| 752 | Cons ole.WriteL ine("The a pplication terminate d with an error."); | |||||
| 753 | Cons ole.WriteL ine("Messa ge: {0}", ex.Message ); | |||||
| 754 | Cons ole.WriteL ine("Stack Trace: {0 }", ex.Sta ckTrace); | |||||
| 755 | Cons ole.WriteL ine("Inner Fault: {0 }", | |||||
| 756 | null == ex .InnerExce ption.Mess age ? "No Inner Faul t" : ex.In nerExcepti on.Message ); | |||||
| 757 | } | |||||
| 758 | catch (S ystem.Exce ption ex) | |||||
| 759 | { | |||||
| 760 | Cons ole.WriteL ine("The a pplication terminate d with an error."); | |||||
| 761 | Cons ole.WriteL ine(ex.Mes sage); | |||||
| 762 | ||||||
| 763 | // D isplay the details o f the inne r exceptio n. | |||||
| 764 | if ( ex.InnerEx ception != null) | |||||
| 765 | { | |||||
| 766 | Console.Wr iteLine(ex .InnerExce ption.Mess age); | |||||
| 767 | ||||||
| 768 | FaultExcep tion<Micro soft.Xrm.S dk.Organiz ationServi ceFault> f e = ex.Inn erExceptio n | |||||
| 769 | as Fau ltExceptio n<Microsof t.Xrm.Sdk. Organizati onServiceF ault>; | |||||
| 770 | if (fe != null) | |||||
| 771 | { | |||||
| 772 | Consol e.WriteLin e("Timesta mp: {0}", fe.Detail. Timestamp) ; | |||||
| 773 | Consol e.WriteLin e("Code: { 0}", fe.De tail.Error Code); | |||||
| 774 | Consol e.WriteLin e("Message : {0}", fe .Detail.Me ssage); | |||||
| 775 | Consol e.WriteLin e("Plugin Trace: {0} ", fe.Deta il.TraceTe xt); | |||||
| 776 | Consol e.WriteLin e("Inner F ault: {0}" , | |||||
| 777 | nu ll == fe.D etail.Inne rFault ? " No Inner F ault" : "H as Inner F ault"); | |||||
| 778 | } | |||||
| 779 | } | |||||
| 780 | } | |||||
| 781 | // Addit ional exce ptions to catch: Sec urityToken Validation Exception, ExpiredSe curityToke nException , | |||||
| 782 | // Secur ityAccessD eniedExcep tion, Mess ageSecurit yException , and Secu rityNegoti ationExcep tion. | |||||
| 783 | ||||||
| 784 | finally | |||||
| 785 | { | |||||
| 786 | Cons ole.WriteL ine("Press <Enter> t o exit."); | |||||
| 787 | Cons ole.ReadLi ne(); | |||||
| 788 | } | |||||
| 789 | ||||||
| 790 | } | |||||
| 791 | #e ndregion M ain | |||||
| 792 | } | |||||
| 793 | } | |||||
| 794 | //</snippe tFetchAggr egationAnd GroupBy> |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.