0

I am using sharepoint CSOM to download / upload file from a OneDriveBusiness account.

Before downloading the file I need to check if the file is currently in use. File.CheckOutType is alway "None". I though using File.LockedByUser property, using the following code, but I got a ServerObjectNullReferenceException when the file is not locked.

var listItem = clientDocs.GetItemById(item.Id);
clientContext.Load(listItem.File.LockedByUser);
clientContext.ExecuteQuery();
var locked = listItem.File.LockedByUser.UserId;


I was hoping to be able to do do something like : 
if (file."locked")
{
  throw exception...

}

enter code here

Any idea ? Thanks !

1 Answers1

1

File.LockedByUser property is a deferred property, it need to be requested explicitly as demonstrated below:

var list = ctx.Web.Lists.GetByTitle(listTitle);
var listItem = list.GetItemById(itemId);
ctx.Load(listItem, i => i.File.CheckOutType, i => i.File.CheckedOutByUser, i => i.File.LockedByUser);
ctx.ExecuteQuery();
if(listItem.File.CheckOutType != CheckOutType.None) //Is checked out?
{
     var checkoutUserName = listItem.File.CheckedOutByUser.LoginName;
     var lockedUserName = listItem.File.LockedByUser.LoginName;
}
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193
  • Thanks for your answer, but Is it possible that the File.CheckOutType is always none when the file is open on a the Ipad with excel ? I will do more test, but at first sight editing the file with excel doesn't change the CheckOutType. – Marko Lachance Apr 29 '15 at 00:02