0

This question already has an answer here:

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?

marked as duplicate by Eric Alexander Dec 2 '16 at 18:19

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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

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