0

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.

1 Answer 1

0

Replace the following line of code

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

with

var file = ctx.Web.GetFileByServerRelativeUrl(folder.ServerRelativeUrl +"/"+ targetFileName);
4
  • 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 that you have read and understand our privacy policy and code of conduct.

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