Wednesday, May 23, 2012

SPDocumentLibrary.CheckedOutFiles? A Common Misunderstanding

What is a checked out file?
Any file which locked for the changes are not visible to the other users. In SharePoint, we have a option to check out a file.
Go to the library, open a file's ECB (Edit Control Block) menu options and clik "Check Out" menu item.

So any file, which is checked out this way, is a checked out file. Isn't it? Yes, it is checked out file.

Now, try this.
Write a small console application, which will display count of SPDocumentLibrary.CheckedOutFiles. In case of SharePoint 2010, a small PowerShell script will do.

Execute the utility and note down the count of Checked Out files. Go to a document library and check out one file. Re-execute the utility and read the count...It does not change.Try again with two-three files. It does not change the count of SPDocuemntLibrary.CheckedOutFiles.

 MSDN describes "SPDocumentLibrary.CheckedOutFiles" as "the collection of files that are uploaded to the document library but are not checked in."

If the document library requires check out before editing any file and you upload a file to this library...the file will added in a checked out state. The file does not have a checked in version available. The file will not be visible to any one before check in.

SPDocumentLibrary.CheckedOutFiles represent the collection of files, which do not have a checked in version.

"Requires Check out" setting is available in "Version Settings" of library.

Can we override Check out of these files from Object Model?
SPDocumentLibrary.CheckedOutFiles is a collection of SPCheckedOutFile objects. SPCheckedOutFile does not derive from any class. SPCheckedOutFile does not have any member to check in the file.

SPCheckedOutFile has a property "ListItemId". If we exploit it to retrieve SPFile object, then we can check in the file. SPFile has a method "CheckIn". Code must be executed with the credentials of the user who has the ownership of these files. As the files are not visible to others, code with other user credentials fails to retrieve the SPFile object for these files.

SPCheckedOutFile type has a method "TakeOverCheckOut". If we use this method, you will have the file ownership and then we can retrieve the SPFile object for check in purpose.

Can we override check out of these files from UI?
These files are only visible to the user who has uploaded the files. Then how to override the check out for these files.
Go to the library settings, you will see a link "Manage Checked Out Files" in "Permissions and Management" category. MOSS 2007 and SharePoint 2010 has different versions of the link text as  follows.
MOSS 2007:
SPS 2010:
SharePoint 2010 corrected the confusing text.

So click on this link, and you can see the files which do not have checked in version. The UI provide the option to take the ownership of these files. Take the ownership of this files and from library view, you can check in/ delete the files. Taking ownership of these files, makes the files visible to you only and other users can not see the files.

Files checked out which have checked in version
Any file which is explictly checked out from the visible to all with check out status. It can be retrieved as SPFile object. SPFile has properties to read the details of check out. These details are also presented in UI.

SPFile.CheckOutStatus property indicates whether the file is checked out or not. There are other members of SPFile which gives details about check out. e.g.CheckedOutBy, CheckedOutDate etc.

As the files are visible to all, we can easily override the check out of these files from UI or Object Model code.

To summarize
SPDocumentLibrary.CheckedOutFiles represents files which does not have checked version. This property does not count the files, which are explictly checked out. This property should not be used to report the number of checked out files.

Use loop in object model and count the files with CheckedOutStatus turned ON.  Then add the count of SPDocumentLibrary.CheckedOutFiles.

In UI, create a view of checked files, note the count of files. Visit "Manage Checked out files" link in library settings, count the files. Add the counts of these files.
Post a Comment