Thursday, September 21, 2006

Sharepoint 2003 : Unlocking longterm checked-out documents

An user asked me if I could take care of a document that was locked, being the sharepoint administrator that I am, I figured this could be solved by checking in the document and discard the changes. But Sharepoint told me that the document was not checked out and if I wanted to check it out it gave the following error :

"The file [filename] is checked out or locked for editing by [username]"

After some googling I found out that there were two types of checking-out:

Long term check out
This is the explicit check out . 
This holds a lock on the document whether you have it open for edit or not.

Short term check out
You get this feature implicitly. 
If you open a document for editing, you get a short term lock on the document to prevent other people from editing the doc while you are. 
The Office client applications refresh this lock periodically as long as you keep the document
open.  Once you close the document, your short term check out is released

Kudos to Mike Walsh's FAQ for the info!

Also, mentioned in the following KB-article, if your application crashes a short term lock of 10mins will occur

Unfortunatly I could not check in the file and the user who did check out the file could not do it either.
So I wrote some code to force the check-in.. only problem is that a new version will be written and the 'modified date' and 'modified by' metadata will be overwritten.

 

 

SPSite mySiteCollection = new SPSite("teamsitelink");
SPWeb web = mySiteCollection.RootWeb;
SPListCollection lists = web.Lists;

foreach (SPList list in lists)
{
	if (list.Title == "Document Library")
	{
		SPListItemCollection docLibItems = list.Items;

		foreach (SPListItem docLibItem in docLibItems)
		{
			if (docLibItem.File.CheckOutStatus == SPFile.SPCheckOutStatus.LongTerm)
			{
				Console.WriteLine(docLibItem.File.Name.ToString());
				try
				{
					docLibItem.File.CheckIn("");
				}
				catch (Exception error)
				{
					Console.WriteLine(error.Message.ToString());
				}
			}
		}
	}
}
web.Dispose();
mySiteCollection.Dispose();

10 comments:

Anonymous said...

Robin,

Thanks for the information. I have a similiar issue that I was hoping you might have a solution or suggestion about. I have given certian user's read permision only. I however have noticed that sharepoint 2003 still allows them to open the document residing on the server thus locking it from editing from authorized user with write permision. I was wondering if there is a method that can force that can force a read only user to have to open a copy on the local drive of his or her own machine? I would greatly appreciate any insight you or your colleages may have on this matter.

Robin Meuré said...

Hi,

sorry about the delay in my reply. But if I'm not mistaken, a user who has read-only rights who will open a document. The document will be openend in readonly mode and thus not enforcing a short term lock.

Anonymous said...

What Visual Studio Project Should I use to run this code? How can I implement this in SharePoint 2007?

I have Visual Studio 2005, but don't know how to easily run this code snippet to unlock the file.

Please help. Thanks.

Anonymous said...

Actually, it's not this complicated. Open the library in SharePoint Designer, right-click on the locked document and select Check-In on the context menu.

Thien Ton said...

See relate post for this in http://social.msdn.microsoft.com/Forums/en-US/tfsgeneral/thread/d1d5a9bd-9f37-418f-8ed6-8bb70a3d120b?prof=required

Use SQL Server Management to connect to your WSS_Content database do select your files using this select:
(select * from alldocs where dirName like "directory to your file"%' and checkoutuserid = "user")

Update the checkoutuserid, checkoutdate, and checkoutexpires to NULL.

JohnLBevan said...

Hi Robin,

I just found another fix, which seems to work OK for me in the same situation.

Click Site Actions > Site Settings
Under Site Administration, select Content and Structure
Navigate to the relevant doc library
Find your document, hover your mouse over the name field, then select Discard Checkout from the drop down list.

The other method of managing checked out files is to go the the list & select Settings, Document Library Settings. Next, click "Manage checked out files" under "Permissions and Management"; I believe that's the method you had been using previously?

Hope that helps,

JB

Anonymous said...

I exported the list with contents as a template, created a new list from the template, and everything worked. I then deleted the original list with the bad file.

Rob said...

Stumbled accross this post whilst trying to resolve an issue I was having with being unable to check out a document that was not checked out or locked locally. I eventually found the cause to be that we had reached SharePoint's inherent limit of 511 minor draft versions and the fix was to publish a major version. Switching off 'Require checkout' on the versioning setttings revealed the underlying problem.

Anonymous said...

I ecountered the same issue on SP2003, and I went to the General Setting and uncheck Version control and re-enable the version control again, then the file can be opening again without telling you it was locked by a user.

public key infrastructure said...

Very informative post. I sometimes do presentations on SharePoint and was wondering if I could use your Print List example in my presentations and refer my audience to your website for further info.