-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMetaDataHelper.cs
77 lines (66 loc) · 2.74 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
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Metadata.Query;
using Microsoft.Xrm.Sdk.Query;
using System.Text;
using System.Xml;
namespace MsCrmTools.AuditCenter
{
/// <summary>
/// Class for querying Crm Metadata
/// </summary>
internal class MetadataHelper
{
public static EntityMetadataCollection LoadEntities(IOrganizationService service)
{
var entityQueryExpression = new EntityQueryExpression
{
Properties = new MetadataPropertiesExpression("LogicalName", "DisplayName", "ObjectTypeCode")
};
var retrieveMetadataChangesRequest = new RetrieveMetadataChangesRequest
{
Query = entityQueryExpression,
ClientVersionStamp = null
};
return ((RetrieveMetadataChangesResponse)service.Execute(retrieveMetadataChangesRequest)).EntityMetadata;
}
public static EntityMetadataCollection LoadEntitiesBelowv9(IOrganizationService service)
{
var entityQueryExpression = new EntityQueryExpression
{
Properties = new MetadataPropertiesExpression("LogicalName", "DisplayName", "ObjectTypeCode")
};
var retrieveMetadataChangesRequest = new RetrieveMetadataChangesRequest
{
Query = entityQueryExpression,
ClientVersionStamp = null
};
return ((RetrieveMetadataChangesResponse)service.Execute(retrieveMetadataChangesRequest)).EntityMetadata;
}
/// <summary>
/// Retrieves main forms for the specified entity
/// </summary>
/// <param name="logicalName">Entity logical name</param>
/// <param name="oService">Crm organization service</param>
/// <returns>Document containing all forms definition</returns>
public static XmlDocument RetrieveEntityForms(string logicalName, IOrganizationService oService)
{
QueryByAttribute qba = new QueryByAttribute("systemform");
qba.Attributes.AddRange("objecttypecode", "type");
qba.Values.AddRange(logicalName, 2);
qba.ColumnSet = new ColumnSet(true);
EntityCollection ec = oService.RetrieveMultiple(qba);
StringBuilder allFormsXml = new StringBuilder();
allFormsXml.Append("<root>");
foreach (Entity form in ec.Entities)
{
allFormsXml.Append(form["formxml"]);
}
allFormsXml.Append("</root>");
XmlDocument docAllForms = new XmlDocument();
docAllForms.LoadXml(allFormsXml.ToString());
return docAllForms;
}
}
}