1

I have the following code, which I use to check out a file from my Document Library:

var ctx = new ClientContext("http://sharepoint/mysite");
var web = ctx.Web;
var file = web.GetFileByServerRelativeUrl("mysite/documentlibrary/filename.pdf");
file.CheckOut();
ctx.ExecuteQuery();

This code appears to work on most occasions, but sometimes when calling it I get this error message on the last line:

"The file 'http://sharepoint/mysite/documentlibrary/filename.pdf' is not checked out."

When I navigate to the file in a browser, I can see that the file is indeed not checked out, but the error is not helpful at all as that is what I am trying to do!

I am also unable to check the file out manually from the browser with the same message.

Does anyone know what might be happening here?

0

2 Answers 2

0

Try this instead:

var ctx = new ClientContext("http://sharepoint/mysite");
var web = ctx.Web;
var file = web.GetFileByServerRelativeUrl("mysite/documentlibrary/filename.pdf");
ctx.Load(file);
ctx.ExecuteQuery();
file.CheckOut();
3
  • This doesn't throw an error, but also doesn't check the file out. I tried adding an ExecuteQuery after the last line, but this gave the same error as my original code
    – Bassie
    Commented Aug 17, 2016 at 15:22
  • I also just tried checking the file out manually (through the browser) and got the same error - maybe best to start again with a new file...
    – Bassie
    Commented Aug 17, 2016 at 15:25
  • that was my next guess, the library had publishing
    – Mike
    Commented Aug 17, 2016 at 15:41
0

To solve this I went into the file properties and selected Unpublish this version :

enter image description here

After doing this and then making sure the file was checked in, I was able to check it out manually as well as with the following code:

using (var ctx = new ClientContext(_sitename))
{
    var web = ctx.Web;
    var file = web.GetFileByServerRelativeUrl(serverRelativeUrl);
    ctx.Load(file);
    file.CheckOut();
    ctx.ExecuteQuery();
}

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.