Code to download a .CSV document from a sharepoint site keeps throwing exception

Diana 1 Reputation point
2024-04-09T04:30:54.9533333+00:00

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

SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,707 questions
{count} votes