Couple of weeks ago i got a requirement from the customer that the files/documents have to be checked-out by the user before editing it,so that the history can be kept tracked off .Initially my thought was straightforward, i went ahead to the Library settings >Versioning settings and changed the Require Checkout option to ‘Yes’ , this makes sure that the user has to checkout before editing any of the document.
To my surprise, there was a code for uploading files to library and this was unsuccessful after changing the above setting. I was getting exception : The file is not checked out. You must first check out this document before making changes.Below is the fix.This fix will check out and check-in the file automatically when its uploaded.
1. Check if the require checkout option is true or false.
SPDocumentLibrary has a property called ForceCheckout to get the above set value.
bool bCheckoutRes = myDocumentLibrary.ForceCheckout ;
2. if the option is set to ‘Yes’ ,checkout the file after its been added to the library.
SPFile file = myDocumentLibrary.Files.Add(” “);
3. Now check-in the above checked out file after calling ListItem.Update()
file.Checkin(“Checkin file programatically”);