Uploading Files to Document Library SharePoint Online

If you want to upload a document to a share point library in a online environment 2013, here are the server side code to achieve this requirement.
 
string Url = "http://mysite/sites";
string libraryName = "My Documents";
string fileName = "c:\\sample.docx";
using (ClientContext clientContext = new ClientContext(Url))
{
    SecureString passWord = new SecureString();
    foreach (char c in "Password".ToCharArray()) passWord.AppendChar(c);
    clientContext.Credentials = new SharePointOnlineCredentials("[email protected]", passWord);
    List list = clientContext.Web.Lists.GetByTitle(libName);
    clientContext.Load(list.RootFolder);
    clientContext.ExecuteQuery();
    using (FileStream fileStream = new FileStream(fileName, FileMode.Open))
    {
        Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, list.RootFolder.ServerRelativeUrl.ToString() + "/" +         fileName.Split('\\')[1], fileStream, true);
    }
}
Happy SharePointing :-) 
Ebook Download
View all
Learn
View all