Repository navigation
Expand file tree
/
Copy pathParallelRetrieveMultipleSamples.cs
More file actions
185 lines (158 loc) · 8.17 KB
/
Copy pathParallelRetrieveMultipleSamples.cs
File metadata and controls
185 lines (158 loc) · 8.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*================================================================================================================================
This Sample Code is provided for the purpose of illustration only and is not intended to be used in a production environment.
THIS SAMPLE CODE AND ANY RELATED INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
We grant You a nonexclusive, royalty-free right to use and modify the Sample Code and to reproduce and distribute the object
code form of the Sample Code, provided that You agree: (i) to not use Our name, logo, or trademarks to market Your software
product in which the Sample Code is embedded; (ii) to include a valid copyright notice on Your software product in which the
Sample Code is embedded; and (iii) to indemnify, hold harmless, and defend Us and Our suppliers from and against any claims
or lawsuits, including attorneys’ fees, that arise or result from the use or distribution of the Sample Code.
=================================================================================================================================*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Crm.Sdk.Messages;
namespace Microsoft.Pfe.Xrm.Samples
{
class ParallelRetrieveMultipleSamples
{
public ParallelRetrieveMultipleSamples(Uri serverUri, string username, string password)
{
this.Manager = new OrganizationServiceManager(serverUri, username, password);
}
/// <summary>
/// Reusable instance of OrganizationServiceManager
/// </summary>
OrganizationServiceManager Manager { get; set; }
/// <summary>
/// Demonstrates parallelized submission of multiple queries
/// </summary>
/// <param name="queries">The keyed collection of queries to retrieve</param>
/// <returns>The keyed collection of query result collections</returns>
public IDictionary<string, EntityCollection> ParallelRetrieveMultiple(IDictionary<string, QueryBase> queries)
{
IDictionary<string, EntityCollection> results = null;
try
{
results = this.Manager.ParallelProxy.RetrieveMultiple(queries);
foreach (var result in results)
{
Console.WriteLine("Query with key={0} for {1} returned {2} records.",
result.Key,
result.Value.EntityName,
result.Value.Entities.Count);
}
}
catch (AggregateException ae)
{
// Handle exceptions
}
return results;
}
/// <summary>
/// Demonstrates parallelized submission of multiple queries where all pages of results are retrieved for each query
/// </summary>
/// <param name="queries">The keyed collection of queries to retrieve</param>
/// <returns>The keyed collection of query result collections</returns>
public IDictionary<string, EntityCollection> ParallelRetrieveMultipleAllPages(IDictionary<string, QueryBase> queries)
{
IDictionary<string, EntityCollection> results = null;
try
{
results = this.Manager.ParallelProxy.RetrieveMultiple(queries, true);
foreach (var result in results)
{
Console.WriteLine("Query with key={0} for {1} returned {2} records.",
result.Key,
result.Value.EntityName,
result.Value.Entities.Count);
}
}
catch (AggregateException ae)
{
// Handle exceptions
}
return results;
}
/// <summary>
/// Demonstrates parallelized submission of multiple queries. Queries can be mix of QueryExpression and FetchXML
/// </summary>
/// <returns>The keyed collection of query result collections</returns>
public IDictionary<string, EntityCollection> ParallelRetrieveMultipleFull()
{
IDictionary<string, EntityCollection> results = null;
var queries = new Dictionary<string, QueryBase>();
var accountQuery = new QueryExpression("account");
accountQuery.ColumnSet.AddColumns("name", "address1_city", "primarycontactid");
accountQuery.Criteria.AddCondition(new ConditionExpression("name", ConditionOperator.BeginsWith, "C"));
accountQuery.NoLock = true;
queries.Add("accounts", accountQuery);
var contactQuery = new QueryExpression("contact");
contactQuery.ColumnSet.AddColumns("firstname", "lastname", "parentcustomerid");
contactQuery.Criteria.AddCondition(new ConditionExpression("statecode", ConditionOperator.Equal, 1));
contactQuery.NoLock = true;
queries.Add("contacts", contactQuery);
var oppQuery = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='opportunity'>
<attribute name='name' />
<attribute name='estimatedvalue' />
<attribute name='customerid' />
<filter type='and'>
<condition attribute='estimatedvalue' operator='gt' value='1000' />
</filter>
</entity>
</fetch>";
queries.Add("opportunities", new FetchExpression(oppQuery));
try
{
results = this.Manager.ParallelProxy.RetrieveMultiple(queries, true);
foreach (var result in results)
{
Console.WriteLine("Query with key={0} for {1} returned {2} records.",
result.Key,
result.Value.EntityName,
result.Value.Entities.Count);
}
}
catch (AggregateException ae)
{
// Handle exceptions
}
return results;
}
/// <summary>
/// Demonstrates parallelized submission of multiple queries with the optional exception handler delegate
/// </summary>
/// <param name="queries">The keyed collection of queries to retrieve</param>
/// <returns>The keyed collection of query result collections</returns>
/// <remarks>
/// The exception handler delegate is provided the request type and the fault exception encountered. This delegate function is executed on the
/// calling thread after all parallel operations are complete
/// </remarks>
public IDictionary<string, EntityCollection> ParallelRetrieveMultipleWithExceptionHandler(IDictionary<string, QueryBase> queries)
{
int errorCount = 0;
IDictionary<string, EntityCollection> results = null;
try
{
results = this.Manager.ParallelProxy.RetrieveMultiple(queries, true,
(query, ex) =>
{
System.Diagnostics.Debug.WriteLine("Error encountered during query with key={0}: {1}", query.Key, ex.Detail.Message);
errorCount++;
});
}
catch (AggregateException ae)
{
// Handle exceptions
}
Console.WriteLine("{0} errors encountered during parallel queries.", errorCount);
return results;
}
}
}