locked
Write plugin on publish of an entity RRS feed

  • Question

  • Hi,

    I've created plugin for CRM 2011 there is nothing I am doing in the plugin only adding the required services and I've register the plugin with following of the information:-

    Message:- Publish
    Primary Entity:- None
    Secondary Entity:- None

    But it generates the following exception .

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
       at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
       at Saved_Query_on_Publish.Class1.Execute(IServiceProvider isp) in C:\Users\shakeel.ishaq\Desktop\Wahaj\Saved Query on Publish\Saved Query on Publish\Class1.cs:line 22Detail: 
    <OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
      <ErrorCode>-2147220891</ErrorCode>
      <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
        <KeyValuePairOfstringanyType>
          <d2p1:key>CallStack</d2p1:key>
          <d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">   at Microsoft.Crm.Extensibility.VersionedPluginProxyStepBase.Execute(PipelineExecutionContext context)
       at Microsoft.Crm.Extensibility.Pipeline.Execute(PipelineExecutionContext context)
       at Microsoft.Crm.Extensibility.MessageProcessor.Execute(PipelineExecutionContext context)
       at Microsoft.Crm.Extensibility.InternalMessageDispatcher.Execute(PipelineExecutionContext context)
       at Microsoft.Crm.Extensibility.ExternalMessageDispatcher.ExecuteInternal(IInProcessOrganizationServiceFactory serviceFactory, IPlatformMessageDispatcherFactory dispatcherFactory, String messageName, String requestName, Int32 primaryObjectTypeCode, Int32 secondaryObjectTypeCode, ParameterCollection fields, CorrelationToken correlationToken, CallerOriginToken originToken, UserAuth userAuth, Guid callerId, Guid transactionContextId, Int32 invocationSource, Nullable`1 requestId, Version endpointVersion)
       at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.ExecuteRequest(OrganizationRequest request, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)
       at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Execute(OrganizationRequest request, CorrelationToken correlationToken, CallerOriginToken callerOriginToken, WebServiceType serviceType)</d2p1:value>
        </KeyValuePairOfstringanyType>
        <KeyValuePairOfstringanyType>
          <d2p1:key>OperationStatus</d2p1:key>
          <d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">0</d2p1:value>
        </KeyValuePairOfstringanyType>
      </ErrorDetails>
      <Message>System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
       at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
       at Saved_Query_on_Publish.Class1.Execute(IServiceProvider isp) in C:\Users\shakeel.ishaq\Desktop\Wahaj\Saved Query on Publish\Saved Query on Publish\Class1.cs:line 22</Message>
      <Timestamp>2014-03-04T12:55:04.7285261Z</Timestamp>
      <InnerFault i:nil="true" />
      <TraceText>
    
    [Saved Query on Publish: Saved_Query_on_Publish.Class1]
    [ce44b526-99a3-e311-8241-00155d002947: Saved_Query_on_Publish.Class1: Publish of  any Entity]
    
    
    </TraceText>
    </OrganizationServiceFault>
    I want to seek your kind help and suggestion in this regard to run the code on to the track.

    Tuesday, March 4, 2014 12:56 PM

All replies

  • Post your code, without the code it is not possible to understand what it is happening here:

    <Message>System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
       at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
       at Saved_Query_on_Publish.Class1.Execute(IServiceProvider isp) in C:\Users\shakeel.ishaq\Desktop\Wahaj\Saved Query on Publish\Saved Query on Publish\Class1.cs:line 22</Message>


    My blog: www.crmanswers.net - Rockstar 365 Profile

    Tuesday, March 4, 2014 1:14 PM
  • Here is my code in the following.

    public void Execute(IServiceProvider isp)
            { 
                IExecutionContext context = (IExecutionContext)isp.GetService(typeof(IExecutionContext));
                IOrganizationServiceFactory factory = (IOrganizationServiceFactory)isp.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = factory.CreateOrganizationService(context.UserId);
                try
                {
                    
                }
                catch(Exception ex)
                {
                    throw new InvalidPluginExecutionException(Convert.ToString(ex));
                }
                
            }

    Tuesday, March 4, 2014 1:17 PM
  • None of those lines look like they should throw the error you've reported. The error message says the error is on line 22 - that should be a big clue

    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Tuesday, March 4, 2014 6:20 PM
    Moderator
  • In the try catch block there is code but every thing is commented.
    Wednesday, March 5, 2014 5:55 AM
  • I've remove the commented lines from the code and just reset the IIS no it is working fine.

    Thank you all for sharing time from your busy schedule.

    Wednesday, March 5, 2014 7:20 AM