Cache Object Model

Applies to: SharePoint Server 2010

Microsoft Business Connectivity Services (BCS) provides a public object model (OM), the Microsoft.BusinessData.Offlining namespace in Microsoft.SharePoint.dll, to work with the cache and find and resolve errors.

The following code snippet shows how to enumerate the items of an external content type in the client cache.

RemoteSharedFileBackedMetadataCatalog catalog = new RemoteSharedFileBackedMetadataCatalog();
            INamespacedEntityDictionaryDictionary entDictDict = catalog.GetEntities("*");

            foreach (INamedEntityDictionary entDict in entDictDict.Values)
            {
                foreach (IEntity entity in entDict.Values)
                {
                    entityDictonary.Add(entity.Name, entity);
                }
            }


            IEntity entity = entityDictonary[0];
            IEntityInstanceEnumerator instanceEnumerator = entity.FindFiltered(
                                        entity.GetDefaultFinderFilters(),
                                        entity.GetMethodInstances(MethodInstanceType.Finder)[0].Value.Name,
                                        entity.GetLobSystem().GetLobSystemInstances()[0].Value,
                                        OperationMode.CachedWithoutRefresh);


            DataTable dt = new DataTable();

            string finderName = entity.GetMethodInstances(MethodInstanceType.Finder)[0].Value.Name;
            IView v = entity.GetFinderView(finderName);

            foreach (IField f in v.Fields)
            {
                
                dt.Columns.Add(new DataColumn(f.Name, Type.GetType(f.TypeDescriptor.TypeName)));
            }

            while (instanceEnumerator.MoveNext())
            {
                IEntityInstance iei = instanceEnumerator.Current;
                DataRow dr = dt.NewRow();
                foreach (IField f in v.Fields)
                {
                    dr[f.Name] = iei[f.Name];
                }
                dt.Rows.Add(dr);
            }

            return dt;

The following code snippet shows how to programmatically do a cache refresh from a client application. This code when run starts the BCSSync service if it is not already running and requests a refresh in the BCS client cache. This code snippet assumes that you already have a cache subscription set up for the referenced external content type. Replace <EntityNamespace>, <EntityName>, <ViewName>, and the <SubscriptionName> with valid values from the model and subscription files.

RemoteOfflineRuntime offlineRuntime = new RemoteOfflineRuntime();
ISubscriptionManager subManager = offlineRuntime.GetSubscriptionManager();
ISubscription sub = subManager.GetSubscription("<EntityNameSpace>", "<EntityName>", "<ViewName>", "<SubscriptionName>");
sub.RequestRefresh(true);

Some of the key things to note when working with the cache and object model are outlined below.

In the runtime OM, at the method instance level, Business Connectivity Services enables you to pass in an operation mode which tells the runtime if and how you want to use the cache. Business Connectivity Services supports two kinds of cache usage modes.

  • Online: If the application is set to Online mode, cache is not used. All the calls to the external data source such as Read, Update, etc. are routed directly to the external data source via the Business Data Connectivity (BDC) runtime.

  • Cached: If the application is set to Cached mode, the cache is used. Cache is populated with the entity instances from the external data source at deployment time and data is refreshed from the external data source at specified intervals. Operations are not immediately routed to the external data source but are routed to the cache. The cache puts these operations in the operation queue and executes them one-by-one.