Friday, 30 August 2013

Upload file to document library using SharePoint 2010 obect model

using (SPSite site = new SPSite("http://SiteURL"))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;

SPFolder folder = web.Folders[site.URL + "/Shared Documents/"];
byte[] content = null;

using (FileStream filestream = new FileStream(@"C:\MyDocument.docx",System.IO.FileMode.Open))
{
content = new byte[(int) filestream.Length];

filestream.Read(content, 0, (int) filestream.Length);

filestream.Close();
}

SPFile file = folder.Files.Add("MyDocument.docx", content, true);

}
}

No comments:

Post a Comment