What is the best approach if i want to write custom code for uploading files to a document library in WSS 3.0 ?

I have few custom columns also in my document library. So, i would need that also as an input along with the file to be uploaded.

Thanks in advance.

2 Answers 2


It is of course possible to upload files by code. There are 2 ways:

  • Either you use full object model, so as it was said you can create a webpart and upload the files from there one by one, or you can say here is the location to the folder and upload all of the files in that folder.

  • Or you can write client side code, I'm talking now about client side application, it can just be a console .Net application which would use SharePoint API and also for example by the parameters passed to a folder upload all files in the folder.

Basically everything is possible in your situation.

Here is just some sample which you can start from:

// Getting a reference to the document library 
using(SPSite site = new SPSite("http://localhost"))
using(SPWeb web = sp.OpenWeb())
var folder = web.GetFolder("Documents");
var files = folder.Files; 

// Opening a filestream 
var fStream = File.OpenRead("C:MyDocument.docx"); 
var contents = new byte[fStream.Length]; 
fStream.Read(contents, 0, (int)fStream.Length); 

// Adding any metadata needed 
var documentMetadata = new Hashtable {{"Comments", "Hello World"}}; 

// Adding the file to the SPFileCollection 
var currentFile = 
    files.Add("Documents/MyDocument.docx", contents, documentMetadata, true); 


I hope it was helpful, happy coding!


Is there any special reason you want to write custom code for it? And what do you mean exactly by saying custom code btw? If you want to create a form like structure where all custom columns and uploading file is in same page (contrary to sharepoint default upload first and then form) only then you can consider writing your own form, in that case I would suggest design the form with custom control (ascx), write a webpart and in the same feature deploy the ascx also, load the ascx in the webpart and apply logic and fill custom column, upload the file with object model, it's not a tough task

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.