I am developing a C# console app that needs to copy an xlsx file from a SharePoint doc library to the local disk.
This runs as a scheduled task with specific user credentials defined for the task.
I have tried:
Mapping a network drive and using System.IO.File.Copy. This works as along as the drive stays mapped however the drive mapping keeps failing because SharePoint starts denying access. This is due to the login expiring and SharePoint requiring a login to refresh the credentials. I would prefer this solution if anyone has advice however it looks like this is a known issue for SharePoint online and mapping drives.
Using the WebClient I tried both the DownloadFile and DownloadData methods. Using either method I can successfully contact the SharePoint server and get to the file. It acts like it downloads and returns no errors however it only downloads a portion of the file (around 45KB and the file is ~200KB). I cannot get it to download the entire file.
I have been reading many posts across many forums for the past two days and have seen others with this problem but many of the posts are quite old. I have seen a number of posts that say using DownloadFile should solve the problem. Based on a couple postings I found people do have problems with Office (Word and Excel) files getting corrupted but I am not finding definite solutions. Also, I am new to this so I am assuming there is something I am overlooking. I can't believe it is this hard to copy a single file.
Here is the WebClient code I am using:
string url = ConfigurationManager.AppSettings["SharePointLocation"]; // Create a new WebClient instance. WebClient myClient = new WebClient(); myClient.UseDefaultCredentials = true; myClient.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36"); // Download the Web resource and save it into the current filesystem folder. myClient.DownloadFile(url, ConfigurationManager.AppSettings["MasterScheduleLocalPath"]);
Any pointers would be much appreciated. This has turned what I thought was a quick fix into a big problem.
Thanks,
DMK