none
1st attempt at a plugin

    Question

  • Crm 2013. I have tried creating a simple plugin to add a record to an external SQL db when a new account is added.

    I an conning up with the following error

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Error inserting new customer - Request for the permission of type 'System.Data.SqlClient.SqlClientPermission, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.Detail: 
    <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>OperationStatus</d2p1:key>
          <d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">0</d2p1:value>
        </KeyValuePairOfstringanyType>
        <KeyValuePairOfstringanyType>
          <d2p1:key>SubErrorCode</d2p1:key>
          <d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">-2146233088</d2p1:value>
        </KeyValuePairOfstringanyType>
      </ErrorDetails>
      <Message>Error inserting new customer - Request for the permission of type 'System.Data.SqlClient.SqlClientPermission, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.</Message>
      <Timestamp>2013-11-02T18:44:37.7021358Z</Timestamp>
      <InnerFault i:nil="true" />
      <TraceText>
    
    [CRM2013 Plugins: CRM2013_Plugins.ClientAdminPlugin.ClientAdmin]
    [0e480ec7-ee43-e311-bfbd-00155d01d306: CRM2013_Plugins.ClientAdminPlugin.ClientAdmin: Create of account]
    
    
    </TraceText>
    </OrganizationServiceFault>

    code

    Imports System
    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text
    Imports Microsoft.Xrm.Sdk
    Imports System.ServiceModel
    Imports Microsoft.SqlServer
    
    
    Namespace ClientAdminPlugin
        Public Class ClientAdmin
            Implements IPlugin
    
            Public Sub Execute(serviceProvider As System.IServiceProvider) Implements Microsoft.Xrm.Sdk.IPlugin.Execute
                Dim context As IPluginExecutionContext = DirectCast(serviceProvider.GetService(GetType(IPluginExecutionContext)), IPluginExecutionContext)
    
                Try
                    Dim SQLconnction As New SqlClient.SqlConnection("Data Source=xxxxxxxxxxx;Initial Catalog=xxxxxx;Persist Security Info=True;User ID=sa;password=xxxxxxxx")
                    Dim SQLCommand As New SqlClient.SqlCommand
                    SQLCommand.CommandText = "INSERT INTO [TestDB].[dbo].[ClientAdmin] (Licence) Values ('999999') "
                    SQLCommand.CommandType = CommandType.Text
                    SQLCommand.Connection = SQLconnction
                    SQLCommand.Connection.Open()
                    SQLCommand.ExecuteNonQuery()
                    SQLCommand.Connection.Close()
                    SQLCommand.Connection.Dispose()
                Catch ex As Exception
                    Throw New InvalidPluginExecutionException("Error inserting new customer - " & ex.Message.ToString)
                End Try
    
            End Sub
    
    
    
        End Class
    End Namespace


    Dont ask me .. i dont know

    Saturday, November 02, 2013 6:56 PM

Answers

  • after further searching, I have made some changes to my code, and have come up with a different error

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: An error occurred. Contact a system administrator or refer to the Microsoft Dynamics CRM SDK troubleshooting guide.Detail: <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>OperationStatus</d2p1:key> <d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">0</d2p1:value> </KeyValuePairOfstringanyType> <KeyValuePairOfstringanyType> <d2p1:key>SubErrorCode</d2p1:key> <d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">-2146233088</d2p1:value> </KeyValuePairOfstringanyType> </ErrorDetails> <Message>An error occurred. Contact a system administrator or refer to the Microsoft Dynamics CRM SDK troubleshooting guide.</Message> <Timestamp>2013-11-02T20:14:19.4317819Z</Timestamp> <InnerFault i:nil="true" /> <TraceText> [ActivityFeeds.Plugins: ActivityFeeds.Plugins.ActivityClose] [cde27612-f843-e311-bfbd-00155d01d306: ActivityFeeds.Plugins.ActivityClose: Create of account] </TraceText> </OrganizationServiceFault>

    New code
    
    Imports System
    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text
    Imports Microsoft.Xrm.Sdk
    Imports System.ServiceModel
    Imports Microsoft.SqlServer
    
    
    Namespace ClientAdminPlugin
        Public Class ClientAdmin
            Implements IPlugin
    
            Public Sub Execute(serviceProvider As System.IServiceProvider) Implements Microsoft.Xrm.Sdk.IPlugin.Execute
                Dim context As IPluginExecutionContext = DirectCast(serviceProvider.GetService(GetType(IPluginExecutionContext)), IPluginExecutionContext)
    
                Dim SQL_Connection As New SqlClient.SqlConnection
                Dim SQL_Command As New SqlClient.SqlCommand
                SQL_Connection = New SqlClient.SqlConnection("Data Source=xxxxxxxx;Initial Catalog=TestDB;Persist Security Info=True;User ID=sa;password=xxxxxxxxx")
    
                Try
                    SQL_Connection.Open()
                    SQL_Command.CommandType = CommandType.Text
                    SQL_Command.CommandText = "INSERT INTO ClientAdmin (Licence) Values ('217514')"
                    SQL_Command.Connection = SQL_Connection
                    SQL_Command.ExecuteNonQuery()
                Catch ex As Exception
                    Throw New InvalidPluginExecutionException("Error inserting new customer - " & ex.Message.ToString)
                Finally
                    SQL_Connection.Close()
                    SQL_Connection.Dispose()
                End Try
    
            End Sub
    
    
    
        End Class
    End Namespace


    Dont ask me .. i dont know

    • Marked as answer by Pete Newman Saturday, November 02, 2013 8:41 PM
    Saturday, November 02, 2013 8:27 PM