1.PnP PowerShell
#Config Variables
$SiteURL = "https://tenant.sharepoint.com/sites/test"
$SourceFilePath ="C:\document.docx"
$DestinationPath = "Shared Documents" #Site Relative Path of the Library
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -interactive
#powershell pnp to upload file to sharepoint online
Add-PnPFile -Path $SourceFilePath -Folder $DestinationPath
2.CSOM
string userName = "xxx@xxxx.onmicrosoft.com";
string password = "xxx";
var securePassword = new SecureString();
foreach (char c in password)
{
securePassword.AppendChar(c);
}
using (var clientContext = new ClientContext("https://tenant.sharepoint.com/sites/test"))
{
clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);
Web web = clientContext.Web;
clientContext.Load(web, a => a.ServerRelativeUrl);
clientContext.ExecuteQuery();
List documentsList = clientContext.Web.Lists.GetByTitle("Shared Documents");
var fileCreationInformation = new FileCreationInformation();
//Assign to content byte[] i.e. documentStream
fileCreationInformation.Content = System.IO.File.ReadAllBytes(@"C:\document.docx");
//Allow owerwrite of document
fileCreationInformation.Overwrite = true;
//Upload URL
fileCreationInformation.Url = "https://tenant.sharepoint.com/sites/test/" + "Shared Documents" + "/documents.docx
";
Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(fileCreationInformation);
//Update the metadata for a field having name "DocType"
uploadFile.ListItemAllFields["Title"] = "UploadedviaCSOM";
uploadFile.ListItemAllFields.Update();
clientContext.ExecuteQuery();
}
3.Graph
Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.