This documentation is archived and is not being maintained.

Finding the ID Using Filtered Views

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

Find the latest SDK documentation: CRM 2015 SDK

Another approach to retrieve the Microsoft Dynamics CRM entity instance IDs is to use the standard .NET SQL Data Access classes to query the filtered views in Microsoft Dynamics CRM. You must create a query to retrieve the records that you are interested in.

Note    You cannot access filtered views in Microsoft Dynamics CRM Online because access to the SQL database is not supported in Microsoft Dynamics CRM Online.


The following sample illustrates the same query used for the QueryExpression example but instead uses the Microsoft Dynamics CRM Filtered Views.

[Visual Basic .NET]
Public Shared Function Run(ByVal crmServerUrl As String, ByVal orgName As String, ByVal crmDatabaseName as String) As Boolean
      ' Define the SQL Query that selects all leads that were generated
      ' by employee referrals. Because this queries against a filtered view,
      ' this query only returns records that the calling user has Read
      ' access to.
      Dim sqlQuery As String = "SELECT * " +  _
                              "FROM FilteredLead " + _
                              "WHERE leadsourcecodename = 'Employee Referral'"

      ' Connect to the Microsoft Dynamics CRM Database Server. You must use Windows Authentication;
      ' SQL Authentication will not work.
      ' TODO: Substitute "Localhost" and "Adventure_Works_Cycle" with your SQL Server and CRM DB names.
      Dim connection As New SqlConnection("Data Source=localhost;Initial Catalog=" + crmDatabaseName + ";Integrated Security=SSPI")

      ' Create a DataTable to store the results of the query.
      Dim table As New DataTable()

      ' Create and configure the SQL Data Adapter that will fill the DataTable.
      Dim adapter As New SqlDataAdapter()
      adapter.SelectCommand = New SqlCommand(sqlQuery, connection)

      ' Execute the query by filling the DataTable.

   Catch e As System.Web.Services.Protocols.SoapException
      ' Add your error handling code here.
   End Try

   Return True
End Function 'Run

See Also


© 2010 Microsoft Corporation. All rights reserved.