forked from abelevtsov/CrmPluginBase2015
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExamples.cs
98 lines (87 loc) · 3.71 KB
/
Examples.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
using System;
using System.Runtime.CompilerServices;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using CrmPluginBase;
using CrmPluginBase.Exceptions;
using ProxyClasses;
namespace PluginTests
{
// ReSharper disable once RedundantExtendsListEntry
// ReSharper disable once ClassNeverInstantiated.Global
/// <summary>
/// A simple example of CrmPluginBase usage with proxy class entity rare_search.
/// Of course you can use any your own proxy class or even just Entity as generic type parameter (<see cref="TraceAllUpdateOperationsPlugin"/>)
/// </summary>
public class DenyActiveRecordDeletionPlugin : CrmPlugin<rare_search>, IPlugin
{
/// <summary>
/// Deny deletion of active rare_search entities
/// </summary>
/// <exception cref="CrmException">Deletion of active rare_search records is forbidden!</exception>
public override void OnDelete(IPluginExecutionContext context, string entityName, Guid primaryEntityId, rare_search preEntityImage)
{
if (preEntityImage.statuscode == rare_searchStatus.Активный_Активный)
{
throw new CrmException("Deletion of active rare_search records is forbidden!", expected: true);
}
}
}
// 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 = string.Format("Entity '{0}', Id = '{1}' updated", entity.LogicalName, primaryEntityId);
var traceEntity = new Entity("rare_trace");
traceEntity["rare_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;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private QueryExpression ToQueryExpression(QueryBase query)
{
var fetchExpression = query as FetchExpression;
if (fetchExpression == null)
{
return query as QueryExpression;
}
var request =
new FetchXmlToQueryExpressionRequest
{
FetchXml = fetchExpression.Query
};
return ((FetchXmlToQueryExpressionResponse)SystemOrgService.Execute(request)).Query;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool UserAvailableForExportAccountsToExcel(Guid userId)
{
// note: Paste your custom check logic here
return false;
}
}
}