Skip to content

Commit 1064c11

Browse files
authored
Merge pull request #3 from bradsymons/master
Handling of out of the box fields on entities and the ability to set a prefix
2 parents d76c5bf + c4d8d98 commit 1064c11

1 file changed

Lines changed: 80 additions & 26 deletions

File tree

src/lib/MsDyn.Contrib.CloneFieldDefinitions/CloneFieldsControl.cs

Lines changed: 80 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public class CloneFieldDefinitionsControl : PluginControlBase
2828
private ColumnHeader columnHeader4;
2929
private Button button1;
3030
private List<EntityMetadata> _entities;
31+
private ColumnHeader columnHeader5;
32+
private Label label3;
33+
private TextBox txtPrefix;
3134
private readonly List<EntityMetadata> _entitiesDetailed;
3235

3336

@@ -41,13 +44,13 @@ public CloneFieldDefinitionsControl()
4144
private void SetAvailableEntities()
4245
{
4346
_entities
44-
.Select(e => e.LogicalName)
45-
.OrderBy(e => e)
47+
.Select(e => new { e.LogicalName, e.MetadataId })
48+
.OrderBy(e => e.LogicalName)
4649
.ToList()
4750
.ForEach(name =>
4851
{
49-
comboBox1.Items.Add(name);
50-
comboBox2.Items.Add(name);
52+
comboBox1.Items.Add(name.LogicalName);
53+
comboBox2.Items.Add(name.LogicalName);
5154
});
5255
}
5356

@@ -97,26 +100,32 @@ private void InitializeComponent()
97100
this.label2 = new System.Windows.Forms.Label();
98101
this.comboBox2 = new System.Windows.Forms.ComboBox();
99102
this.button1 = new System.Windows.Forms.Button();
103+
this.label3 = new System.Windows.Forms.Label();
104+
this.txtPrefix = new System.Windows.Forms.TextBox();
100105
this.listView1 = new System.Windows.Forms.ListView();
101106
this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
102107
this.columnHeader3 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
103-
this.columnHeader4 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
104108
this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
109+
this.columnHeader4 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
110+
this.columnHeader5 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
105111
this.flowLayoutPanel2.SuspendLayout();
106112
this.SuspendLayout();
107113
//
108114
// flowLayoutPanel2
109115
//
116+
this.flowLayoutPanel2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
117+
| System.Windows.Forms.AnchorStyles.Right)));
110118
this.flowLayoutPanel2.Controls.Add(this.label1);
111119
this.flowLayoutPanel2.Controls.Add(this.comboBox1);
112120
this.flowLayoutPanel2.Controls.Add(this.label2);
113121
this.flowLayoutPanel2.Controls.Add(this.comboBox2);
114122
this.flowLayoutPanel2.Controls.Add(this.button1);
123+
this.flowLayoutPanel2.Controls.Add(this.label3);
124+
this.flowLayoutPanel2.Controls.Add(this.txtPrefix);
115125
this.flowLayoutPanel2.Location = new System.Drawing.Point(3, 3);
116126
this.flowLayoutPanel2.Name = "flowLayoutPanel2";
117-
this.flowLayoutPanel2.Size = new System.Drawing.Size(920, 39);
127+
this.flowLayoutPanel2.Size = new System.Drawing.Size(1129, 39);
118128
this.flowLayoutPanel2.TabIndex = 0;
119-
this.flowLayoutPanel2.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
120129
//
121130
// label1
122131
//
@@ -167,30 +176,47 @@ private void InitializeComponent()
167176
this.button1.UseVisualStyleBackColor = true;
168177
this.button1.Click += new System.EventHandler(this.OnCloneButtonClick);
169178
//
179+
// label3
180+
//
181+
this.label3.AutoSize = true;
182+
this.label3.Location = new System.Drawing.Point(548, 0);
183+
this.label3.Name = "label3";
184+
this.label3.Size = new System.Drawing.Size(58, 13);
185+
this.label3.TabIndex = 6;
186+
this.label3.Text = "Field Prefix";
187+
this.label3.Click += new System.EventHandler(this.label3_Click);
188+
//
189+
// txtPrefix
190+
//
191+
this.txtPrefix.Location = new System.Drawing.Point(612, 3);
192+
this.txtPrefix.Name = "txtPrefix";
193+
this.txtPrefix.Size = new System.Drawing.Size(100, 20);
194+
this.txtPrefix.TabIndex = 5;
195+
//
170196
// listView1
171197
//
172198
this.listView1.Alignment = System.Windows.Forms.ListViewAlignment.SnapToGrid;
173-
this.listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent | ColumnHeaderAutoResizeStyle.HeaderSize);
174-
this.listView1.HeaderStyle = ColumnHeaderStyle.Clickable;
175199
this.listView1.AllowColumnReorder = true;
200+
this.listView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
201+
| System.Windows.Forms.AnchorStyles.Left)
202+
| System.Windows.Forms.AnchorStyles.Right)));
176203
this.listView1.BackColor = System.Drawing.Color.WhiteSmoke;
177204
this.listView1.CheckBoxes = true;
178205
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
179206
this.columnHeader1,
180207
this.columnHeader3,
181208
this.columnHeader2,
182-
this.columnHeader4});
209+
this.columnHeader4,
210+
this.columnHeader5});
183211
this.listView1.FullRowSelect = true;
212+
this.listView1.ImeMode = System.Windows.Forms.ImeMode.On;
184213
this.listView1.Location = new System.Drawing.Point(3, 48);
185214
this.listView1.Name = "listView1";
186-
this.listView1.Size = new System.Drawing.Size(920, 443);
215+
this.listView1.Size = new System.Drawing.Size(1129, 443);
187216
this.listView1.Sorting = System.Windows.Forms.SortOrder.Ascending;
188217
this.listView1.TabIndex = 1;
189218
this.listView1.UseCompatibleStateImageBehavior = false;
190219
this.listView1.View = System.Windows.Forms.View.Details;
191-
this.listView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
192-
| System.Windows.Forms.AnchorStyles.Left)
193-
| System.Windows.Forms.AnchorStyles.Right)));
194220
//
195221
// columnHeader1
196222
//
@@ -202,28 +228,34 @@ private void InitializeComponent()
202228
this.columnHeader3.Text = "Display Name";
203229
this.columnHeader3.Width = 150;
204230
//
231+
// columnHeader2
232+
//
233+
this.columnHeader2.Text = "Type";
234+
this.columnHeader2.Width = 150;
235+
//
205236
// columnHeader4
206237
//
207238
this.columnHeader4.Text = "Description";
208-
this.columnHeader4.Width = -2;
239+
this.columnHeader4.Width = 466;
209240
//
210-
// columnHeader2
241+
// columnHeader5
211242
//
212-
this.columnHeader2.Text = "Type";
213-
this.columnHeader2.Width = 150;
243+
this.columnHeader5.Text = "Custom";
244+
this.columnHeader5.Width = 150;
214245
//
215246
// CloneFieldDefinitionsControl
216247
//
217-
this.AutoSize = true;
218-
this.Controls.AddRange(new Control[] { this.flowLayoutPanel2, this.listView1 });
219-
this.Name = "CloneFieldDefinitionsControl";
220-
this.Size = new System.Drawing.Size(932, 500);
221248
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
222249
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
250+
this.AutoSize = true;
251+
this.Controls.Add(this.flowLayoutPanel2);
252+
this.Controls.Add(this.listView1);
253+
this.Name = "CloneFieldDefinitionsControl";
254+
this.Size = new System.Drawing.Size(1141, 500);
223255
this.flowLayoutPanel2.ResumeLayout(false);
224256
this.flowLayoutPanel2.PerformLayout();
225257
this.ResumeLayout(false);
226-
this.PerformLayout();
258+
227259
}
228260

229261
private string GetDisplayLabel(Microsoft.Xrm.Sdk.Label label)
@@ -328,9 +360,16 @@ private void CloneFields(List<string> fields, string sourceEntityName, string ta
328360
for (var i = 0; i < fields.Count; i++)
329361
{
330362
var fieldName = fields[i];
331-
363+
AttributeMetadata targetAttribute = null;
332364
var attribute = sourceEntity.Attributes.Single(attr => attr.LogicalName.Equals(fieldName, StringComparison.InvariantCultureIgnoreCase));
333-
var targetAttribute = targetEntity.Attributes.SingleOrDefault(attr => attr.LogicalName.Equals(fieldName, StringComparison.InvariantCultureIgnoreCase));
365+
if (!attribute.IsCustomAttribute.Value)
366+
{
367+
targetAttribute = targetEntity.Attributes.SingleOrDefault(attr => attr.LogicalName.Equals(String.Format("{0}_{1}", txtPrefix.Text.Replace("_", ""), fieldName).ToLower(), StringComparison.InvariantCultureIgnoreCase));
368+
}
369+
else
370+
{
371+
targetAttribute = targetEntity.Attributes.SingleOrDefault(attr => attr.LogicalName.Equals(fieldName, StringComparison.InvariantCultureIgnoreCase));
372+
}
334373

335374
if (targetAttribute != null)
336375
{
@@ -352,6 +391,13 @@ private void CloneFields(List<string> fields, string sourceEntityName, string ta
352391
AttributeTypeCode.Owner
353392
}.Contains(attribute.AttributeType.Value);
354393

394+
if (!attribute.IsCustomAttribute.Value)
395+
{
396+
attribute.LogicalName = String.Format("{0}_{1}", txtPrefix.Text.Replace("_", ""), attribute.LogicalName).ToLower();
397+
attribute.SchemaName = String.Format("{0}_{1}", txtPrefix.Text.Replace("_", ""), attribute.SchemaName).ToLower();
398+
399+
}
400+
355401
if (!isEntityReference)
356402
{
357403
request = new CreateAttributeRequest
@@ -522,7 +568,9 @@ private void PopulateAttributeList()
522568
var attributes = sourceEntity.Attributes
523569
// Get only custom attributes and leave out base fields for currencies
524570
.Where(attr =>
525-
attr.IsCustomAttribute.HasValue && attr.IsCustomAttribute.Value && attr.IsValidForCreate.HasValue &&
571+
attr.IsCustomAttribute.HasValue
572+
//&& attr.IsCustomAttribute.Value
573+
&& attr.IsValidForCreate.HasValue &&
526574
attr.IsValidForCreate.Value)
527575
.Where(attr => targetEntity == null || targetEntity.Attributes.All(a => !a.LogicalName.Equals(attr.LogicalName)))
528576
.ToList();
@@ -534,9 +582,15 @@ private void PopulateAttributeList()
534582
item.SubItems.Add(GetDisplayLabel(attributeMetadata.DisplayName));
535583
item.SubItems.Add(attributeMetadata.AttributeType.ToString());
536584
item.SubItems.Add(GetDisplayLabel(attributeMetadata.Description));
585+
item.SubItems.Add(attributeMetadata.IsCustomAttribute.Value.ToString());
537586

538587
listView1.Items.Add(item);
539588
}
540589
}
590+
591+
private void label3_Click(object sender, EventArgs e)
592+
{
593+
594+
}
541595
}
542596
}

0 commit comments

Comments
 (0)