string sourcePath = "C:\\Xie\\Test\\"; string siteUrl = "https://xxx.sharepoint.com/sites/test"; var authManager = new OfficeDevPnP.Core.AuthenticationManager(); // This method calls a pop up window with the login page and it also prompts // for the multi factor authentication code. ClientContext ctx = authManager.GetWebLoginClientContext(siteUrl); // The obtained ClientContext object can be used to connect to the SharePoint site. Web web = ctx.Web; ctx.Load(web, w => w.Title); ctx.ExecuteQuery(); Console.WriteLine("You have connected to {0} site, with Multi Factor Authentication enabled!!", web.Title); List targetList = ctx.Web.Lists.GetByTitle("TestFolder"); FolderCollection oFolderCollection = targetList.RootFolder.Folders; ctx.Load(oFolderCollection); ctx.ExecuteQuery(); foreach (Folder oFolder in oFolderCollection) { Console.WriteLine("Folder Name: " + oFolder.Name + " Folder URL: " + oFolder.ServerRelativeUrl); Folder folder = web.GetFolderByServerRelativeUrl(oFolder.ServerRelativeUrl); ctx.Load(folder); DirectoryInfo directory = new DirectoryInfo(sourcePath); FileInfo[] files = directory.GetFiles("*.*"); foreach (FileInfo file in files) { var filePath = sourcePath + file.Name; FileCreationInformation newFile = new FileCreationInformation(); newFile.Content = System.IO.File.ReadAllBytes(sourcePath + file.Name); newFile.Url = file.Name; newFile.Overwrite = true; Microsoft.SharePoint.Client.File newspFile = folder.Files.Add(newFile); } ctx.ExecuteQuery(); } Console.ReadLine();