0
votes

How can i access the sharepoint library and get files list? I want to download and upload files to the sharepoint using C#. My sharepoint login asks for OTP whenever i log in

1

1 Answers

0
votes

Have you set credential for your request?

Demo to upload.

using (ClientContext context = new ClientContext("https://xxx.sharepoint.com/sites/site"))
            {
                string s = "password";
                SecureString passWord = new SecureString();
                foreach (var c in s)
                    passWord.AppendChar(c);
                context.Credentials = new SharePointOnlineCredentials("[email protected]", passWord);

                //List docs = context.Web.Lists.GetByTitle("largeLib1");
                Folder folder = context.Web.GetFolderByServerRelativeUrl("/sites/site/largeLib1/set2");
                var filePath = @"C:\Lee\template.xlsx";
                using (FileStream fs = new FileStream(filePath, FileMode.Open))
                {                        
                    FileCreationInformation flciNewFile = new FileCreationInformation();

                    flciNewFile.ContentStream = fs;
                    flciNewFile.Url = System.IO.Path.GetFileName(filePath);
                    flciNewFile.Overwrite = true;                    
                    Microsoft.SharePoint.Client.File uploadFile = folder.Files.Add(flciNewFile);
                    context.Load(uploadFile);
                    context.ExecuteQuery();
                }                                                           
            }

download demo