Share via


Code Snippet: Determine and Resolve External Items in Error in the BCS Client Cache

Applies to: SharePoint Server 2010

In this article
Description
Prerequisites
To use this example

Description

The following example shows how to check whether there are any failed operations for each external item of a subscription in the Business Connectivity Services Client Cache, and how to resolve the external items that are in error.

Prerequisites

  • Microsoft SharePoint Server 2010 or Microsoft SharePoint Foundation 2010 installed on the server

  • Microsoft .NET Framework 3.5 installed on the client computer

  • Microsoft Visual Studio

  • At least one subscription in the Business Connectivity Services Client Cache

To use this example

  1. Start Visual Studio on the client computer, and then create a new C# Microsoft Office application add-in project. Select .NET Framework 3.5 when you create the project.

  2. From the View menu, select Property Pages to bring up the project properties

  3. On the Build tab, for the Platform target, select Any CPU.

  4. Close the project properties window.

  5. In Solution Explorer, under References, remove all project references except for System and System.Core.

  6. Add the following references to the project:

    1. Microsoft.Office.BusinessApplications.Runtime

    2. Microsoft.BusinessData

  7. Replace the existing using statements with the following statements:

    using System;
    using Microsoft.BusinessData.Offlining;
    using Microsoft.Office.BusinessData.Offlining;
    using System.Windows.Forms;
    
  8. In the add-in’s startup event, call the two methods defined at the end of this procedure.

  9. Replace the placeholder values of <entityNamespace>, <entityName>, <viewName>, and <subscriptionName> with valid values.

  10. Save the project.

  11. Compile and run the project.

    This opens the Office application and executes the following code.

        // Instance-level error resolution.
        public static void ResolveCustomerErrors(char inErrorAction, char inConflictAction)
        {
            //Create an instance of remote offline runtime that creates a connection to the client cache.
            using (RemoteOfflineRuntime offlining = new RemoteOfflineRuntime())
            {
                ISynchronizationManager syncManager = offlining.GetSynchronizationManager();
                IMetadataCatalog catalog = offlining.GetMetadataCatalog();                

                //1. Get the view to resolve.
                IEntity entity = catalog.GetEntity("<entityNamespace>", "<entityName>");
                IView view = entity.GetSpecificFinderView("Read Item");

                //2. Get all instances in error - and in conflict - for the view.
                IEnumerator<IEntityInstance> instancesInError =
                    syncManager.GetAllInstancesInError(view);

                while (instancesInError.MoveNext())
                {
                    IOfflineEntityInstance offlineInstance = (IOfflineEntityInstance)instancesInError.Current;

                    //3. Check to see if instance is in error.
                    if (offlineInstance.SynchronizationStatus == SynchronizationStatus.InError)
                    {
                        switch (inErrorAction)
                        {
                            case 'C':
                                //3a. Cancel all operations for the instance.
                                offlineInstance.CancelAllInstanceOperations();
                                break;
                            case 'R':
                                //3b. Retry the failed operation.
                                offlineInstance.GetFailedOperation().Retry();
                                break;
                            case 'D':
                                //3c. Delete the instance.
                                offlineInstance.Delete();
                                break;
                        }
                    }
                    //4. Check to see whether instance is in conflict.
                    else if (offlineInstance.SynchronizationStatus == SynchronizationStatus.InConflict)
                    {
                        MessageBox.Show("Instance with the following conflicts found:");

                        ConflictData[] conflicts = null;
                        offlineInstance.GetConflictData(out conflicts);
                        foreach (ConflictData conflict in conflicts)
                        {
                            MessageBox.Show(String.Format("Conflict found on field with name '{0}', full field name '{1}'. Current value is '{2}', but LOB system has '{3}'.",
                                conflict.fieldName,
                                conflict.fieldFullName,
                                conflict.currentValue,
                                conflict.authoritativeValue));
                        }

                        switch (inConflictAction)
                        {
                            case 'R':
                                //4a. Retry with local changes.
                                offlineInstance.ReplaceChanges();
                                break;
                            case 'K':
                                //4b. Revert local changes.
                                offlineInstance.CancelAllInstanceOperations();
                                break;
                        }
                    }
                }
            }
        }

        // Operation-level error resolution.
        public static void ResolveOperationErrors(char errorAction)
        {
            using (RemoteOfflineRuntime offlining = new RemoteOfflineRuntime())
            {
                ISynchronizationManager syncManager = offlining.GetSynchronizationManager();

                // Get failed operations.
                IOperationCollection failedOperations = syncManager.GetAllOperationsInError();
                foreach (IOperation operation in failedOperations)
                {
                    MessageBox.Show(String.Format("FAILED OPERATION - ActivityId: '{0}', LOBSystem: '{1}', EntityName: '{2}', LastExceptionMessage: '{3}', Last Successful Operation Execution: '{4}'",
                               operation.ActivityId.ToString(),
                               operation.LobSystemInstance.GetDefaultDisplayName(),
                               operation.Entity.GetDefaultDisplayName(),
                               operation.LastException.Message,
                               operation.LastExecuted.ToString()));                     

                    switch (errorAction)
                    {
                        case 'C':
                            // Cancel all operations for the instance.
                            operation.CancelSingleOperation();
                            break;
                        case 'R':
                            // Retry the failed operation.
                            operation.Retry();
                            break;
                    }
                }
            }
        }

See Also

Reference

RemoteOfflineRuntime

GetSynchronizationManager()

GetMetadataCatalog()

IMetadataCatalog

GetEntity(String, String)

IEntity

GetSpecificFinderView(String)

IView

GetAllInstancesInError(IView)

IEntityInstance

IOfflineEntityInstance

SynchronizationStatus

CancelAllInstanceOperations()

GetFailedOperation()

GetConflictData([])

ConflictData

GetAllOperationsInError()

IOperationCollection

IOperation