Welcome to Comunidade Bloggers |create|it| Sign in | Join | Help

André Vala

SharePoint
Office
FAST Search Server
.Net

News

  • European SharePoint Community Awards 2012 Winner
    Locations of visitors to this page

    Comunidade Portuguesa de SharePoint

    View André Vala's profile on LinkedIn

    © André Vala and Create IT, 2006-2011. Unauthorized use and/or duplication of this material without express and written permission from this blog’s author and/or owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to André Vala and Create IT with appropriate and specific direction to the original content.

SharePoint Tip #17: Testing if an Office Document is Open

In a Document Library, when you create a new document using the New button (or edit one that already exists), SharePoint will open the proper client application (if it knows which one it is) for you to edit the file. If you have an event handler attached to the ItemAdded or ItemUpdated event, and you wish to update the list item inside that handler, you might not be allowed to do so because the client application is locking the file while it's being edited.

So, how do you check if the file is being locked by the client application?

The SPFile class has three properties that might help you with that:

SPFile.CheckOutStatus – this property indicates if and how the file has been checked-out. It has one of four possible values:

  • SPFile.SPCheckOutStatus.None – the file is not checked-out.
  • SPFile.SPCheckOutStatus.LongTerm – the file was checked-out using the SharePoint interface (or hasn't been checked-in yet).
  • SPFile.SPCheckOutStatus.LongTermOffline – the file was check-out for offline editing.
  • SPFile.SPCheckOutStatus.ShortTerm – the file is not checked-out in SharePoint, but is being locked by a client application.

SPFile.CheckOutDate – this property stores the date and time of the check-out (if applicable) in UTC date format.

SPFile.CheckOutExpires – this property stores the date and time when a Short Term Check-Out license expires, in UTC date format. This property can only be used if the CheckOutStatus value is ShortTerm.

To check whether a file is being locked by a client application, just test the SPFile.CheckOutStatus for the ShortTerm value. Additionally, you might want to know when it was checked-out (SPFile.CheckOutDate) and when will the short term check-out expire (SPFile.CheckOutExpires).

Notes:

  • The client application might not keep the file locked until the short term check-out expires, or it might renew the short term check-out license.
  • Word 2007, for instance, locks a file in 10 minute intervals (600 seconds).
Posted: Friday, September 21, 2007 5:00 AM by andrevala

Comments

PhilChilds said:

Hi André,

Nice post - I couldn't find this information anywhere else and I think that it should be better documented because it seems to me that it should be a well known issue.

I don't suppose that you have some sample code that you could send me please?

I'm having lots of problems with an event handler that modifies the columns of a document when created or edited - it works fine when a document is uploaded using the browser UI, but does not fire when a user adds or changes the document directly in Office.

Many thanks,

Phil

# August 4, 2008 6:54 AM

Schalk's evolutionary ramblings said:

I've been getting the following messages logged to a workflow history "Error updating a list

# February 22, 2009 11:50 PM

Schalk's evolutionary ramblings said:

I've been getting the following messages logged to a workflow history "Error updating a list

# February 23, 2009 6:19 PM

carlos said:

A great tidbit of information. alongside this issue, I also have a problem trying to figure out how to open a document lib document in read only mode like SharePoint does when you click the document link. The client wants to display the contents of a document lib in an SPGridview an offer just the checkout options along side with preview button so the user can click the preview button a the document opens up in read only node. PS I'm supposed to do this in a couple of hours. hopefull you're sill bloging in this post... anywho thanks
# May 13, 2010 9:42 AM

Varismii said:

Great info. I was wondering about the ShortTerm usage and how it is handled. Info about Word creating locks at an interval is appreciated. Thanks. -Miika
# June 22, 2010 5:05 AM
Anonymous comments are disabled