Sunday, May 02, 2004 4:11 PM Jan Tielens

SharePoint Document Library Event Handlers & "Locked for Editing" Status

In my previous post I described a problem I was having while implementing a generic SharePoint Document Library Event Handler. When you save a document from Office 2003 (for example Word 2003) into a SharePoint Document Library, the document item in the library is “locked for editing” as long as the Office 2003 application stays open. This can be a problem, for example when you want the Event Handler to update a field of the document item when it’s submitted to the Document Library. There are no problems when using the web interface, but when a user does this by using Office 2003 you’ll end up with the following exception:
Microsoft.SharePoint.SPException: Document Locked
The document you are attempting to edit is locked and cannot be updated.  It may be in use by another user or undergoing content indexing.

Too bad there isn’t even an event that the Event Handler can intercept when the document is un-locked (when the user closes the Office 2003 application). So I came up with following solution: you can use the CheckOutStatus property of the SPFile class to check if the document item is locked. The CheckOutStatus property can have three values:

  • LongTerm
    Specifies that the check-out is long-term.
  • None
    Specifies that the file is not checked out.
  • ShortTerm
    Specifies that the file has been opened by a user and is locked for editing. A short-term check-out, or file-locking, only has effect as long as the application renews the lock.

As long as the CheckOutStatus property is not equal to None, I suspend the thread that’s handling the event. You can do this pretty easy in a loop:

public void OnEvent(SPListEvent listEvent)
{
    SPFile file = web.GetFile(listEvent.UrlAfter);
    while(file.CheckOutStatus != SPFile.SPCheckOutStatus.None)
    {
        System.Threading.Thread.Sleep(1000);
        file = web.GetFile(le.UrlAfter);
    }
}

The second line in the while code block is needed to refresh the values of the SPFile object. I’m not completely happy with the solution, my first concern is that when the IIS Worker Process gets killed, all the event handlers that haven’t finished are lost. So the worst case scenario is that for example the IISRESET is used when an event handler is in the while loop. My second concern is scalability: when a lot of users submit a lot of documents at the same, possibly a lot of threads could be sleeping, so a lot of threads could be active which could lead to some performance issues. Well for now I can live with these concerns, just keep in mind when you use this approach what the consequences are. In my opinion, the cleanest solution would be that SharePoint only raised events when the document is un-locked...

Filed under:

Comments

# re: SharePoint Document Library Event Handlers & "Locked for Editing" Status

Tuesday, May 04, 2004 10:36 AM by chadbr

that does seem like a serious scalability killer... couldn't you write the locked files to a sharepoint list and process the list every once in a while (i.e. on startup start a timer...)?

# re: SharePoint Document Library Event Handlers & "Locked for Editing" Status

Tuesday, May 04, 2004 10:40 AM by Jan

Nice idea! Although this would require some additonal coding (no problem) but also the need for an extra document library. I'll consider it, thanks!

# Sharepoint Document Event Handlers

Sunday, May 23, 2004 5:31 PM by TrackBack

# re: SharePoint Document Library Event Handlers & "Locked for Editing" Status

Tuesday, June 20, 2006 6:21 PM by Larry

I seem to have a related problem.  My documents aren't locking, but when I get to the line (vb.net):

Dim oItem As SPListItem = file.Item

oItem is always nothing.  file is NOT nothing, but oItem never gets instantiated, so my fields never get enumerated, and nothing ever gets updated.

Any ideas?

# re: SharePoint Document Library Event Handlers & "Locked for Editing" Status

Wednesday, January 10, 2007 12:24 PM by Luke

Any idea how to tell if the document was uploaded or created via a template?  I want to disallow uploading of documents to my library.

# re: SharePoint Document Library Event Handlers & "Locked for Editing" Status

Friday, March 16, 2007 3:13 PM by Mahendra ghedia

Thanks dude...

it is helpful.

# re: SharePoint Document Library Event Handlers & "Locked for Editing" Status

Friday, March 16, 2007 3:21 PM by Mahendra G. Ghedia

it is helpful.

but we should stop the look after some time

so i have done below trick to stop waiting after 30 minute...

public void OnEvent(SPListEvent listEvent)

{

   int intSecondCounter=0;

   SPFile file = web.GetFile(listEvent.UrlAfter);

   while(file.CheckOutStatus != SPFile.SPCheckOutStatus.None)

   {

       System.Threading.Thread.Sleep(1000);

       file = web.GetFile(le.UrlAfter);

       intSecondCount++;

       //if document is still open for more than 30 min, exit the waiting thread...

       if(intSecondCount>=1800)

       {

break;

       }

   }

}

# re: SharePoint Document Library Event Handlers & "Locked for Editing" Status

Monday, April 23, 2007 8:48 AM by eddy

This option is not good I think.

It blocks not only holds the current thread, but all other threads.

Even de webiste itself has trouble loading, because it is waiting.

A better solution I think is to park the unfinished businness (on a another list) en try it another time.

# re: SharePoint Document Library Event Handlers & "Locked for Editing" Status

Tuesday, July 17, 2007 6:34 PM by jbobSharepoint

use smigrate to create a backup then delete the website itself & recreate it in the same location

# "Document checked out" workflow issue in SharePoint document library

Sunday, February 22, 2009 11:50 PM by Schalk's evolutionary ramblings

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

# "Document checked out" workflow issue in SharePoint document library

Monday, February 23, 2009 6:19 PM by Schalk's evolutionary ramblings

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