private static bool CheckOutIfNeeded(Web web, File targetFile) { var checkedOut = false; try { web.Context.Load(targetFile, f => f.CheckOutType, f => f.ListItemAllFields.ParentList.ForceCheckout); web.Context.ExecuteQueryRetry(); if (targetFile.ListItemAllFields.ServerObjectIsNull.HasValue && !targetFile.ListItemAllFields.ServerObjectIsNull.Value && targetFile.ListItemAllFields.ParentList.ForceCheckout) { if (targetFile.CheckOutType == CheckOutType.None) { targetFile.CheckOut(); } checkedOut = true; } } catch (ServerException ex) { // Handling the exception stating the "The object specified does not belong to a list." if (ex.ServerErrorCode != -2146232832) { throw; } } return checkedOut; }