Upload Document to SharePoint Document Library

private void UploadDocument()
using (SPSite site = new SPSite(SPContext.Current.Web.Url))
using (SPWeb web = site.OpenWeb())
SPDocumentLibrary documentLib = web.Lists.TryGetList(“Document Libarry name”) as SPDocumentLibrary;

Stream fStream = DocumentFileUpload.PostedFile.InputStream; // get Input stream from Document Library

byte[] _byteArray = new byte[fStream.Length];

fStream.Read(_byteArray, 0, (int)fStream.Length);


web.AllowUnsafeUpdates = true;

string _fileUrl = documentLib.RootFolder.Url + “/” +Path.GetFileNameWithoutExtension(DocumentFileUpload.PostedFile.FileName) + Path.GetExtension(DocumentFileUpload.PostedFile.FileName); // Set Document uploaded url

bool IsOverwriteFile = true; // Overwrite document true

SPFile file = documentLib.RootFolder.Files.Add(_fileUrl, _byteArray, IsOverwriteFile);

SPListItem item = file.Item;



web.AllowUnsafeUpdates = false;




Author: eshan1604

Hi Guys, I'm Udara Eshan Ariyarathne from Sri Lanka. Currently i'm working as a Software Engineer (Microsoft Technologies). I'm sharing some IT related articles such as Development ,Configuration and Designing things what i have learn from day to day works. Hope it useful all of us. "Enjoying IT" Thanks & Regards, Udara Eshan Ariyarthne

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 )

Google+ photo

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

Twitter picture

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

Facebook photo

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


Connecting to %s