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

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.

share|improve this question

2 Answers 2

up vote 0 down vote accepted

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); 
fStream.Close(); 

// 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!

share|improve this answer

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

share|improve this answer

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.