Business Connectivity Services (BCS) has a synchronization framework that uses a client cache to enable you to work with external data even when disconnected.
In this post, we’ll discuss ways to optimize solutions to best use the synchronization framework client cache. We’ll be using the Contoso Sales Manager sample solution presented in the previous post, BCS Solution Packaging Tool. Please check that post for instructions on how to package and deploy the solution.
How does the client cache work?
When a solution is deployed to an Office client, a process called BCSSync.exe contacts the external system directly and downloads the data into a per-user SQL CE database managed by the synchronization framework.
Every client will be contacting the external system directly, so it’s really important to optimize your solution for offline use to minimize the load on the external system.
Working with Subscriptions
To determine what data will be brought to the client cache, solutions need to define subscription xml files. Each subscription file is responsible for defining how to request data from the external system.
Subscriptions can have 3 types of items: Queries, Explicit Identities and Associations. Queries point to finders that will be executed against the external system to populate the cache, Explicit Identities are additional instances that are added individually to the subscription, and Associations are used to bring related instances.
Here is the subscription file for Contoso Sales Manager solution defining how to request data for the “Customers” external content type.
The subscription above has one query, which runs the “GetCustomers” Finder on the external content type. All Customer instances returned by the finder call will be cached locally on every client that has the solution installed. The refresh interval for the query is set to 180 minutes, so every 3 hours the finder will be executed and the local data will be refreshed on each client.
If you know exactly which items to synchronize, you can just add their identities to the subscription individually. This is a lot easier to do programmatically since you have to put the serialized identity on the subscription XML file.
To get the serialized Identity, you need to instantiate an Identity object with the appropriate identifier values, and call its Serialize method. More information can be found at the Identity.Serialize method page on MSDN. Once you have the Identity’s serialized string, you just need to add it in an “<Identity>” tag within the “<Identities>” tag, as will be shown later in the example.
The obvious way to optimize synchronization is to reduce the amount of data that is downloaded by the client. The easiest way to do this is by using filters. The filters supported by the synchronization framework that can be used to reduce the number of items downloaded are Wildcard, Comparison and Limit filters.
For example, you can use a Wildcard filter to download all employees whose names start with the letter “M” using “M*” as the filter value. Or you can select all customers that have their zip code equal to “98052” using a comparison filter. A limit filter can be used to limit the number of products to 100.
Here is an example of the subscription above with 11003 and 11020 as explicit identities and a filter to return only customer with CustumerIDs greater than 11050.
This subscription will only download the specified customers, as you can see in the image below.
The filters used by the subscription file have to be defined on the model, and the external system needs to support them, too. The model for the Contoso Sales Manager solution and the AdventureWorks web service used by the solution were updated to support a comparison filter on the CustomerID field. The updated solution can be found in the package attached to this post.
To learn more about BCS client cache, navigate to Understanding Business Connectivity Services Client Cache to Optimize Your Solutions.
For more information on how to bring external data to Office client applications go to:
- BCS Team Channel: External Lists in Outlook
- External Lists in SharePoint Workspace
- BCS Team Channel: External Data in SharePoint Workspace.
For more information on subscriptions and subscription files, see What Is a Cache Subscription?.
Is that all?
Limiting the amount of data downloaded by the client is not always an option. For those cases, version field, Bulk Stereotypes and Last Id Seen filter are a great option. They will be covered in an upcoming post.
– Rodrigo Silveira.