-
Notifications
You must be signed in to change notification settings - Fork 0
/
DependencySearchControl.cs
162 lines (148 loc) · 5.34 KB
/
DependencySearchControl.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using XrmToolBox.Extensibility;
using Microsoft.Crm.Sdk.Messages;
using Newtonsoft.Json;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace DependencySearch {
public partial class DependencySearchControl : PluginControlBase {
protected List<OptionSetLabel> ComponentTypes = new List<OptionSetLabel>();
protected List<OptionSetLabel> DependencyTypes = new List<OptionSetLabel>();
public DependencySearchControl() {
InitializeComponent();
cbRequest.Items.Add("RetrieveDependenciesForDeleteRequest");
cbRequest.Items.Add("RetrieveDependenciesForUninstallRequest");
cbRequest.Items.Add("RetrieveDependentComponentsRequest");
btnExecute.Enabled = false;
this.WorkAsync(new WorkAsyncInfo("Getting types...", (a) => {
var comptypes = GetOptionSetValue("componenttype");
ComponentTypes.AddRange(comptypes.Options.OrderBy(o => o.Label.LocalizedLabels.First().Label).Select(osv => new OptionSetLabel() {
Id = osv.Value ?? -1,
Name = osv.Label.LocalizedLabels.First().Label
}));
foreach (var c in ComponentTypes) {
this.Invoke((MethodInvoker)delegate {
cbType.Items.Add(c);
});
}
var deptypes = GetOptionSetValue("dependencytype");
DependencyTypes.AddRange(deptypes.Options.OrderBy(o => o.Label.LocalizedLabels.First().Label).Select(osv => new OptionSetLabel() {
Id = osv.Value ?? -1,
Name = osv.Label.LocalizedLabels.First().Label
}));
}));
}
public OptionSetMetadata GetOptionSetValue(string name) {
try {
var resp = this.ConnectionDetail.ServiceClient.Execute(new RetrieveOptionSetRequest() {
Name = name
});
if (resp is RetrieveOptionSetResponse) {
var rosr = resp as RetrieveOptionSetResponse;
return (rosr.OptionSetMetadata as OptionSetMetadata);
}
}
catch (Exception e) {
this.Invoke((MethodInvoker)delegate {
rtbResponse.Text = JsonConvert.SerializeObject(e, Formatting.Indented);
});
}
finally {
this.Invoke((MethodInvoker)delegate {
btnExecute.Enabled = true;
});
}
return null;
}
public void RequestThings() {
try {
int type = -1;
string text = "";
string req = "";
this.Invoke((MethodInvoker)delegate {
type = (cbType.SelectedItem as OptionSetLabel).Id;
req = (string)cbRequest.SelectedItem;
text = tbGuid.Text;
});
Microsoft.Xrm.Sdk.OrganizationRequest request = null;
if (req == "RetrieveDependenciesForDeleteRequest") {
request = new RetrieveDependenciesForDeleteRequest() {
ObjectId = new Guid(text),
ComponentType = type
};
}
if (req == "RetrieveDependenciesForUninstallRequest") {
request = new RetrieveDependenciesForUninstallRequest() {
SolutionUniqueName = text,
};
}
if (req == "RetrieveDependentComponentsRequest") {
request = new RetrieveDependentComponentsRequest() {
ObjectId = new Guid(text),
};
}
var resp = this.ConnectionDetail.ServiceClient.Execute(request);
this.Invoke((MethodInvoker)delegate {
rtbResponse.Text = JsonConvert.SerializeObject(resp, Formatting.Indented);
EntityCollection ec = null;
if (resp is RetrieveDependenciesForDeleteResponse) {
ec = (resp as RetrieveDependenciesForDeleteResponse).EntityCollection;
}
if (ec != null) {
rtbResponse.Text = "";
foreach (var entity in ec.Entities) {
if (entity.LogicalName == "dependency") {
//rtbResponse.Text += JsonConvert.SerializeObject(entity.Attributes, Formatting.Indented);
//var dep = this.ConnectionDetail.ServiceClient.Retrieve("solutioncomponent", entity.GetAttributeValue<Guid>("dependentcomponentobjectid"), new Microsoft.Xrm.Sdk.Query.ColumnSet(true));
var compType = ComponentTypes.FirstOrDefault(ct => ct.Id == entity.GetAttributeValue<OptionSetValue>("dependentcomponenttype").Value);
var objId = entity.GetAttributeValue<Guid>("dependentcomponentobjectid");
rtbResponse.Text += $"[{compType}] ";
if (compType == ComponentTypes.FirstOrDefault(ct => ct.Name == "Workflow")) {
var workflow = this.ConnectionDetail.ServiceClient.Retrieve("workflow", objId, new ColumnSet(true));
rtbResponse.Text += ": '" + workflow.GetAttributeValue<string>("name") + "'";
}
rtbResponse.Text += $" {{{objId}}}" + Environment.NewLine;
}
}
if (!ec.Entities.Any()) {
rtbResponse.Text += "No Dependencies.";
}
}
});
}
catch (Exception e) {
this.Invoke((MethodInvoker)delegate {
rtbResponse.Text = JsonConvert.SerializeObject(e, Formatting.Indented);
});
}
finally {
this.Invoke((MethodInvoker)delegate {
btnExecute.Enabled = true;
});
}
}
private void btnExecute_Click(object sender, EventArgs e) {
btnExecute.Enabled = false;
if (tbGuid.Text.Any() && cbRequest.SelectedItem != null) {
rtbResponse.Text = "";
this.WorkAsync(new WorkAsyncInfo("Requesting...", (a) => {
RequestThings();
}));
}
else {
rtbResponse.Text = "Please ensure you have an ID and Request Type set.";
btnExecute.Enabled = true;
}
}
}
}