-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathEntityReferenceExtensions.cs
66 lines (59 loc) · 2.38 KB
/
EntityReferenceExtensions.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
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Query;
namespace Xrm
{
public static partial class EntityReferenceExtensions
{
/// <summary>
/// Retrieve entity from EntityReference
/// </summary>
/// <param name="reference">EntityReference</param>
/// <param name="columns">Columns to retrieve</param>
/// <param name="service">IOrganizationService</param>
/// <typeparam name="T">Early bound entity type</typeparam>
/// <returns></returns>
public static T ToEntity<T>(this EntityReference reference, ColumnSet columns, IOrganizationService service) where T : Entity
{
if (reference == null)
{
return null;
}
return (T)service.Retrieve(reference.LogicalName, reference.Id, columns);
}
/// <summary>
/// Retrieve entity metadata
/// </summary>
/// <param name="reference">EntityReference to retrieve metadata for</param>
/// <param name="service">IOrganizationService</param>
/// <returns>RetrieveEntityResponse</returns>
public static RetrieveEntityResponse GetTableMetadata(this EntityReference reference, IOrganizationService service)
{
var request = new RetrieveEntityRequest
{
EntityFilters = EntityFilters.Attributes,
LogicalName = reference.LogicalName
};
return (RetrieveEntityResponse)service.Execute(request);
}
/// <summary>
/// Calculate rollup field for entity
/// </summary>
/// <param name="entity">Entity to calculate</param>
/// <param name="columnName">Column to calculate</param>
/// <param name="service">Organization service</param>
/// <returns>Calculate rollup response</returns> <summary>
public static T CalculateRollup<T>(this EntityReference entity, string columnName, IOrganizationService service)
{
var request = new CalculateRollupFieldRequest
{
Target = entity,
FieldName = columnName
};
var response = (CalculateRollupFieldResponse)service.Execute(request);
return (T)response.Entity[columnName];
}
}
}