Skip to main content

Notifications

Announcements

No record found.

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

  • Suggested answer
    Emad Hanna Profile Picture
    Emad Hanna on at
    RE: Creating a simple plugin

    Thank You, that worked and my plugin code worked.  It turns out there was another plugin that was causing the error, once I removed it it worked.

  • Verified answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Creating a simple plugin

    There should be nothing special. Try following code:

    using System;
    using System.ServiceModel;
    using Microsoft.Xrm.Sdk;
    
    namespace Microsoft.Crm.Sdk.Samples
    {
       public class UpdateDescription : IPlugin
       {
           public void Execute(IServiceProvider serviceProvider)
           {
               ITracingService tracingService =
    
                   (ITracingService)serviceProvider.GetService(typeof(ITracingService));
               IPluginExecutionContext context = (IPluginExecutionContext)
                   serviceProvider.GetService(typeof(IPluginExecutionContext));
               if (context.InputParameters.Contains("Target") &&
                   context.InputParameters["Target"] is Entity)
               {
                   Entity entity = (Entity)context.InputParameters["Target"];
                   if (entity.LogicalName == "account")
                   {
                           entity["description"] = "Important Account Information here";
                   }
               }
           }
       }
    }


  • Emad Hanna Profile Picture
    Emad Hanna on at
    RE: Creating a simple plugin

    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>

  • a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Creating a simple plugin

    Ok, and what about error that you get?

  • Emad Hanna Profile Picture
    Emad Hanna on at
    RE: Creating a simple plugin

    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
    a33ik 84,325 Most Valuable Professional on at
    RE: Creating a simple plugin

    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
    Emad Hanna on at
    RE: Creating a simple plugin

    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>

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,325 Most Valuable Professional on at
    RE: Creating a simple plugin

    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
    Emad Hanna on at
    RE: Creating a simple plugin

    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>

  • Emad Hanna Profile Picture
    Emad Hanna on at
    RE: Creating a simple plugin

    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>

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,494 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,309 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans