C# – Upload File To SharePoint Online(office 365) Library Invokes Error

coffice365sharepoint

How can one resolve this runtime error?

Error

Could not load file or assembly 'Microsoft.SharePoint.Library, Version = 14.0.0.0, Culture = neutral, PublicKeyToken = 71e9bce111e9429c' or one of its dependencies. The system can not find the file specified.

CODE

string destUrl = "URL";
string destFileUrl = destUrl  + "biblioteca" + "/text.txt";
using(SPWeb site = new SPSite(destUrl).OpenWeb())
{
    site.AllowUnsafeUpdates = true;

    FileStream fileStream = File.Open("FILE" , FileMode.Open);

    site.Files.Add(destFileUrl, fileStream, true/*overwrite*/);

    fileStream.Close();
}

Best Answer

This works for me.

using (ClientContext clientContext = new ClientContext("SHAREPOINT URL")) {
    SecureString passWord = new SecureString();
    foreach (char c in "PASSWORD".ToCharArray()) passWord.AppendChar(c);
    clientContext.Credentials = new SharePointOnlineCredentials("ACOUNT.onmicrosoft.com", passWord);
    Web web = clientContext.Web; 
    FileCreationInformation newFile = new FileCreationInformation();
    newFile.Content = System.IO.File.ReadAllBytes(FILE);
    newFile.Url = NAMEFORTHEFILE;
    
    List docs = web.Lists.GetByTitle("LIBRARY NAME");
    Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);

    clientContext.ExecuteQuery();
}