Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Resolve SharePoint Document Locked for Editing Issue

, 7 Sep 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
CodeProjectThis morning I got a support request from the client I'm consulting with, basically he's having a "SharePoint Document Locked for Editing" issue while working with SharePoint documents. Most of the time, it's a valid warning message, which notifies that someone else might edit the documen

This morning I got a support request from the client I'm consulting with, basically he's having a "SharePoint Document Locked for Editing" issue while working with SharePoint documents. Most of the time, it's a valid warning message, which notifies that someone else might edit the document during the same period of the time.

Some other time, it turns out that there is no current user having this document open for editing, and just couldn't help wondering how this could be possible?

Under the hood, this issue could be caused by the following scenarios:

  1. The Microsoft Office products cashes while you were working on that particular document
  2. PC crashes while the document is open
  3. Loss of network connection (network issue) while document is open
  4. And more

Well, there is a few quick fixes out there to work around this issue by removing the local copy of the file under the local user account CacheFolder. However, the major downside of this approach is to require the access to the user PC, in order to remove the cached copy from there.

Therefore, what if that particular user doesn't show up for work that day, or we couldn't get hold of the user via the phone/email?

The solution hereby is to build a small program to unlock the document/file against SharePoint Content Database. (I'm fully aware of that direct updating SharePoint Content database is not encouraged by Microsoft SharePoint Best Practice Guide), however, if you know how AllDocs table works and make sure that your query will still keep the referential integrity of the data, this approach might work perfectly for you in this case.

The type of the application itself could vary (windows form/console/web applications) based upon your speciality and strength, the magic here is the code snippet below, by which it updates the values from CheckoutExpires Column in order to drop the lock of the file.

private void UpdateItemCheckoutExpiration(SPListItem item)
        {
    SqlConnection contentDatabaseConnection = null;
    try
    {
        contentDatabaseConnection = 
          new SqlConnection(item.Web.Site.ContentDatabase.DatabaseConnectionString);
        contentDatabaseConnection.Open();

        string updateCommandText = string.Format(
            "UPDATE dbo.AllDocs SET CheckoutExpires " + 
            "= '{0:yyyy-MM-dd HH:mm:ss:fff}' WHERE Id = '{1}'",
            DateTime.Now.ToUniversalTime(), item.UniqueId);

        SqlCommand updateCommand = 
          new SqlCommand(updateCommandText, contentDatabaseConnection);
        SqlDataAdapter contentDataAdapter = new SqlDataAdapter();
        contentDataAdapter.UpdateCommand = updateCommand;
        contentDataAdapter.UpdateCommand.ExecuteNonQuery();
        contentDatabaseConnection.Close();
    }
    catch (Exception)
    {
        // handle exception here
    }
    finally
    {
        if (contentDatabaseConnection != null && 
            contentDatabaseConnection.State != ConnectionState.Closed)
            contentDatabaseConnection.Close();
    }
}

Once the query has been executed against the SharePoint database, the file itself will be release instantly. Therefore, by having this awesome software, we won't worry about some naughty users open the documents without checking it out and go for lunch, then there's no way force it free if you can't get hold of their PCs.

Happy SharePointing Wink | ;)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


Comments and Discussions

 
SuggestionUnsupported PinmemberDev-Guru21-Aug-12 8:47 
QuestionArticle Reference Pinmemberjabit14-Nov-11 7:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411023.1 | Last Updated 7 Sep 2011
Article Copyright 2011 by Eric Xue (brokensnow)
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid