I am trying to Discard Check Out on a specific Checked Out SharePoint file but I got stuck.
Here is my code snippet:
//Define a ClientContext with the specific Share Point site Url.
var ctx = new ClientContext(siteUrl);
var folder = ctx.Web.GetFolderByServerRelativeUrl(folderUrl);
//Get specific folder`s Url, where the target file exists.
ctx.Load(folder, f => f.ServerRelativeUrl);
ctx.ExecuteQuery();
//Get file`s data.
var targetFileName = "someFile.txt";
var file = ctx.Web.GetFileByServerRelativeUrl(folder.ServerRelativeUrl + targetFileName);
ctx.Load(file, f => f.Exists, f => f.Name);
ctx.ExecuteQuery();
//Output file data to ensure that we got exactly that file. That WriteLine provides required output without any errors.
Console.WriteLine($"File Data\n--> Exists: {file.Exists}\n--> Name: {file.Name}");
//Try to Undo Check Out. DID NOT WORK ??? File is checked out 100500%!
file.UndoCheckOut();
ctx.ExecuteQuery();
Console.WriteLine("Done");
The problem is that when I’m trying to execute the UndoCheckOut method - it throws the following kind of exception:
Unhandled Exception: Microsoft.SharePoint.Client.ServerException: The file "cst/PTFixLists/RPA_Robot testing folder/Combined Fix list and Manual Errors 5-14-18 2.xlsx" is not checked out.