public class Plugin IPlugin StreamWriter sw private string crmUrl publ

 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
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
{
DynamicEntity numerator = new DynamicEntity();
numerator.Name = "new_numerator";
ICrmService iservice = (ICrmService)context.CreateCrmService(true);
AllColumns cols = new AllColumns(); // пробовал и ColumnSet cols = new ColumnSet();
// cols.AddColumns(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;
DynamicEntity retrievedNumerator = (DynamicEntity)numerators.BusinessEntities[0];