Saturday, December 12, 2009

Uploading File to Document Library Using ASP.Net

1. Add reference to Microsoft.Sharepoint.Dll
2. [Code Behind ]

using (SPSite site = new SPSite("YourSite"))
{
using (SPWeb web = site.OpenWeb())
{
SPFolder folder = web.GetFolder(YourDocumentLibrary);
SPFileCollection files = folder.Files;
FileStream fStream = File.OpenRead(OfflineUploadCtrl.PostedFile.FileName);
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
Hashtable MetaDataTable = new Hashtable();
MetaDataTable.Add("Comments", "MyFirstUpload");
SPFile currentFile = files.Add(YourDocLibraryPath + OfflineUploadCtrl.FileName, contents, MetaDataTable, true);
}
}

3. UI Code
<asp:FileUpload ID="OfflineUploadCtrl" runat="server" />
<asp:Button ID="UploadButton" runat="server" Text="Upload"
onclick="UploadButton_Click" />

1 comment: