Tuesday, May 12, 2009

Programmatically Upload documents to SharePoint Document Library

Scenario: Upload a document (any type) to SharePoint Document Library programmatically.

How? : Use the below code.

Parameters:
1. docName: Name of the document with which you want to upload to the library
2. content: byte[] content of the document you want to upload
3. siteName: Site where you want to upload the document
4. docLibName: Document Library Name to which you want to upload the document


///
///
///

/// Name of the document
/// byte format of the document
/// site name
/// sharepoint document library name
private void UploadDocumentToSharePointLibrary(string docName, byte[] content, string siteName, string docLibName)
{
using (SPSite site = new SPSite(siteName))
{
using (SPWeb web = site.RootWeb)
{
SPFolder docFolder = web.Folders[docLibName];
SPFile file = docFolder.Files.Add(docFolder.Url + "/" + docName, content, true);

file.Update();
docFolder.Update();
}
}

}

No comments: