CrmPluginBase2015 1.0.0.11

This package provides ability for easy CRM plugins creation with focus on what you need to do and out of pain InputParameters/OutputParameters parsing.
See example of use on project site: https://github.com/abelevtsov/CrmPluginBase2015.

There is a newer version of this package available.
See the version list below for details.
Install-Package CrmPluginBase2015 -Version 1.0.0.11
dotnet add package CrmPluginBase2015 --version 1.0.0.11
<PackageReference Include="CrmPluginBase2015" Version="1.0.0.11" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CrmPluginBase2015 --version 1.0.0.11
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: CrmPluginBase2015, 1.0.0.11"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install CrmPluginBase2015 as a Cake Addin
#addin nuget:?package=CrmPluginBase2015&version=1.0.0.11

// Install CrmPluginBase2015 as a Cake Tool
#tool nuget:?package=CrmPluginBase2015&version=1.0.0.11
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

CrmPluginBase

Basic functionality for MS Dynamics CRM 2015 plugins - see example.

Provides ability for easy CRM plugins creation with focus on what you need to do and
out of pain InputParameters/OutputParameters parsing.

Install package from nuget and enjoy!

IMPORTANT NOTE:

you must name your PreEntityImage as preimage and PostEntityImage as postimage exactly

see ParametersWrapper .ctor.

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);
            }
        }
    }

    // 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;
        }
    }
}

CrmPluginBase

Basic functionality for MS Dynamics CRM 2015 plugins - see example.

Provides ability for easy CRM plugins creation with focus on what you need to do and
out of pain InputParameters/OutputParameters parsing.

Install package from nuget and enjoy!

IMPORTANT NOTE:

you must name your PreEntityImage as preimage and PostEntityImage as postimage exactly

see ParametersWrapper .ctor.

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);
            }
        }
    }

    // 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;
        }
    }
}

Release Notes

fix MessageName from different contexts

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
2.0.0 410 8/12/2019
1.0.0.11 646 7/30/2017
1.0.0.10 555 7/30/2017
1.0.0.9 605 7/8/2017
1.0.0.8 551 7/8/2017
1.0.0.7 594 7/8/2017
1.0.0.6 657 11/11/2016
1.0.0.5 616 11/11/2016
1.0.0.4 584 11/11/2016
1.0.0.3 697 10/19/2016
1.0.0.2 590 10/13/2016
1.0.0.1 764 7/13/2016
1.0.0 756 7/13/2016