I have a document library in SharePoint 2010 to which a user is uploading spreadsheets via a custom web part.

These spreadsheets are later processed by another custom web part and updated. A user could also potentially go to the document library and directly edit the spreadsheet as well (in the case that it contains errors that need to be fixed before it can be processed). We wish to track each version of the document so "Versioning - Major Versions Only" is enabled. To ensure that the user updates the document in the document library and puts a checkin comment, I set "Require documents to be checked out before they can be edited?" to Yes.

[Edit]: We also want to ensure that the document isn't edited whilst the code is reading and writing to the file.

The code in the web part that processes the spreadsheets retrieves the list of documents from the document library then loops through them one at a time to process them. At the start of the loop, the code attempts to perform a Check Out on the file but receives an error stating that the file is already checked out by that user and thus cannot be processed.

If I set "Require documents to be checked out before they can be edited?" to No, then this error goes away and the web part can check out the file and process it without any issues. As such, my question is this:

When does SharePoint check out the file when "Require documents to be checked out before they can be edited?" is set to Yes?

3 Answers 3


You might be better off looking at the file locking mechanism built into SharePoint, particularly if your files are MS Office files.


If you obtain a lock on a file before you start your processing, then you can be sure that another user will not be able to edit that file. Conversely, if a user has the file open in edit mode in the office client, they will have a lock on the file and thus your processing will have to wait until the file lock is released by the user (by closing the file).

This mechanism is similar, but independent of the checkout/checkin function of SharePoint.

  • I've looked at SPFile.Lock() previously and from what I understood this would work similarly to checkout, wouldn't it? In which case I would no doubt encounter the same issue where SharePoint is checking out the files before my processing code manually does so if I forced the documents to be checked out, correct? I'm assuming the problem is to do with placement of the code to check out. I loop through the files in my document library and perform checkout on each one before opening them (and checkin as I finish with each file)
    – B-K
    Nov 22, 2011 at 3:47
  • Not really, the lock doesn't have anything to do with checkout/in so it doesn't matter whether you have require checkout turned on or not. The lock will either a) prevent another user from modifying the file while your code is modifying it, or b) prevent your code from modifying the file while another user is editing it.
    – Paul Lucas
    Nov 22, 2011 at 5:53
  • Having said that though, if a user has the file checked out (or locked), you won't be able to lock the file yourself and will have to wait until it becomes available.
    – Paul Lucas
    Nov 22, 2011 at 6:01

Are you refering to the 'Require documents to be checked out before they can be edited?' setting?

If so it is to ensure that only ONE user at a time can edit a document. So when somebody checks out a document in order to change it and then does not check it back in your code can either undo the check-out (which will loose all changes that user made to the document and its' metadata) or it cannot not modify the document.

The setting has nothing to do with versioning at all. So if you want to make sure your code works smoothly you can simply deactivate it and keep the Major versioning.

  • Yes, that's the setting I'm referring to. Have updated my post above with the correct name now. Ideally we'd like to prevent people from editing files that are currently being processed so we can be sure of which version of the file was processed. Do you know at what point SharePoint is checking out the file?
    – B-K
    Nov 20, 2011 at 22:41
  • I think that Paul's solution with a lock on the SPFile is the way to go in your scenario... Nov 21, 2011 at 9:14

When this option is set to Yes, SharePoint checks out a file when it is uploaded (created).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.