Smart filtering in Business Connectivity Services
Business Connectivity Services allows the rapid creation of reference connections to live legacy data such as tables or views in SQL Server. The wildcard filtering is great, but what if you want to customize it?
Smart MyFilterParm Filtering in BCS
Using a stored procedure requires deviating from the easy out-of-box dynamic SQL, and defining the input parameter(s).
Here’s the pseudocode T-SQL for the smart MyFilterParm filtering. Note that first up to 10 MyFilterParms are listed, based on matching on a specific field, then 50 more generic wildcard matches are matched. This has the advantage of a high speed response, even if a user enters the letter “a” for search, making long distance (inter-farm) lookups more responsive. For the Union, note the fields from each Select need to match precisely in sequence, name and type. Best is if the exact same fields and names are returned that we are using today.
CREATE procedure dbo.sp_MySmartSearch @MyFilterParmSmart nvarchar(255) = null AS SELECT 10 FROM [MyDataBase].[dbo].[CompanyView] WHERE MyFilterParm LIKE @MyFilterParmSmart + '%' UNION SELECT 50 FROM [MyDataBase].[dbo].[CompanyView] WHERE CompanyNM LIKE '%' + @MyFilterParmSmart + '%'
Once this Stored Procedure is written, export the BDCM (using SPD) and edit the XML to provide hard-coded reference to the above Stored procedure, MyFilterParm filter parameter, and fields returned. The BDCM import is not done in SPD, but is instead done in Central Admin in the BCS service app config. Here’s the XML Pseudocode to replace within the Methods XML group in the BDCM (important parts highlighted in larger font):
<Property Name="BackEndObject" Type="System.String">sp_MySmartSearch <Property Name="BackEndObjectType" Type="System.String">SqlServerRoutine <Property Name="RdbCommandText" Type="System.String">[dbo].[sp_MySmartSearch] <Property Name="RdbCommandType" Type="System.Data.CommandType, System.Data, Version=220.127.116.11, Culture=neutral, PublicKeyToken=b77a5c561934e089">StoredProcedure <Property Name="Schema" Type="System.String">dbo <Parameter Direction="In" Name="@MyFilterParmSmart"> <TypeDescriptor TypeName="System.String" AssociatedFilter="Wildcard" Name="@MyFilterParmSmart">
You’ll find the XML much easier to edit in Visual Studio (any version) as the nesting is a bit much to handle in Notepad.
MSDN offers a similar example of a stored procedure, in this case, designed to return precisely one row:
Scot Hillier’s BCS book is also an excellent reference: