using System using System Collections Generic using System Text using

  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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using System.Web.Services;
using Microsoft.Crm.Sdk.Query;
using System.IO;
//using ContactPostNumeratorPlugIn.CRM;
namespace ContactPostNumeratorPlugIn
{
public class Plugin : IPlugin
{
StreamWriter sw;
private string crmUrl;
public Plugin(string unsecure, string secure)
{
crmUrl = unsecure;
}
//http://vs14/mscrmservices/2007/CrmServiceWsdl.aspx
public void Execute(IPluginExecutionContext context)
{
if (!File.Exists(@"C:\Temp\EntityUpdateOnNoteCreate.txt"))
{
File.Create(@"C:\Temp\EntityUpdateOnNoteCreate.txt");
}
FileInfo f = new FileInfo(@"C:\Temp\EntityUpdateOnNoteCreate.txt");
if (f.Exists)
f.Delete();
sw = f.AppendText();
sw.WriteLine(DateTime.Now.ToLongTimeString() + " start");
sw.WriteLine("Primary Entity Name (form context) : " + context.PrimaryEntityName);
Microsoft.Crm.Sdk.DynamicEntity entity = null;
#region Verify execution context
// Check if the input parameters property bag contains a target
// of the create operation and that target is of type DynamicEntity.
if (context.InputParameters.Properties.Contains("Target") &&
context.InputParameters.Properties["Target"] is Microsoft.Crm.Sdk.DynamicEntity)
{
// Obtain the target business entity from the input parmameters.
entity = (DynamicEntity)context.InputParameters.Properties[ParameterName.Target];
//entity = (Microsoft.Crm.Sdk.DynamicEntity)context.InputParameters.Properties["Target"];
// Verify that the entity represents an contact.
if (entity.Name != EntityName.contact.ToString()) { return; }
sw.WriteLine("entity" + entity.Name);
}
else
{
return;
}
#endregion Verify execution context
try
{
Guid newNumerator = Guid.Empty;
DynamicEntity numerator = new DynamicEntity();
numerator.Name = "new_numerator";
//// Set up the CRM Service.
//CrmAuthenticationToken token = new CrmAuthenticationToken();
//token.AuthenticationType = 0;
//token.OrganizationName = context.OrganizationName;
//CrmService service = new CrmService();
//service.Url = crmUrl;
//service.CrmAuthenticationTokenValue = token;
//service.Credentials = System.Net.CredentialCache.DefaultCredentials;
// Retrieve data for the created note
ICrmService iservice = (ICrmService)context.CreateCrmService(true);
AllColumns cols = new AllColumns(); //new ColumnSet();
// cols.AddColumns(new string[] { "new_donornumber" });
//cols.Attributes = new string[] { "new_donornumber" };
TargetRetrieveDynamic trDynamic = new TargetRetrieveDynamic();
trDynamic.EntityName = numerator.Name;
QueryExpression query = new QueryExpression();
query.EntityName = numerator.Name;
query.ColumnSet = cols;
sw.WriteLine("Before iservice.RetrieveMultiple(query)");
RetrieveMultipleRequest retrieveMultiple = new RetrieveMultipleRequest();
retrieveMultiple.Query = query;
retrieveMultiple.ReturnDynamicEntities = true;
RetrieveMultipleResponse response = (RetrieveMultipleResponse)iservice.Execute(retrieveMultiple);
BusinessEntityCollection numerators = response.BusinessEntityCollection;
//The RetrievedMultiple method was used (and not the regular Retrieve method)
//since it does not require the Guid of the target entity
//BusinessEntityCollection numerators = iservice.RetrieveMultiple(query);
sw.WriteLine("numerators.BusinessEntities.Count " + numerators.BusinessEntities.Count.ToString());
sw.WriteLine();
DynamicEntity retrievedNumerator;
if (numerators.BusinessEntities.Count == 0)
{
newNumerator = NumeratorCreation(numerator, iservice);
sw.WriteLine("newNumerator " + newNumerator.ToString());
sw.WriteLine();
sw.WriteLine("Before iservice.Retrieve(numerator.Name, newNumerator, cols)");
retrievedNumerator = (DynamicEntity)iservice.Retrieve(numerator.Name, newNumerator, cols);
sw.WriteLine("After iservice.Retrieve(numerator.Name, newNumerator, cols)");
sw.WriteLine();
sw.WriteLine(" retrievedNumerator.Properties.Count " + retrievedNumerator.Properties.Count.ToString());
}
else
{
sw.WriteLine("Before (DynamicEntity)numerators.BusinessEntities[0];");
//new_numerator retrievedNumerator = (new_numerator)numerators.BusinessEntities[0];
retrievedNumerator = (DynamicEntity)numerators.BusinessEntities[0];
sw.WriteLine("After (DynamicEntity)numerators.BusinessEntities[0];");
sw.WriteLine("retrievedNumerator.Properties.Count" + retrievedNumerator.Properties.Count.ToString());
sw.WriteLine("retrievedNumerator.Name" + retrievedNumerator.Name.ToString());
sw.WriteLine();
}
// Refer to the contactid
if (context.OutputParameters.Properties.Contains("id"))
{
int donorNtor = -1;
sw.WriteLine("context.OutputParameters.Properties.Contains(id) == true");
//Updates the numerator entity
if (retrievedNumerator.Properties.Contains("new_donornumber"))
{
sw.WriteLine("retrievedNumerator.Properties.Contains(new_donornumber) == true");
if (retrievedNumerator.Properties["new_donornumber"] == null)
{
retrievedNumerator.Properties["new_donornumber"] = "0";
donorNtor = 0;
}
else
{
donorNtor = Convert.ToInt32(retrievedNumerator.Properties["new_donornumber"]);
}
}
else
{
sw.WriteLine("retrievedNumerator.Properties.Contains(new_donornumber) == false");
sw.WriteLine("E R R O R");
return;
}
//if (retrievedNumerator.new_donornumber == null)
// retrievedNumerator.new_donornumber = "0";
if (donorNtor == -1)
{
sw.WriteLine("donorNtor == -1");
sw.WriteLine("E R R O R");
return;
}
//donorNtor = int.Parse(retrievedNumerator.new_donornumber);
donorNtor++;
retrievedNumerator.Properties["new_donornumber"] = donorNtor.ToString();
sw.WriteLine();
sw.WriteLine("Before iservice.Update(retrievedNumerator)");
//update numerator entity
iservice.Update(retrievedNumerator);
sw.WriteLine("After iservice.Update(retrievedNumerator)");
sw.WriteLine();
sw.WriteLine("Before iservice.Update(contactEntity)");
//update contact entity
DynamicEntity contactEntity = new DynamicEntity();
contactEntity.Name = EntityName.contact.ToString();
Guid contactid = new Guid(context.OutputParameters.Properties["id"].ToString());
KeyProperty key = new KeyProperty();
key.Name = "contactid";
key.Value = new Key();
key.Value.Value = contactid;
contactEntity.Properties.Add(key);
StringProperty serialNumber = new StringProperty();
serialNumber.Name = "new_donorno";
serialNumber.Value = donorNtor.ToString();
contactEntity.Properties.Add(serialNumber);
iservice.Update(contactEntity);
sw.WriteLine("After iservice.Update(retrievedNumerator)");
sw.WriteLine();
//contact contact = new contact();
//contact.contactid = new Key();
//contact.contactid.Value = contactid;
//contact.new_donorno = donorNtor.ToString();
//iservice.Update(contact);
}
}
catch (System.Web.Services.Protocols.SoapException ex)
{
throw new InvalidPluginExecutionException("An error occurred in the ContactNumCreateHandler plug-in.", ex);
}
finally
{
sw.Close();
}
}
private Guid NumeratorCreation(DynamicEntity newNumerator, ICrmService iservice)
{
Guid numeratorID = new Guid();
StringProperty numeratorName = new StringProperty();
numeratorName.Name = "new_name";
numeratorName.Value = "donors";
newNumerator.Properties.Add(numeratorName);
StringProperty numeratorNumber = new StringProperty();
numeratorNumber.Name = "new_donornumber";
numeratorNumber.Value = "0";
newNumerator.Properties.Add(numeratorNumber);
numeratorID = iservice.Create(newNumerator);
return numeratorID;
}
}
}