Not able to retrieve Sharepoint folders/files list from C# app

Stefano Mora 1 Reputation point
2023-03-13T11:48:17.08+00:00

Hi all,

I need to get the list of files and folder of a SharePoint area but I'm unable to.

Let's say that I'm able to connect with the server but when I request the files I always receive an exception 'Shared documents' are not present on the site:

            Web web = ctx.Web;

            Console.WriteLine(web.Lists.ToString());

            Folder fold = web.GetFolderByServerRelativeUrl(".");
            Console.WriteLine(fold.ItemCount);

            // ----

            string listName = "Shared documents";
            Microsoft.SharePoint.Client.List list = ctx.Web.Lists.GetByTitle(listName);

            FolderCollection folders = list.RootFolder.Folders;
            ctx.Load(folders);
            try
            {
                ctx.ExecuteQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }

            foreach (Folder folder in folders)
            {
                Console.WriteLine(folder.Name);
            }

The site url I'm connected is:

        string siteUrl = "https://mysite.sharepoint.com/sites/sitek";

where sitek is a private group I'm able to connect on browser.

Which listName I should use to get the files?

Thanks!

Microsoft 365 and Office | SharePoint | For business | Windows
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,496 Reputation points Microsoft External Staff
    2023-03-14T02:55:02.0566667+00:00

    Hi @Stefano Mora

    I have tested the code you provide, and it runs successfully.

    User's image

    Could you just run the code without try catch. And check if there is still the error message

                string listName = "Shared documents";
                Microsoft.SharePoint.Client.List list = ctx.Web.Lists.GetByTitle(listName);
    
                FolderCollection folders = list.RootFolder.Folders;
                ctx.Load(folders);
    
                {
                    ctx.ExecuteQuery();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return;
                }
    
                foreach (Folder folder in folders)
                {
                    Console.WriteLine(folder.Name);
                }
    
    

    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.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.