web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Creating a simple plugin

(0) ShareShare
ReportReport
Posted on by

Hello Creating a simple plugin, but I get an error.  Register on pre-event.

//<snippetFollowupPlugin>
using System;
using System.ServiceModel;

// Microsoft Dynamics CRM namespace(s)
using Microsoft.Xrm.Sdk;

namespace Microsoft.Crm.Sdk.Samples
{
public class UpdateDescription : IPlugin
{
/// <summary>
/// A plug-in that creates a follow-up task activity when a new account is created.
/// </summary>
/// <remarks>Register this plug-in on the Create message, account entity,
/// and asynchronous mode.
/// </remarks>
public void Execute(IServiceProvider serviceProvider)
{
//Extract the tracing service for use in debugging sandboxed plug-ins.
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));

//<snippetFollowupPlugin1>
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
//</snippetFollowupPlugin1>

//<snippetFollowupPlugin2>
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parameters.
Entity entity = (Entity)context.InputParameters["Target"];
//</snippetFollowupPlugin2>

// Verify that the target entity represents an account.
// If not, this plug-in was not registered correctly.
if (entity.LogicalName == "account")
{
try
{


entity["description"] = "Important Account Information here";

//<snippetFollowupPlugin4>
// Obtain the organization service reference.
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
//</snippetFollowupPlugin4>

// Create the task in Microsoft Dynamics CRM.
tracingService.Trace("Plugin:added Description");
service.Update(entity);
}
//<snippetFollowupPlugin3>
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("An error occurred in the FollowupPlugin plug-in.", ex);
}
//</snippetFollowupPlugin3>

catch (Exception ex)
{
tracingService.Trace("Description Plugin: {0}", ex.ToString());
throw;
}
return;
}
}
}
}
}

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Adrian Begovich Profile Picture
    1,027 Moderator on at

    Hi Emad Hanna,

    What error are you receiving?

    This article will assist you with Plugin Development.

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    1. What error do you get?

    2. Remove service.Update(entity); line - it has no sense because your code runs pre-operation.

  • Emad Hanna Profile Picture
    on at

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: An error occurred in the FollowupPlugin plug-in.Detail:

    <OrganizationServiceFault xmlns:i="www.w3.org/.../XMLSchema-instance&quot; xmlns="schemas.microsoft.com/.../Contracts&quot;>

     <ActivityId>29b9471e-6cb4-4c5f-a005-d5eaec34c715</ActivityId>

     <ErrorCode>-2147220891</ErrorCode>

     <ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic&quot;>

       <KeyValuePairOfstringanyType>

         <d2p1:key>OperationStatus</d2p1:key>

         <d2p1:value xmlns:d4p1="www.w3.org/.../XMLSchema&quot; i:type="d4p1:string">0</d2p1:value>

       </KeyValuePairOfstringanyType>

       <KeyValuePairOfstringanyType>

         <d2p1:key>SubErrorCode</d2p1:key>

         <d2p1:value xmlns:d4p1="www.w3.org/.../XMLSchema&quot; i:type="d4p1:string">-2146233088</d2p1:value>

       </KeyValuePairOfstringanyType>

     </ErrorDetails>

     <Message>An error occurred in the FollowupPlugin plug-in.</Message>

     <Timestamp>2018-10-19T18:52:59.8005406Z</Timestamp>

     <ExceptionRetriable>false</ExceptionRetriable>

     <ExceptionSource i:nil="true" />

     <InnerFault i:nil="true" />

     <OriginalException i:nil="true" />

     <TraceText>

    [ClassLibrary5: Microsoft.Crm.Sdk.Samples.FollowupPlugin]

    [6ddc6ed5-d6b2-e811-815d-e0071b6af211: Microsoft.Crm.Sdk.Samples.FollowupPlugin: Create of account]

    FollowupPlugin: Creating the task activity.

    </TraceText>

    </OrganizationServiceFault>

  • Emad Hanna Profile Picture
    on at

    2- I did, still get this error.

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: An error occurred in the FollowupPlugin plug-in.Detail:

    <OrganizationServiceFault xmlns:i="www.w3.org/.../XMLSchema-instance&quot; xmlns="schemas.microsoft.com/.../Contracts&quot;>

     <ActivityId>b4b47a35-a10c-4dfb-8e47-1f06322a7018</ActivityId>

     <ErrorCode>-2147220891</ErrorCode>

     <ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic&quot;>

       <KeyValuePairOfstringanyType>

         <d2p1:key>OperationStatus</d2p1:key>

         <d2p1:value xmlns:d4p1="www.w3.org/.../XMLSchema&quot; i:type="d4p1:string">0</d2p1:value>

       </KeyValuePairOfstringanyType>

       <KeyValuePairOfstringanyType>

         <d2p1:key>SubErrorCode</d2p1:key>

         <d2p1:value xmlns:d4p1="www.w3.org/.../XMLSchema&quot; i:type="d4p1:string">-2146233088</d2p1:value>

       </KeyValuePairOfstringanyType>

     </ErrorDetails>

     <Message>An error occurred in the FollowupPlugin plug-in.</Message>

     <Timestamp>2018-10-19T20:00:56.6364465Z</Timestamp>

     <ExceptionRetriable>false</ExceptionRetriable>

     <ExceptionSource i:nil="true" />

     <InnerFault i:nil="true" />

     <OriginalException i:nil="true" />

     <TraceText>

    [ClassLibrary5: Microsoft.Crm.Sdk.Samples.FollowupPlugin]

    [6ddc6ed5-d6b2-e811-815d-e0071b6af211: Microsoft.Crm.Sdk.Samples.FollowupPlugin: Create of account]

    FollowupPlugin: Creating the task activity.

    </TraceText>

    </OrganizationServiceFault>

  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Remove following catch statement - anyway it doesn't make any sense:

    catch (FaultException<OrganizationServiceFault> ex)

    {

    throw new InvalidPluginExecutionException("An error occurred in the FollowupPlugin plug-in.", ex);

    }

    rebuild, redeploy, run, bring log here.

  • Emad Hanna Profile Picture
    on at

    okay did that and here is the log.

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: An error occurred in the FollowupPlugin plug-in.Detail:

    <OrganizationServiceFault xmlns:i="www.w3.org/.../XMLSchema-instance&quot; xmlns="schemas.microsoft.com/.../Contracts&quot;>

     <ActivityId>28abca32-8ed0-4a10-b723-4b98885094f8</ActivityId>

     <ErrorCode>-2147220891</ErrorCode>

     <ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic&quot;>

       <KeyValuePairOfstringanyType>

         <d2p1:key>OperationStatus</d2p1:key>

         <d2p1:value xmlns:d4p1="www.w3.org/.../XMLSchema&quot; i:type="d4p1:string">0</d2p1:value>

       </KeyValuePairOfstringanyType>

       <KeyValuePairOfstringanyType>

         <d2p1:key>SubErrorCode</d2p1:key>

         <d2p1:value xmlns:d4p1="www.w3.org/.../XMLSchema&quot; i:type="d4p1:string">-2146233088</d2p1:value>

       </KeyValuePairOfstringanyType>

     </ErrorDetails>

     <Message>An error occurred in the FollowupPlugin plug-in.</Message>

     <Timestamp>2018-10-19T20:11:19.5284019Z</Timestamp>

     <ExceptionRetriable>false</ExceptionRetriable>

     <ExceptionSource i:nil="true" />

     <InnerFault i:nil="true" />

     <OriginalException i:nil="true" />

     <TraceText>

    [ClassLibrary5: Microsoft.Crm.Sdk.Samples.FollowupPlugin]

    [6ddc6ed5-d6b2-e811-815d-e0071b6af211: Microsoft.Crm.Sdk.Samples.FollowupPlugin: Create of account]

    FollowupPlugin: Creating the task activity.

    </TraceText>

    </OrganizationServiceFault>

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    If you did that there should be "An error occurred in the FollowupPlugin plug-in." message. Please provide your current code.

  • Emad Hanna Profile Picture
    on at

    Here is the latest code.

    using System;

    using System.ServiceModel;

    // Microsoft Dynamics CRM namespace(s)

    using Microsoft.Xrm.Sdk;

    namespace Microsoft.Crm.Sdk.Samples

    {

       public class UpdateDescription : IPlugin

       {

           /// <summary>

           /// A plug-in that creates a follow-up task activity when a new account is created.

           /// </summary>

           /// <remarks>Register this plug-in on the Create message, account entity,

           /// and asynchronous mode.

           /// </remarks>

           public void Execute(IServiceProvider serviceProvider)

           {

               //Extract the tracing service for use in debugging sandboxed plug-ins.

               ITracingService tracingService =

                   (ITracingService)serviceProvider.GetService(typeof(ITracingService));

               //<snippetFollowupPlugin1>

               // Obtain the execution context from the service provider.

               IPluginExecutionContext context = (IPluginExecutionContext)

                   serviceProvider.GetService(typeof(IPluginExecutionContext));

               //</snippetFollowupPlugin1>

               //<snippetFollowupPlugin2>

               // The InputParameters collection contains all the data passed in the message request.

               if (context.InputParameters.Contains("Target") &&

                   context.InputParameters["Target"] is Entity)

               {

                   // Obtain the target entity from the input parameters.

                   Entity entity = (Entity)context.InputParameters["Target"];

                   //</snippetFollowupPlugin2>

                   // Verify that the target entity represents an account.

                   // If not, this plug-in was not registered correctly.

                   if (entity.LogicalName == "account")

                   {

                       try

                       {

                           entity["description"] = "Important Account Information here";

                           //<snippetFollowupPlugin4>

                           // Obtain the organization service reference.

                           IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

                           IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                           //</snippetFollowupPlugin4>

                           // Create the task in Microsoft Dynamics CRM.

                           tracingService.Trace("Plugin:added Description");

                          // service.Update(entity);

                       }

                       catch (Exception ex)

                       {

                           tracingService.Trace("Description Plugin: {0}", ex.ToString());

                           throw;

                       }

                       return;

                   }

               }

           }

       }

    }

    //</snippetFollowupPlugin>

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Ok, and what about error that you get?

  • Emad Hanna Profile Picture
    on at

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: An error occurred in the FollowupPlugin plug-in.Detail:

    <OrganizationServiceFault xmlns:i="www.w3.org/.../XMLSchema-instance&quot; xmlns="schemas.microsoft.com/.../Contracts&quot;>

     <ActivityId>98d9b707-f832-440e-ba93-0de649a19c20</ActivityId>

     <ErrorCode>-2147220891</ErrorCode>

     <ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic&quot;>

       <KeyValuePairOfstringanyType>

         <d2p1:key>OperationStatus</d2p1:key>

         <d2p1:value xmlns:d4p1="www.w3.org/.../XMLSchema&quot; i:type="d4p1:string">0</d2p1:value>

       </KeyValuePairOfstringanyType>

       <KeyValuePairOfstringanyType>

         <d2p1:key>SubErrorCode</d2p1:key>

         <d2p1:value xmlns:d4p1="www.w3.org/.../XMLSchema&quot; i:type="d4p1:string">-2146233088</d2p1:value>

       </KeyValuePairOfstringanyType>

     </ErrorDetails>

     <Message>An error occurred in the FollowupPlugin plug-in.</Message>

     <Timestamp>2018-10-19T20:29:41.1713798Z</Timestamp>

     <ExceptionRetriable>false</ExceptionRetriable>

     <ExceptionSource i:nil="true" />

     <InnerFault i:nil="true" />

     <OriginalException i:nil="true" />

     <TraceText>

    [ClassLibrary5: Microsoft.Crm.Sdk.Samples.FollowupPlugin]

    [6ddc6ed5-d6b2-e811-815d-e0071b6af211: Microsoft.Crm.Sdk.Samples.FollowupPlugin: Create of account]

    FollowupPlugin: Creating the task activity.

    </TraceText>

    </OrganizationServiceFault>

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
AS-17030037-0 Profile Picture

AS-17030037-0 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans