Uploading a file to a SharePoint library from C# code

I needed a way for a user to be able to upload a file to a SharePoint library from a custom web part.

While, the post here: http://elczara.spaces.live.com/blog/cns!554EC06D366AC9D5!692.entry?wa=wsignin1.0&sa=891066527 helped get me started, it only worked when I was on my localhost (and only in IE for whatever odd reason)

Turns out, I needed to modify things a bit so that the file was first uploaded to the server, then copied to the SPList and then I could delete the file from the temp directory.

The code below hasn’t been rigorously tested and I need to add some restraints on what types of files can be uploaded. It creates a subfolder, if needed, in the SharePoint library as well.:

public void UploadFile(NewFile file)
string sFileName = flUpload.PostedFile.FileName;

string sListFolder = System.Configuration.ConfigurationManager.AppSettings[“DocumentLibrary”];
using (SPSite spsite = new SPSite(sRootSiteURL + sPrimarySiteCollectionURL))
//reusing the spweb object doesn’t grab the new folder from the list.
using (SPWeb spweb1 = spsite.OpenWeb())
//create the folder
spweb1.AllowUnsafeUpdates = true;
SPFolder spfolder1 = spweb1.Folders[sRootSiteURL + sPrimarySiteCollectionURL + sListFolder];
bool bCreate = true;
foreach (SPFolder subFolder1 in spfolder1.SubFolders)
if (subFolder1.Name == file.FileID.ToString())
bCreate = false;
if (bCreate == true)
SPList splist = spweb1.Lists[sListFolder];
SPListItem splistitem = splist.Items.Add(splist.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, file.FileID.ToString());
spweb1.AllowUnsafeUpdates = false;

using (SPWeb spweb = spsite.OpenWeb())
//first save it to a temp location
string serverFileName = Path.GetFileName(flUpload.PostedFile.FileName);
flUpload.PostedFile.SaveAs(@”c:\tempfiles\” + serverFileName);
string sFileLocation = @”c:\tempfiles\” + serverFileName;

//create the file
spweb.AllowUnsafeUpdates = true;

SPFolder spfolder = spweb.Folders[sRootSiteURL + sPrimarySiteCollectionURL + sListFolder];
byte[] content = null;

using (FileStream filestream = new FileStream(sFileLocation, FileMode.Open))
content = new byte[(int)filestream.Length];
filestream.Read(content, 0, (int)filestream.Length);
string delimStr = @”\”;
char[] delimiter = delimStr.ToCharArray();

string[] arrFileName = sFileName.Split(delimiter);
sFileName = arrFileName[arrFileName.Length – 1];

foreach (SPFolder subFolder in spfolder.SubFolders)
if (subFolder.Name == file.FileID.ToString())
//Upload file in subfolder.
SPFile spfile = subFolder.Files.Add(sFileName, content, true);

//Delete the file from the temp directory
spweb.AllowUnsafeUpdates = false;


catch (Exception ex)
Utils.WriteToEventLog(ex.ToString(), “UploadFile”, EventLogEntryType.Information, “Application”);


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s