Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a question. How can I add a file without checking it in directly?

I add file like this:

Library.RootFolder.Files.Add(fileName, buffer, overwrite);

But when I do that I suppose the file is checked in by default, but how can i add it without checking it in?

Any idea how to do it?

PS: i dont want to add as checked in and then check out.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

If the library requires check-out for editing documents then your code should upload the document without checking it in if you set the last parameter to False.

If the library does not require check-out for editing documents or you pass True for the last parameter then your code will automatically check-in the file after uploading.

If it's acceptable in your case change the library's versioning settings to require check-out and upload the documents with your code by passing False for the last parameter.

Library.RootFolder.Files.Add(fileName, buffer, false);

That's how I do that in one of my tools. The tool is for 2010 but should be the same behaviour for 2007.

EDIT:

For SP 2010, SP 2013:

If there are required fields in the list then you can use SPFileCollectionAddParameters as below

System.IO.FileStream fileStream = null;
Byte[] fileContent = null;
string filePath = @"C:\.....\TestFile.docx";
string fileName = "TestFile1";
SPList lib = web.Lists["ListName"];
fileStream = System.IO.File.OpenRead(filePath);
fileContent = new byte[Convert.ToInt32(fileStream.Length)];
fileStream.Read(fileContent, 0, Convert.ToInt32(fileStream.Length));
SPFileCollectionAddParameters parameters = new SPFileCollectionAddParameters();
parameters.AutoCheckoutOnInvalidData = true;
parameters.CheckRequiredFields = true;

SPFile uploadedFile = library.RootFolder.Files.Add(lib.RootFolder.Url + "/" + fileName, fileContent, parameters);

Just tested and it works. If you don't have required fields then the file will be checked in automatically but that also happens when adding new documents using the UI.

UI doesn't check in the document automatically only if there are required fields.

I think OOTB functionality of adding documents is using SPFileCollectionAddParameters too because the behaviour is the same.

For 2007:

For MOSS 2007 there is no SPFileCollectionAddParameters. There are other overloads of SPFileCollection.Add that have bool parameter checkRequiredFields. (not tested)

share|improve this answer
    
the third parameter is not about the checkin or checkout it is boolOverwrite –  Shkipper Feb 13 at 7:40
    
I know. I never discovered why its this behaviour for a document uploading for the first time with fresh name but it worked for me. –  Naim Feb 13 at 7:43
    
It is just so that even if the library doesn't required check in - check out, IF i add a document manually it is by default is checked out, and then i can check in, so i need the same but programmatically –  Shkipper Feb 13 at 7:55
    
Sorry, my workaround works only for libraries which require checkout. Documents are checked out state (never checked in) by default only when you create them with Office or drag-drop with Explorer View. It seems SPFileCollection.Add(..) has the behaviour like when you upload a file with "Upload Document" ribbon button, in which case document is automatically checked in. –  Naim Feb 13 at 8:16
    
Not really i uploaded some document to a library which doesn't require check in, it was a text document and it was checked out :/ –  Shkipper Feb 13 at 8:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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