none
Accesso a custom entity in società non principale da plugin

    Discussione generale

  • Se eseguo una query su una custom entity definita solo in una società non principale da un plugin (intendo la società non di default alla quale acceso tramite http://<nome server>/<nome società>) ottengo il seguente errore:

    exception: There is an error in XML document (1, 473).
    exception: System.InvalidOperationException: There is an error in XML document (1, 473). ---> System.InvalidOperationException: The specified type was not recognized: name='new_dettagliopreventivo', namespace='http://schemas.microsoft.com/crm/2007/WebServices', at <BusinessEntity xmlns='http://schemas.microsoft.com/crm/2006/WebServices'>.
       at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read133_BusinessEntity(Boolean isNullable, Boolean checkType)
       at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read168_BusinessEntityCollection(Boolean isNullable, Boolean checkType)
       at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read1631_RetrieveMultipleResponse()
       at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer21.Deserialize(XmlSerializationReader reader)
       at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
       --- End of inner exception stack trace ---
       at Microsoft.Crm.Extensibility.SdkTypeProxyCrmServiceWrapper.InternalInvoke(MethodInfo methodInfo, Object[] parameters)
       at Microsoft.Crm.Extensibility.SdkTypeProxyCrmServiceWrapper.RetrieveMultiple(QueryBase query)
       at TestPluginCrm4.Crm.Plugins.MyPlugin.Execute(IPluginExecutionContext context) in C:\Documents and Settings\robbin\Documenti\Visual Studio 2005\Projects\TestPluginCrm4\plugin.vb:line 89
    inner exception: The specified type was not recognized: name='new_dettagliopreventivo', namespace='http://schemas.microsoft.com/crm/2007/WebServices', at <BusinessEntity xmlns='http://schemas.microsoft.com/crm/2006/WebServices'>.
    inner exception: System.InvalidOperationException: The specified type was not recognized: name='new_dettagliopreventivo', namespace='http://schemas.microsoft.com/crm/2007/WebServices', at <BusinessEntity xmlns='http://schemas.microsoft.com/crm/2006/WebServices'>.
       at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read133_BusinessEntity(Boolean isNullable, Boolean checkType)
       at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read168_BusinessEntityCollection(Boolean isNullable, Boolean checkType)
       at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read1631_RetrieveMultipleResponse()
       at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer21.Deserialize(XmlSerializationReader reader)
       at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)


    Ho provato a eseguire la stessa query di caricamento tramite RetrieveMultiple da una applicazione console e dapprima ottenevo lo stesso errore ma poi ho risolto creando un riferimento web alla società appropriata (in coda all'indirizzo del servizio web ho aggiunto &uniquename=<nome società>. In tal modo ho ottenuto il WSDL specifico della società in questione e completo di definizione delle entità e attributi custom.

    Non so però come risolvere tale problema lato plug-in: per creare l'oggetto service uso l'approccio raccomandato, ovvero uso 'context.CreateCrmService'; il plugin è registrato per la società in questione e risponde correttamente all'evento Post-Update. Tuttavia, come anticipato, ottengo un errore che indica che l'entità custom del caso non esiste.


    Includo il codice sorgente nel caso possa essere utile:


    Public Class MyPlugin
        Implements IPlugin

        ' Related SDK topic: Writing a Plug-in
        Public Sub Execute(ByVal context As IPluginExecutionContext) Implements IPlugin.Execute
            IO.File.AppendAllText(IO.Path.Combine("c:\temp\", "TESTCRM.txt"), "Plugin start; org. name=" & context.OrganizationName & ControlChars.NewLine)
            If context.PrimaryEntityName = "quote" Then
                IO.File.AppendAllText(IO.Path.Combine("c:\temp\", "TESTCRM.txt"), "Quote section" + ControlChars.NewLine)

                Try
                    Dim entity As DynamicEntity = CType(context.InputParameters.Properties("Target"), DynamicEntity)
                    If Not entity.Properties.Contains("new_totalecommissione") Then _
                        Return

                    Dim quoteId As Key = CType(entity.Properties("quoteid"), Key)
                    Dim totAmount As CrmMoney = CType(entity.Properties("new_totalecommissione"), CrmMoney)

                    IO.File.AppendAllText(IO.Path.Combine("c:\temp\", "TESTCRM.txt"), "quote id=" & quoteId.Value.ToString & ControlChars.NewLine)

                    Dim service As ICrmService = CType(context.CreateCrmService(False), ICrmService)
                    'IO.File.AppendAllText(IO.Path.Combine("c:\temp\", "TESTCRM.txt"), "Service url=" & ControlChars.NewLine)

                    Dim cond As New ConditionExpression
                    Dim filter As New FilterExpression
                    Dim query As New QueryExpression

                    cond = New ConditionExpression()
                    cond.AttributeName = "new_preventivodettagliopreventivoid"
                    cond.Operator = ConditionOperator.Equal
                    cond.Values = New Object() {quoteId.Value}

                    filter.FilterOperator = LogicalOperator.And
                    filter.AddCondition(cond)

                    query = New QueryExpression
                    query.EntityName = "new_dettagliopreventivo"
                    query.ColumnSet = New AllColumns() ' cols
                    query.Criteria = filter

                    Dim entities As BusinessEntityCollection = service.RetrieveMultiple(query)
                    IO.File.AppendAllText(IO.Path.Combine("c:\temp\", "TESTCRM.txt"), "sub cnt=" & entities.BusinessEntities.Count.ToString & ControlChars.NewLine)

                Catch ex As Exception
                    IO.File.AppendAllText(IO.Path.Combine("c:\temp\", "TESTCRM.txt"), "exception: " & ex.Message & ControlChars.NewLine)
                    IO.File.AppendAllText(IO.Path.Combine("c:\temp\", "TESTCRM.txt"), "exception: " & ex.ToString & ControlChars.NewLine)
                    If ex.InnerException IsNot Nothing Then
                        IO.File.AppendAllText(IO.Path.Combine("c:\temp\", "TESTCRM.txt"), "inner exception: " & ex.InnerException.Message & ControlChars.NewLine)
                        IO.File.AppendAllText(IO.Path.Combine("c:\temp\", "TESTCRM.txt"), "inner exception: " & ex.InnerException.ToString & ControlChars.NewLine)
                    End If

                    Throw New InvalidPluginExecutionException(String.Format("Errore in plug-in {0}: {1}", Me.GetType().ToString(), ex.Message), ex)
                End Try
            End If

        End Sub
    martedì 10 novembre 2009 13:12