This repository was archived by the owner on May 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathExamples.cs
More file actions
116 lines (103 loc) · 4.31 KB
/
Copy pathExamples.cs
File metadata and controls
116 lines (103 loc) · 4.31 KB
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
using System;
using System.Runtime.CompilerServices;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using CrmPluginBase.Exceptions;
using ProxyClasses;
namespace CrmPluginBase.Examples
{
// ReSharper disable once RedundantExtendsListEntry
// ReSharper disable once ClassNeverInstantiated.Global
/// <summary>
/// A simple example of CrmPluginBase usage with proxy class entity new_search.
/// Certainly you can use any of your own proxy classes or even just Entity as generic type parameter (<see cref="TraceAllUpdateOperationsPlugin"/>)
/// </summary>
public class DenyActiveRecordDeletionPlugin : CrmPlugin<new_search>, IPlugin
{
/// <summary>
/// Deny deletion of active new_search entities
/// </summary>
/// <exception cref="CrmException">Deletion of active new_search records is forbidden!</exception>
public override void OnDelete(IPluginExecutionContext context, string entityName, Guid primaryEntityId, new_search preEntityImage)
{
if (preEntityImage.statuscode == new_searchStatus.Active_Active)
{
throw new CrmException($"Deletion of active {new_search.EntityLogicalName} records is forbidden!", expected: true);
}
}
protected sealed override string PreEntityImageName => "MyCustomPreEntityImage";
}
// ReSharper disable once RedundantExtendsListEntry
// ReSharper disable once ClassNeverInstantiated.Global
/// <summary>
/// A simple example of CrmPluginBase usage with Entity as generic type parameter
/// </summary>
public class TraceAllUpdateOperationsPlugin : CrmPlugin<Entity>, IPlugin
{
public override void OnUpdate(IPluginExecutionContext context, Entity entity, Guid primaryEntityId)
{
var message = $"Entity '{entity.LogicalName}', Id = '{primaryEntityId}' updated";
var traceEntity =
new Entity("new_trace")
{
["new_tracemessage"] = message
};
SystemOrgService.Create(traceEntity);
TracingService.Trace(message);
}
}
public class RestrictAccountsExportToExcelPlugin : CrmPlugin<Account>, IPlugin
{
public override void OnExportToExcel(IPluginExecutionContext context, QueryBase query, EntityCollection entityCollection)
{
if (!UserAvailableForExportAccountsToExcel(context.UserId))
{
throw new CrmException("You can't export accounts to Excel", expected: true);
}
var queryExpression = ToQueryExpression(query);
if (queryExpression == null)
{
return;
}
// note: Add your own conditions here
queryExpression.Criteria.AddCondition("donotpostalmail", ConditionOperator.Equal, false);
// ReSharper disable once RedundantAssignment
query = queryExpression;
}
/// <summary>
/// Just to show a possibility of custom exception handling in pipeline
/// </summary>
protected override void OnException(Exception ex)
{
// ToDo: paste your custom exception handling logic here (log exception for example)
base.OnException(ex);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private QueryExpression ToQueryExpression(QueryBase query)
{
switch (query)
{
case QueryExpression queryExpression:
return queryExpression;
case FetchExpression fetchExpression:
{
var request =
new FetchXmlToQueryExpressionRequest
{
FetchXml = fetchExpression.Query
};
return ((FetchXmlToQueryExpressionResponse)SystemOrgService.Execute(request)).Query;
}
default:
return null;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool UserAvailableForExportAccountsToExcel(Guid userId)
{
// note: Paste your custom check logic here
return false;
}
}
}