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);

//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);

//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%!

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.

1 Answer 1


Replace the following line of code

var file = ctx.Web.GetFileByServerRelativeUrl(folder.ServerRelativeUrl + targetFileName);


var file = ctx.Web.GetFileByServerRelativeUrl(folder.ServerRelativeUrl +"/"+ targetFileName);
  • I did, but it still not working.
    – A.Dovbysh
    Dec 4, 2019 at 8:57
  • Go to Library settings->Versioning Settings, then check if "Require documents to be checked out before they can be edited?" set to "No".
    – LZ_MSFT
    Dec 4, 2019 at 9:05
  • This setting is set to "No".
    – A.Dovbysh
    Dec 4, 2019 at 13:19
  • Create a new library to check if it works.
    – LZ_MSFT
    Dec 5, 2019 at 1:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.