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

Diana 1 Reputation point
2024-04-09T04:32:46.14+00:00

Hi,

I want to download a .CSV file, whose name is known to me, from a sharepoint site.

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.sharepointabc.company.com/sites/Department/XX/Group Dev Library/APP-DD Daily 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.sharepointabc.company.com/sites/Department/XX"))

        {

            context.Credentials = new NetworkCredential("mywindows_username", "windows_password", "domain");

            context.Load(context.Web);

            context.ExecuteQuery();

            List list = context.Web.Lists.GetByTitle("Group Dev Library");

            context.Load(list.RootFolder, f => f.ServerRelativeUrl);

            context.ExecuteQuery();

            string folderUrl = list.RootFolder.ServerRelativeUrl + "/APP%2dDD%20Daily%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,706 questions
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 32,081 Reputation points Microsoft Vendor
    2024-04-09T08:45:30.24+00:00

    Hi @Diana,

    The folderurl should be like /sites/sitename/shared documents/foldername. You could check if your url is correct.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.