-
-
Notifications
You must be signed in to change notification settings - Fork 210
/
FormManager.cs
201 lines (175 loc) · 7.55 KB
/
FormManager.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
using McTools.Xrm.Connection;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
namespace MsCrmTools.FormLibrariesManager.AppCode
{
internal class FormManager
{
public FormManager(IOrganizationService service)
{
Service = service;
}
public IOrganizationService Service { get; set; }
public void AddLibrary(Entity form, string libraryName, bool addFirst)
{
// Read the form xml content
var formXml = form.GetAttributeValue<string>("formxml");
var formDoc = new XmlDocument();
formDoc.LoadXml(formXml);
var formNode = formDoc.SelectSingleNode("form");
if (formNode == null)
{
throw new Exception("Expected node \"formNode\" was not found");
}
var formLibrariesNode = formNode.SelectSingleNode("formLibraries");
if (formLibrariesNode == null)
{
formLibrariesNode = formDoc.CreateElement("formLibraries");
formNode.AppendChild(formLibrariesNode);
}
// Search for existing library
var libraryNode = formLibrariesNode.SelectSingleNode(string.Format("Library[@name='{0}']", libraryName));
if (libraryNode != null)
{
throw new Exception("This library is already included in this form");
}
// Create the new library node
var nameAttribute = formDoc.CreateAttribute("name");
var libraryUniqueIdAttribute = formDoc.CreateAttribute("libraryUniqueId");
nameAttribute.Value = libraryName;
libraryUniqueIdAttribute.Value = Guid.NewGuid().ToString("B");
libraryNode = formDoc.CreateElement("Library");
if (libraryNode.Attributes != null)
{
libraryNode.Attributes.Append(nameAttribute);
libraryNode.Attributes.Append(libraryUniqueIdAttribute);
}
if (formLibrariesNode.ChildNodes.Count > 0 && addFirst)
{
formLibrariesNode.InsertBefore(libraryNode, formLibrariesNode.FirstChild);
}
else
{
formLibrariesNode.AppendChild(libraryNode);
}
// Update the form xml content
form["formxml"] = formDoc.OuterXml;
}
public List<Entity> GetAllForms(ConnectionDetail detail)
{
var qe = new QueryExpression("systemform")
{
ColumnSet = new ColumnSet(new[] { "name", "formxml", "objecttypecode" }),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression("type", ConditionOperator.In, new[] {2,7}),
new ConditionExpression("iscustomizable", ConditionOperator.Equal, true),
}
}
};
if (detail.OrganizationMajorVersion > 5)
{
qe.Criteria.Conditions.Add(new ConditionExpression("formactivationstate", ConditionOperator.Equal, 1));
}
try
{
return Service.RetrieveMultiple(qe).Entities.ToList();
}
catch
{
qe.Criteria.Conditions.RemoveAt(qe.Criteria.Conditions.Count - 1);
return Service.RetrieveMultiple(qe).Entities.ToList();
}
}
public void PublishForm(string entityName)
{
var request = new PublishXmlRequest { ParameterXml = String.Format("<importexportxml><entities><entity>{0}</entity></entities></importexportxml>", entityName) };
Service.Execute(request);
}
public bool RemoveLibrary(Entity form, string libraryName, Form parentForm)
{
// Read the form xml content
var formXml = form.GetAttributeValue<string>("formxml");
var formDoc = new XmlDocument();
formDoc.LoadXml(formXml);
var formNode = formDoc.SelectSingleNode("form");
if (formNode == null)
{
throw new Exception("Expected node \"formNode\" was not found");
}
// Retrieve events that use the library
var handlerEventNodes = formNode.SelectNodes(string.Format("events/event/Handlers/Handler[@libraryName='{0}']", libraryName));
if (handlerEventNodes != null && handlerEventNodes.Count > 0)
{
var result = DialogResult.None;
parentForm.Invoke((MethodInvoker)delegate {
result = MessageBox.Show(parentForm, string.Format(
"The library '{2}' is used by {0} event{1}. If you remove the library, {0} event{1} will be removed too\r\n\r\nDo you want to continue?",
handlerEventNodes.Count, handlerEventNodes.Count > 1 ? "s" : "", libraryName)
, "Question", MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
});
if (result == DialogResult.No)
{
return false;
}
// Remove events that were using the library
foreach (XmlNode handlerNode in handlerEventNodes)
{
// Events> <Event> <Handlers><Handler>
if (handlerNode.ParentNode.ChildNodes.Count == 1)
{
var events = formNode.SelectSingleNode("events");
if (events.ChildNodes.Count == 1)
{
// Remove Events since it only has one Event and the Event only has on handler, and the Handler is getting removed
events.ParentNode.RemoveChild(events);
}
else
{
// Remove Event since it only has one handler and it is being removed, but there are other events
events.RemoveChild(handlerNode.ParentNode.ParentNode);
}
}
else
{
// Remove only handler since there are other handlers
handlerNode.ParentNode.RemoveChild(handlerNode);
}
}
}
var formLibrariesNode = formNode.SelectSingleNode("formLibraries");
if (formLibrariesNode == null)
{
throw new Exception("Expected node \"formLibraries\" was not found");
}
// Search for existing library
var libraryNode = formLibrariesNode.SelectSingleNode(string.Format("Library[@name='{0}']", libraryName));
if (libraryNode == null)
{
throw new Exception("This library is not included in this form");
}
// Remove library
formLibrariesNode.RemoveChild(libraryNode);
if (formLibrariesNode.ChildNodes.Count == 0)
{
formLibrariesNode.ParentNode.RemoveChild(formLibrariesNode);
}
// Update the form xml content
form["formxml"] = formDoc.OuterXml;
return true;
}
public void UpdateForm(Entity form)
{
Service.Update(form);
}
}
}