Click here to Skip to main content
Click here to Skip to main content

Uploading, Deleting, and Downloading a File from SharePoint 2003 Document Library

By , 17 Nov 2007
Rate this:
Please Sign up or sign in to vote.


This article demonstrates how to upload, delete, and download a file from SharePoint 2003 site's Document Library. I have not posted MY solution HERE because it was a bit tricky as I was maintaining some information in my own database too. This would have confused a general user.


I was assigned a project to make a document management system using SharePoint Portal Server. The user's requirement was that the file should be saved in the SharePoint document library and the custom application should be hosted under a SharePoint site only. I Googled around to find a procedure for uploading, deleting, and downloading file from a SharePoint library but none of the code worked in my case; most of the code I found was for SharePoint Server 2007. So I decided to do some research and came up with this code.

Using the code

First of all, you have to include a reference to the Microsoft.SharePoint library and then include the following namespaces in your code file for accessing SharePoint.

using Microsoft.SharePoint; 
using Microsoft.SharePoint.Utilities; 
using Microsoft.SharePoint.WebPartPages; 
using Microsoft.SharePoint.WebControls;

Code for uploading file to document library

// Make an object of your SharePoint site.
// Here sharePointURL is the url of your SharePoint site
SPSite site = new SPSite(sharePointURL); 
SPWeb web = site.OpenWeb(); 
string path ="";  
// here filePath is html file control with runat="server"
// being used to pick the file browsed by user   

string []fileName =filePath.PostedFile.FileName.Split('\\');
int length = fileName.Length;  
// get the name of file from path
string file=fileName[length-1];
//  Get the library object where LibraryName is the name of Document Library.
SPFolder folder = web.GetFolder(LibraryName);  
SPFileCollection files=folder.Files;  
// Pick up the file in binary stream
Stream fStream = filePath.PostedFile.InputStream;  
// Read file in a byte array
byte[] MyData= new byte[fStream.Length];
fStream.Read(MyData, 0, (int)fStream.Length);
SPFile fff= files.Add(file,MyData);

Now if you want, you can maintain an ID at your end the object, fff would be referring to the uploaded file and you can get the ID of newly uploaded file as fff.Item.ID.

In case you have enabled versioning on your document library and a file with same name you are uploading already exists, it will create a version of the existing file and return an object to it; in this case, you get the ID of the existing file.

However, if you have disabled versioning and a file with same name already exists, then you will get an error in uploading the file.

Code for deleting a file from the document library

//Make an object of Site
SPSite site = new SPSite(sharePointURL); 

SPWeb web = site.OpenWeb(); 

//Get the Document Library object
SPFolder folder = web.GetFolder(LibraryName);
//Get the files in the document Library
SPFileCollection files=folder.Files;
//If you dont know the url of file and have maintained only the id of file then 
//Loop through all files to get the desired file and then get url from it
for(int i=0; i< files.Count;i++)
   SPFile tempFile= files[i];
     //if you are maintaing the url of file then you can use this 
     //directly instead of looping through all objects to get file url
     //in that case you just have to pass the url of file intead of tempFile.Url 

Code for downloading a file from the document library

One method of downloading a file is to provide a direct link to the file in the SharePoint Document Library. Let us say that your site URL is http://xyz, and our document library name is doclib, and the file which you want to download is testFile.doc. Now all you have to do is provide a link targeting the URL of file, which is http://xyz/doclib/testFile.doc. When the user clicks on this link, SharePoint will handle the request and download the file. But in many cases, SharePoint opens the file for editing instead of prompting for download so another technique is to get the file in the byte stream and write it on the response stream.

    int flag=0;
    SPSite site = new SPSite(sharePointURL); 
    SPWeb web = site.OpenWeb(); 

    string strContentType=""; 
    // docLib is the name of document library
    SPFolder folder = web.GetFolder("docLib");

    SPFileCollection files=folder.Files;
    //"docLib" is name of document library and testFile.doc is the name of file
    string url=sharePointURL+"/"+"docLib"+"/"+"testFile.doc"

    SPFile tempFile = web.GetFile(url);

    //Get the extension of File.
    string []fext=this.filename[0].Split('.');
    byte []obj=(byte[])tempFile.OpenBinary();

    // Get the extension of File to determine the file type
    string casestring="";
        casestring= fext[fext.Length-1];
    //set the content type of file according to extension
        case "txt": 
            strContentType = "text/plain";
        case "htm" : strContentType = "text/html";
        case "html" : strContentType = "text/html";
        case "rtf" : strContentType = "text/richtext";
        case "jpg" : strContentType = "image/jpeg";
        case "jpeg": strContentType = "image/jpeg";
        case "gif" : strContentType = "image/gif";
        case "bmp" : strContentType = "image/bmp";
        case "mpg" : strContentType = "video/mpeg";
        case "mpeg": strContentType = "video/mpeg";
        case "avi" : strContentType = "video/avi";
        case "pdf" : strContentType = "application/pdf";
        case "doc" : strContentType = "application/msword";
        case "dot": strContentType = "application/msword";
        case "csv" : strContentType = "application/vnd.msexcel";
        case ".xls": strContentType = "application/vnd.msexcel";
        case ".xlt": strContentType = "application/vnd.msexcel";
        default : strContentType = "application/octet-stream";
             "attachment; filename= "+filename[0]);
    Response.ContentType = strContentType;
    //Check that the client is connected and has 
    //not closed the connection after the request
catch(Exception ex)

Points of interest

SharePoint treats the document library as a folder so we have used this to perform all these tasks. My project required the mapping of documents to my custom categories and there can not be two files with the same name in a category. I was using only one document library and there could not be files with same name in a document library as this will create a version if versioning is enabled, so to make the filename unique, I inserted the category ID in the file name before the extension. This made my file name unique and I was able to upload files with the same name in two different categories and my requirement of a unique file in a category was also met.

For all this code to execute, the currently logged in user should have upload, delete, and download permissions in the respective document library.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Shujaat Ullah Khan
Software Developer
India India
Shujaat Ullah Khan is Currently Working with Swash Convergence Technologies as a Senior Software Developer.
His working Technologies include ASP.NET 1.1, ASP.NET 2.0, SQL Server 2005, DotNetNuke, SharePoint Portal Server 2003,MOSS 2007 and windows SharePoint Services 2.0,3.0.

Comments and Discussions

Questionthanks Pinmemberlakshman rao29-Mar-13 1:09 
GeneralMy vote of 5 Pinmemberasifiqbaljh16-Jan-13 19:59 
GeneralRe: My vote of 5 PinmemberShujaat Ullah Khan24-Jan-14 4:15 
Questiondownloading file problem PinmemberMember 80649936-Jul-11 21:21 
I am also facing the same issue, after downloading nothing can be done untill the page is refreshed.
please suggest the solution.
GeneralIgnoring best practices Pinmembersumitkm27-Apr-09 8:37 
GeneralRe: Ignoring best practices PinmemberShujaat Ullah Khan24-Jan-14 4:08 
GeneralYour code on VS 2008 Pinmembershnbe9-Apr-09 9:28 
GeneralRe: Your code on VS 2008 PinmemberShujaat Ullah Khan11-Apr-09 9:09 
QuestionGet List Of Project Documents from share point 2007 using .net Pinmemberkvinodreddy8-Feb-08 1:54 
GeneralRe: Get List Of Project Documents from share point 2007 using .net PinmemberShujaat Ullah Khan8-Feb-08 2:21 
QuestionDownloading file problem Pinmembermaxwell_hung11-Oct-07 23:06 
AnswerRe: Downloading file problem PinmemberShujaat Ullah Khan17-Oct-07 18:41 
GeneralRe: Downloading file problem Pinmemberthewire.net3-Jul-08 2:58 
GeneralRe: Downloading file problem PinmemberShujaat Ullah Khan3-Jul-08 3:31 
GeneralRe: Downloading file problem Pinmemberlakshmpc13-May-10 2:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 17 Nov 2007
Article Copyright 2007 by Shujaat Ullah Khan
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid