Programatically upload file to SharePoint list

Code sample online aren't good for this. Hope this helps.
/*
Reference: Microsoft.SharePoint.dll
using Microsoft.SharePoint
Upload a file to a SharePoint document library. Many examples on the web seem to first
convert a filestream to a byte array (?). You can save yourself some trouble by just
passing in the stream.
Note: if you're confused about the arguments here, just browse to your document library
and look at the URL. If it's "http://mysite/Test/My Doc Lib", then the SiteUrl is as below
*/
private void UploadFileToSharePoint(string strInputFileName, string siteUrl, string docLibName)
{
    string destUrl = siteUrl + "/" + docLibName;
    string destFileUrl = destUrl + "/" + strInputFileName.Substring(strInputFileName.LastIndexOf("\\") + 1);

    SPWeb site = new SPSite(destUrl).OpenWeb();

    site.AllowUnsafeUpdates = true;

    FileStream fileStream = File.Open(strInputFileName, FileMode.Open);

    site.Files.Add(destFileUrl, fileStream, true/*overwrite*/);

    fileStream.Close();
}
cf http://forums.asp.net/t/1164532.aspx.
| Comments (0)TrackBacks (0)

0 TrackBacks

Listed below are links to blogs that reference this entry: Programatically upload file to SharePoint list.

TrackBack URL for this entry: http://www.rootsilver.com/mt-tb.cgi/76

Leave a comment