Code to download a .CSV document from a sharepoint site keeps throwing exception
Hi,
I want to download a .CSV file, whose name is known to me, from a sharepoint site. Edge Copilot generated some c# code for doing that. I do not have much knowledge of using sharepoints programmatically.
I have modified the code, till I got no exceptions, till the very the last statement where it keeps throwing the following exception: Microsoft.SharePoint.Client.ServerException: 'File Not Found.'
The Sharepoint URL of CSV file I want to download is:
https://content.sp2019.company.com/sites/Department/AO/Group Ops Library/APP-ME Wip Report/New_Design_Num/Num_Report.csv
Using segments from within the URL, I have written the following code. I am not really sure, if I have done it correct, but that is the only way I could avoid exceptions, till I reached the end line context.ExecuteQuery();
static void Main(string[] args)
{
using (ClientContext context = new ClientContext("https://content.sp2019.company.com/sites/Department/AO"))
{
context.Credentials = new NetworkCredential("mywindows_username", "windows_password", "domain");
context.Load(context.Web);
context.ExecuteQuery();
List list = context.Web.Lists.GetByTitle("Group Ops Library");
context.Load(list.RootFolder, f => f.ServerRelativeUrl);
context.ExecuteQuery();
string folderUrl = list.RootFolder.ServerRelativeUrl + "/APP%2dME%20Wip%20Report/New%5fDesign%5fNum";
FileCollection files = list.RootFolder.Folders.GetByUrl(folderUrl).Files;
context.Load(files);
context.ExecuteQuery();
}
I have confirmed many times, the URL is correct. It leads me to the file I want to download.
Can you please with fix the code ? or tell how to debug it ? Can you point me to some article to read ?
Thank you