-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntegrationPointPlugin.cs
More file actions
65 lines (60 loc) · 3.52 KB
/
Copy pathIntegrationPointPlugin.cs
File metadata and controls
65 lines (60 loc) · 3.52 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
// <copyright file="IntegrationPointPlugin.cs" company="Korus Consulting">
// Copyright (c) 2017 All Rights Reserved
// </copyright>
// <author>Helena Makarchuk</author>
// <date>07.05.2017</date>
// <summary>Implements the IntegrationPointPlugin Plugin.</summary>
namespace DevTest.CRM_Integration_Plugins
{
using System;
using Microsoft.Xrm.Sdk;
/// <summary>
/// Плагин реализует запрет на ручное создание и изменение записи сущности «Точка интеграции»
/// </summary>
public class IntegrationPointPlugin : Plugin
{
private readonly string preImageAlias = "PreImage";
public IntegrationPointPlugin()
: base(typeof(IntegrationPointPlugin))
{
base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(20, "Create", "new_integrationpoint", new Action<LocalPluginContext>(ExecuteIntegrationPointPlugin)));
base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(20, "Update", "new_integrationpoint", new Action<LocalPluginContext>(ExecuteIntegrationPointPlugin)));
}
protected void ExecuteIntegrationPointPlugin(LocalPluginContext localContext)
{
if (localContext == null) throw new ArgumentNullException("localContext");
var context = localContext.PluginExecutionContext;
var service = localContext.OrganizationService;
Entity targetEntity = (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) ? (Entity)context.InputParameters["Target"] : null;
Entity preImageEntity = (context.PostEntityImages != null && context.PreEntityImages.Contains(this.preImageAlias)) ? context.PreEntityImages[this.preImageAlias] : null;
try
{
if (context.Stage == 20 && (context.MessageName == "Create" || context.MessageName == "Update") && targetEntity != null)
{
if (context.MessageName == "Create")
{
if ((targetEntity.Contains("new_changing_enabled") && (bool)targetEntity["new_changing_enabled"] != true)
|| !targetEntity.Contains("new_changing_enabled"))
{
throw new Exception("Изменять/создавать запись можно только в модуле \"CRM_Integration_Solution\"");
}
}
else if (context.MessageName == "Update")
{
if ((targetEntity.Contains("new_changing_enabled") && (bool)targetEntity["new_changing_enabled"] != true)
|| (!targetEntity.Contains("new_changing_enabled") && preImageEntity != null
&& ((preImageEntity.Contains("new_changing_enabled") && (bool)preImageEntity["new_changing_enabled"] != true)
|| !preImageEntity.Contains("new_changing_enabled"))))
{
throw new Exception("Изменять/создавать запись можно только в модуле \"CRM_Integration_Solution\"");
}
}
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
}
}
}