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
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.
From the View menu, select Property Pages to bring up the project properties
On the Build tab, for the Platform target, select Any CPU.
Close the project properties window.
In Solution Explorer, under References, remove all project references except for System and System.Core.
Add the following references to the project:
Microsoft.Office.BusinessApplications.Runtime
Microsoft.BusinessData
Replace the existing using statements with the following statements:
using System; using Microsoft.BusinessData.Offlining; using Microsoft.Office.BusinessData.Offlining; using System.Windows.Forms;
In the add-in’s startup event, call the two methods defined at the end of this procedure.
Replace the placeholder values of <entityNamespace>, <entityName>, <viewName>, and <subscriptionName> with valid values.
Save the project.
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()