-
Notifications
You must be signed in to change notification settings - Fork 8
/
MetadataHelper.cs
104 lines (92 loc) · 4.07 KB
/
MetadataHelper.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
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Metadata.Query;
using System;
using System.Collections.Generic;
namespace Rappen.XTB.AutoNumManager
{
public static class MetadataHelper
{
#region Public Fields
public static String[] attributeProperties = { "DisplayName", "Description", "AttributeType", "IsManaged", "IsCustomizable", "IsCustomAttribute", "IsValidForCreate", "IsPrimaryName", "SchemaName", "AutoNumberFormat", "MaxLength" };
public static String[] entityDetails = { "Attributes" };
public static String[] entityProperties = { "LogicalName", "DisplayName", "PrimaryNameAttribute", "ObjectTypeCode", "IsManaged", "IsCustomizable", "IsCustomEntity", "IsIntersect", "IsValidForAdvancedFind" };
#endregion Public Fields
#region Private Fields
private static Dictionary<string, EntityMetadata> entities = new Dictionary<string, EntityMetadata>();
#endregion Private Fields
#region Public Methods
public static AttributeMetadata GetAttribute(IOrganizationService service, string entity, string attribute, object value)
{
if (value is AliasedValue)
{
var aliasedValue = value as AliasedValue;
entity = aliasedValue.EntityLogicalName;
attribute = aliasedValue.AttributeLogicalName;
}
return GetAttribute(service, entity, attribute);
}
public static AttributeMetadata GetAttribute(IOrganizationService service, string entity, string attribute)
{
if (!entities.ContainsKey(entity))
{
var response = LoadEntityDetails(service, entity);
if (response != null && response.EntityMetadata != null && response.EntityMetadata.Count == 1 && response.EntityMetadata[0].LogicalName == entity)
{
entities.Add(entity, response.EntityMetadata[0]);
}
}
if (entities != null && entities.ContainsKey(entity))
{
if (entities[entity].Attributes != null)
{
foreach (var metaattribute in entities[entity].Attributes)
{
if (metaattribute.LogicalName == attribute)
{
return metaattribute;
}
}
}
}
return null;
}
public static RetrieveMetadataChangesResponse LoadEntities(IOrganizationService service)
{
if (service == null)
{
return null;
}
var eqe = new EntityQueryExpression();
eqe.Properties = new MetadataPropertiesExpression(entityProperties);
var req = new RetrieveMetadataChangesRequest()
{
Query = eqe,
ClientVersionStamp = null
};
return service.Execute(req) as RetrieveMetadataChangesResponse;
}
public static RetrieveMetadataChangesResponse LoadEntityDetails(IOrganizationService service, string entityName)
{
if (service == null)
{
return null;
}
var eqe = new EntityQueryExpression();
eqe.Properties = new MetadataPropertiesExpression(entityProperties);
eqe.Properties.PropertyNames.AddRange(entityDetails);
eqe.Criteria.Conditions.Add(new MetadataConditionExpression("LogicalName", MetadataConditionOperator.Equals, entityName));
var aqe = new AttributeQueryExpression();
aqe.Properties = new MetadataPropertiesExpression(attributeProperties);
eqe.AttributeQuery = aqe;
var req = new RetrieveMetadataChangesRequest()
{
Query = eqe,
ClientVersionStamp = null
};
return service.Execute(req) as RetrieveMetadataChangesResponse;
}
#endregion Public Methods
}
}