I think, the solution here is to use the scopes approach.
There are two scope types in SharePoint: ConditionalScope and ExceptionHandlingScope. They allow you to implement conditional behaivour server-side. Only one request is sent to the server. Logics in scopes are packed together and sent to the server which is executed as a whole in a single request.
The easest way here is to implement server-side try-catch behaivour, using ExceptionHandlingScope, for example:
ClientContext clientContext = new ClientContext("http://localhost");
ExceptionHandlingScope scope = new ExceptionHandlingScope(clientContext);
using (scope.StartScope())
{
using (scope.StartTry())
{
// do your actions here
}
using (scope.StartCatch())
{
// if error is occured...
}
}
clientContext.ExecuteQuery();
As far as I know, there are no analogues for SPWeb.Exists
in client object model, so using ExceptionHandlingScope is probably the best solution here.
Some articles, which can help to understand server-scopes idea:
- How to: Use Conditional Scope
- How to: Use ExceptionHandlingScope