locked
CRM 2011 - VB.NET SDK RRS feed

  • Question

  • Hello,

    I'm trying to request all accounts from CRM with SDK.

    But I got a null reference object at line RetrieveMultiple. Here's my code:

    Dim service As IOrganizationService = _serviceProxy
    
            Dim page = New PagingInfo()
            page.Count = 5000
            page.PageNumber = 1
    
            Dim filter = New FilterExpression()
            filter.AddCondition("yominame", ConditionOperator.Contains, "a")
    
            Dim queryAccount As New QueryExpression With { _
            .EntityName = Account.EntityLogicalName,
            .ColumnSet = New ColumnSet(True),
            .PageInfo = page,
            .Criteria = filter
            }
            Dim retrieve = New RetrieveMultipleRequest()
            retrieve.Query = queryAccount
            Dim accounts = New RetrieveMultipleResponse()
            accounts = CType(service.Execute(retrieve), RetrieveMultipleResponse)
    


    Connection to CRM is ok, I can retrieve Firstname and Lastname of SystemUser.

    I tried without Criteria ... but same error:

    System.NullReferenceException was unhandled
      Message=La référence d'objet n'est pas définie à une instance d'un objet.
      Source=GetCRMID
      StackTrace:
           à GetCRMID.Form1.btnListAccount_Click(Object sender, EventArgs e) dans C:\Users\Form1.vb:ligne 95
           à System.Windows.Forms.Control.OnClick(EventArgs e)
           à System.Windows.Forms.Button.OnClick(EventArgs e)
           à System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
           à System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
           à System.Windows.Forms.Control.WndProc(Message& m)
           à System.Windows.Forms.ButtonBase.WndProc(Message& m)
           à System.Windows.Forms.Button.WndProc(Message& m)
           à System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           à System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           à System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           à System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           à System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
           à System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           à System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           à Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
           à Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
           à Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
           à GetCRMID.My.MyApplication.Main(String[] Args) dans 17d14f5c-a337-4978-8281-53493378c1071.vb:ligne 81
           à System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           à System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           à Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           à System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           à System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
           à System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           à System.Threading.ThreadHelper.ThreadStart()
      InnerException:

     

    Thanks for your help,

    Jérôme


    Jérôme Lambert http://www.infotec.net
    Wednesday, October 12, 2011 1:57 PM

Answers

  • I found:

    just with that:

    _serviceProxy = New OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri, _
                                                                 serverConfig.Credentials, serverConfig.DeviceCredentials)
                    Using _serviceProxy
    


    Regards,

    Jérôme


    Jérôme Lambert http://www.infotec.net
    • Marked as answer by Jerome2606 Thursday, October 13, 2011 8:24 AM
    Thursday, October 13, 2011 8:24 AM

All replies

  • Can you show us the code that has no criteria?
    Daniel Cai | http://danielcai.blogspot.com | Tata Solutions Inc.
    Wednesday, October 12, 2011 6:42 PM
  • Thanks for your help.

    Of course, here's my code:

    Dim service As IOrganizationService = _serviceProxy
    
    Dim queryAccount = New QueryExpression(Account.EntityLogicalName)
            queryAccount.ColumnSet = New ColumnSet(True)
    
            Dim accounts = service.RetrieveMultiple(queryAccount).Entities
    
            For Each account As Account In accounts
                rtxConsole.Text = rtxConsole.Text + account.Id.ToString + account.Name + ".\n"
            Next
    


    Kind regards,

    Jérôme


    Jérôme Lambert http://www.infotec.net
    Thursday, October 13, 2011 7:02 AM
  • It's object service that is null, problem with reference.

    If I copy this code in my connection to CRM, it's works. So I don't understand why I can't use my class variable in another function. I copy my full code:

    Private _serviceProxy As OrganizationServiceProxy
    
        Public Sub Form1()
            InitializeComponent()
            rtxConsole.Text = rtxConsole.Text + "En attente d'une connexion.\n"
        End Sub
    
        Private Sub btnConToCrm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConToCrm.Click
            rtxConsole.Text = rtxConsole.Text + "On démarre la connexion.\n"
            Try
                ' Obtain the target organization's Web address and client logon 
                ' credentials from the user.
                rtxConsole.Text = rtxConsole.Text + "Connexion réussie.\n"
                Dim credits As New ClientCredentials()
                credits.Windows.ClientCredential = System.Net.CredentialCache.DefaultCredentials
                Using _serviceProxy As New OrganizationServiceProxy(New Uri("http://crm:5555/Domain/XRMServices/2011/Organization.svc"), Nothing, credits, Nothing)
                    _serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(New ProxyTypesBehavior())
                    Dim service As IOrganizationService = _serviceProxy
                    Dim userResponse As WhoAmIResponse = service.Execute(New WhoAmIRequest())
                    Dim userid As Guid = (CType(service.Execute(New WhoAmIRequest()), WhoAmIResponse)).UserId
                    Dim systemUser As SystemUser = service.Retrieve("systemuser", userid, New ColumnSet(New String() {"firstname", "lastname"}))
                    rtxConsole.Text = rtxConsole.Text + "Logged on user is " + systemUser.FirstName + " " + systemUser.LastName + ".\n"
                    Dim versionRequest As New RetrieveVersionRequest
                    Dim versionResponse As RetrieveVersionResponse = service.Execute(versionRequest)
                    rtxConsole.Text = rtxConsole.Text + "Microsoft Dynamics CRM version " + versionResponse.Version + ".\n"
                    Dim queryAccount = New QueryExpression(Account.EntityLogicalName)
                    queryAccount.ColumnSet = New ColumnSet(True)
    
                    Dim accounts = service.RetrieveMultiple(queryAccount)
    
                    For Each account As Account In accounts.Entities
                        rtxConsole.Text = rtxConsole.Text + account.Id.ToString + account.Name + ".\n"
                    Next
                End Using
                '<snippetCRUDOperations2>
            Catch ex As FaultException(Of Microsoft.Xrm.Sdk.OrganizationServiceFault)
                rtxConsole.Text = rtxConsole.Text + "The application terminated with an error." + ".\n"
                rtxConsole.Text = rtxConsole.Text + "Timestamp: " + ex.Detail.Timestamp + ".\n"
                rtxConsole.Text = rtxConsole.Text + "Code: " + ex.Detail.ErrorCode.ToString() + ".\n"
                rtxConsole.Text = rtxConsole.Text + "Message: " + ex.Detail.Message + ".\n"
                rtxConsole.Text = rtxConsole.Text + "Trace: " + ex.Detail.TraceText + ".\n"
                rtxConsole.Text = rtxConsole.Text + "Inner Fault: " + If(Nothing Is ex.Detail.InnerFault, "Has Inner Fault", "No Inner Fault") + ".\n"
            Catch ex As System.TimeoutException
                rtxConsole.Text = rtxConsole.Text + "The application terminated with an error." + ".\n"
                rtxConsole.Text = rtxConsole.Text + "Message: " + ex.Message + ".\n"
                rtxConsole.Text = rtxConsole.Text + "Stack Trace: " + ex.StackTrace + ".\n"
                rtxConsole.Text = rtxConsole.Text + "Inner Fault: " + If(Nothing Is ex.InnerException.Message, "Has Inner Fault", "No Inner Fault") + ".\n"
            Catch ex As System.Exception
                rtxConsole.Text = rtxConsole.Text + "The application terminated with an error." + ".\n"
                rtxConsole.Text = rtxConsole.Text + ex.Message + ".\n"
    
                ' Display the details of the inner exception.
                If ex.InnerException IsNot Nothing Then
                    rtxConsole.Text = rtxConsole.Text + ex.InnerException.Message + ".\n"
    
                    Dim fe As FaultException(Of Microsoft.Xrm.Sdk.OrganizationServiceFault) = _
                        TryCast(ex.InnerException, FaultException(Of Microsoft.Xrm.Sdk.OrganizationServiceFault))
                    If fe IsNot Nothing Then
                        rtxConsole.Text = rtxConsole.Text + "Timestamp: " + fe.Detail.Timestamp + ".\n"
                        rtxConsole.Text = rtxConsole.Text + "Code: " + fe.Detail.ErrorCode + ".\n"
                        rtxConsole.Text = rtxConsole.Text + "Message: " + fe.Detail.Message + ".\n"
                        rtxConsole.Text = rtxConsole.Text + "Trace: " + fe.Detail.TraceText + ".\n"
                        rtxConsole.Text = rtxConsole.Text + "Inner Fault: " + If(Nothing Is fe.Detail.InnerFault, "Has Inner Fault", "No Inner Fault") + ".\n"
                    End If
                End If
                '</snippetCRUDOperations2>
                ' Additonal exceptions to catch: SecurityTokenValidationException, ExpiredSecurityTokenException,
                ' SecurityAccessDeniedException, MessageSecurityException, and SecurityNegotiationException.
            End Try
        End Sub
    
        Private Sub btnListAccount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListAccount.Click
            Dim service As IOrganizationService = _serviceProxy
    
            ' Dim page = New PagingInfo()
            'page.Count = 5000
            'page.PageNumber = 1
    
            'Dim filter = New FilterExpression()
            'filter.AddCondition("yominame", ConditionOperator.Contains, "a")
    
            'Dim queryAccount As New QueryExpression With { _
            '.EntityName = Account.EntityLogicalName,
            '.ColumnSet = New ColumnSet(True),
            '.PageInfo = page,
            '.Criteria = filter
            '}
            'Dim retrieve = New RetrieveMultipleRequest()
            'retrieve.Query = queryAccount
    
            Dim queryAccount = New QueryExpression(Account.EntityLogicalName)
            queryAccount.ColumnSet = New ColumnSet(True)
    
            Dim accounts = service.RetrieveMultiple(queryAccount)
    
            For Each account As Account In accounts.Entities
                rtxConsole.Text = rtxConsole.Text + account.Id.ToString + account.Name + ".\n"
            Next
        End Sub



    Jérôme Lambert http://www.infotec.net
    • Edited by Jerome2606 Thursday, October 13, 2011 7:11 AM
    Thursday, October 13, 2011 7:05 AM
  • I found:

    just with that:

    _serviceProxy = New OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri, _
                                                                 serverConfig.Credentials, serverConfig.DeviceCredentials)
                    Using _serviceProxy
    


    Regards,

    Jérôme


    Jérôme Lambert http://www.infotec.net
    • Marked as answer by Jerome2606 Thursday, October 13, 2011 8:24 AM
    Thursday, October 13, 2011 8:24 AM