I am trying to show an error when a user performs an operation with a list item that is checked out.

How I get the list item:

var listItem = list.GetItemById(itemId);

This works and I can perform some other checks, like check the item's column values.

Now I try to check whether the list item is checked out:

if (listItem.File.CheckOutType != SPFile.SPCheckOutType.None) {
    var checkedOutBy = listItem.File.CheckedOutByUser.Name;
    throw new Exception("This item is checked out by " + checkedOutBy);

This throws an exception "Object reference not set to an instance of an object".

I check whether the listItem.File is null:

if (listItem.File == null) {
    throw new Exception("listItem.File is null"); //gets thrown

This confirms that the .File is null.

What did I do wrong? Is there any other way to check whether a list item is checked out?


1 Answer 1


You need to load the File along with the item.

ctx.Load(items, item=> item.Include(f => f.File));

Ref: Checking if file is checked out through CSOM