Error in creating custom entity using IOrganizationService in crm 2011 plugin.

Вопрос Error in creating custom entity using IOrganizationService in crm 2011 plugin.

  • 13 января 2012 г. 12:36
     
     

    Hello Everyone,

    I am creating a plugin which will create custom entity. I have used "CreateCustomActivityEntity.cs" for help, this class is using OrganizationServiceProxy.execute and instead of that I am using IOrganizationService.execute. I am posting my code here. This code is generating this error when plugin fires - You cannot start a transaction with a different isolation level than is already set on the current transaction.

    // Obtain the execution context from the service provider.

                var context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));

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

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


                // Create the custom activity entity.
                CreateEntityRequest request = new CreateEntityRequest
                {
                    HasNotes = true,
                    HasActivities = false,
                    PrimaryAttribute = new StringAttributeMetadata
                    {
                        SchemaName = "Subject",
                        RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
                        MaxLength = 100,
                        DisplayName = new Label("Subject", 1033)
                    },
                    Entity = new EntityMetadata
                    {
                        IsActivity = true,
                        SchemaName = "new_test",
                        DisplayName = new Label("Test", 1033),
                        DisplayCollectionName = new Label("Test", 1033),
                        OwnershipType = OwnershipTypes.UserOwned,
                        IsAvailableOffline = true,
                    }
                };

                service.Execute(request);  // in sdk class its _serviceProxy.Execute(request);

    I think I am doing something wrong because I am new in crm development and dobn't know how to use its classes. Please guide me why above code is not working. And just to fire my plugin I have registered it on account creation.Thanks in advance :)

    -- NMathur

Все ответы

  • 26 января 2012 г. 1:20
     
     

    Creating an entity/metadata through a plugin cannot be done.  Within the same sql transaction you are attempting to use and create metadata.  What is the scenario you are trying to do?  Perhaps there is another way it can be achieved? 

    Thanks,

    Brandon

  • 26 января 2012 г. 6:13
    Модератор
     
     
    I think there shoudl be a workaround to your problem - register the plugin to run asynchronously. This will take it out of the main transaction
    Microsoft CRM MVP - http://mscrmuk.blogspot.com  http://www.excitation.co.uk
  • 27 января 2012 г. 4:16
     
     
    I agree with you, I was doing it in wrong way. Thanks for reply :)
    -- NMathur
  • 27 января 2012 г. 4:16
     
     

    Hi David,

    Thanks for reply, I will try your solution.

    Thanks again :)


    -- NMathur
  • 22 февраля 2012 г. 0:57
     
     

    Hey NMathur,

    What was wrong in the solution ?  How did u rectify the problem ?

  • 22 февраля 2012 г. 5:18
     
     

    Hi aStani,

    I droped the idea to create custom entity through plugin because it was not feasible and I created it in my dummy solution using CRM UI. May be this can show you why my code was not working -

    http://stackoverflow.com/questions/5903422/crm-2011-organizationserviceproxy-vs-organizationservicecontext

    Sorry I can't help you with your problem because I am new to CRM development. Please post your problem as new Questiion so that CRM experts can help you with that. And please also post the Message, Stage and Execution mode in which you are registring your plugin, so that they can easily identify the issue.

    Thanks.


    -- NMathur


    • Изменено nmathur 22 февраля 2012 г. 5:19
    •