-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
SampleHelpers.cs
214 lines (187 loc) · 9.18 KB
/
SampleHelpers.cs
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Tooling.Connector;
using PowerApps.Samples.LoginUX;
using System;
using System.Configuration;
using System.ServiceModel;
namespace PowerApps.Samples
{
public class SampleHelpers
{
/// <summary>
/// Checks whether the current environment will support this sample.
/// </summary>
/// <param name="service">The service to use to check the version. </param>
/// <param name="minVersion">The minimum version.</param>
/// <returns>true when the version is higher than the minimum verions, otherwise false.</returns>
public static bool CheckVersion(CrmServiceClient service, Version minVersion)
{
if (service.ConnectedOrgVersion.CompareTo(minVersion) >= 0)
{
return true;
}
else
{
Console.WriteLine("This sample cannot be run against the current version.");
Console.WriteLine(string.Format("Upgrade your instance to a version above {0} to run this sample.", minVersion.ToString()));
return false;
}
}
/// <summary>
/// Imports a solution if it is not already installed.
/// </summary>
/// <param name="service">The service to use to import the solution. </param>
/// <param name="uniqueName">The unique name of the solution to install.</param>
/// <param name="pathToFile">The path to the solution file.</param>
/// <returns>true if the solution was installed, otherwise false.</returns>
public static bool ImportSolution(CrmServiceClient service, string uniqueName, string pathToFile)
{
QueryByAttribute queryCheckForSampleSolution = new QueryByAttribute();
queryCheckForSampleSolution.AddAttributeValue("uniquename", uniqueName);
queryCheckForSampleSolution.EntityName = "solution";
EntityCollection querySampleSolutionResults = service.RetrieveMultiple(queryCheckForSampleSolution);
if (querySampleSolutionResults.Entities.Count > 0)
{
Console.WriteLine("The {0} solution is already installed.", uniqueName);
return false;
}
else
{
Console.WriteLine("The {0} solution is not installed. Importing the solution....", uniqueName);
Guid ImportId = Guid.Empty;
service.ImportSolutionToCrm(pathToFile, out ImportId);
return true;
}
}
/// <summary>
/// Prompts user to delete solution. Deletes solution if they choose.
/// </summary>
/// <param name="service">The service to use to delete the solution. </param>
/// <param name="uniqueName">The unique name of the solution to delete.</param>
/// <returns>true when the solution was deleted, otherwise false.</returns>
public static bool DeleteSolution(CrmServiceClient service, string uniqueName)
{
bool deleteSolution = true;
Console.WriteLine("Do you want to delete the {0} solution? (y/n)", uniqueName);
String answer = Console.ReadLine();
deleteSolution = (answer.StartsWith("y") || answer.StartsWith("Y"));
if (deleteSolution)
{
Console.WriteLine("Deleting the {0} solution....", uniqueName);
QueryExpression solutionQuery = new QueryExpression
{
EntityName = "solution",
ColumnSet = new ColumnSet(new string[] { "solutionid", "friendlyname" }),
Criteria = new FilterExpression()
};
solutionQuery.Criteria.AddCondition("uniquename", ConditionOperator.Equal, uniqueName);
Entity solution = service.RetrieveMultiple(solutionQuery).Entities[0];
if (solution != null)
{
service.Delete("solution", (Guid)solution["solutionid"]);
Console.WriteLine("Deleted the {0} solution.", solution["friendlyname"]);
return true;
}
else
{
Console.WriteLine("No solution named {0} is installed.");
}
}
return false;
}
/// <summary>
/// A function to manage exceptions thrown by console application samples
/// </summary>
/// <param name="exceptionFromSample">The exception thrown</param>
public static void HandleException(Exception exceptionFromSample) {
Console.WriteLine("The application terminated with an error.");
try
{
throw exceptionFromSample;
}
catch (FaultException<OrganizationServiceFault> fe)
{
Console.WriteLine("Timestamp: {0}", fe.Detail.Timestamp);
Console.WriteLine("Code: {0}", fe.Detail.ErrorCode);
Console.WriteLine("Message: {0}", fe.Detail.Message);
Console.WriteLine("Plugin Trace: {0}", fe.Detail.TraceText);
Console.WriteLine("Inner Fault: {0}",
null == fe.Detail.InnerFault ? "No Inner Fault" : "Has Inner Fault");
}
catch (TimeoutException te) {
Console.WriteLine("Message: {0}", te.Message);
Console.WriteLine("Stack Trace: {0}", te.StackTrace);
Console.WriteLine("Inner Fault: {0}",
null == te.InnerException.Message ? "No Inner Fault" : te.InnerException.Message);
}
catch (Exception ex) {
// Display the details of the inner exception.
if (ex.InnerException != null)
{
Console.WriteLine(ex.InnerException.Message);
FaultException<OrganizationServiceFault> fe = ex.InnerException
as FaultException<OrganizationServiceFault>;
if (fe != null)
{
Console.WriteLine("Timestamp: {0}", fe.Detail.Timestamp);
Console.WriteLine("Code: {0}", fe.Detail.ErrorCode);
Console.WriteLine("Message: {0}", fe.Detail.Message);
Console.WriteLine("Plugin Trace: {0}", fe.Detail.TraceText);
Console.WriteLine("Inner Fault: {0}",
null == fe.Detail.InnerFault ? "No Inner Fault" : "Has Inner Fault");
}
}
}
}
/// <summary>
/// Gets a named connection string from App.config
/// </summary>
/// <param name="name">The name of the connection string to return</param>
/// <returns>The named connection string</returns>
private static string GetConnectionStringFromAppConfig(string name)
{
//Verify cds/App.config contains a valid connection string with the name.
if (ConfigurationManager.ConnectionStrings[name] == null)
{
Console.WriteLine("You can define a connection string in PowerApps-Samples/dataverse/App.config before running this sample. Switching to interactive mode...");
return string.Empty;
}
return ConfigurationManager.ConnectionStrings[name].ConnectionString;
}
public static CrmServiceClient Connect(string name) {
CrmServiceClient service = null;
//You can specify connection information in cds/App.config to run this sample without the login dialog
if (string.IsNullOrEmpty(GetConnectionStringFromAppConfig("Connect")))
{
// Failed to find a connection string... Show login Dialog.
ExampleLoginForm loginFrm = new ExampleLoginForm();
// Login process is Async, thus we need to detect when login is completed and close the form.
loginFrm.ConnectionToCrmCompleted += LoginFrm_ConnectionToCrmCompleted;
// Show the dialog here.
loginFrm.ShowDialog();
// If the login process completed, assign the connected service to the CRMServiceClient var
if (loginFrm.CrmConnectionMgr != null && loginFrm.CrmConnectionMgr.CrmSvc != null && loginFrm.CrmConnectionMgr.CrmSvc.IsReady)
service = loginFrm.CrmConnectionMgr.CrmSvc;
}
else
{
// Try to create via connection string.
service = new CrmServiceClient(GetConnectionStringFromAppConfig("Connect"));
}
return service;
}
/// <summary>
/// Handle closing the dialog when completed.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void LoginFrm_ConnectionToCrmCompleted(object sender, EventArgs e)
{
if (sender is ExampleLoginForm)
{
((ExampleLoginForm)sender).Close();
}
}
}
}