2

Is there a Sharepoint API to get entire content of files/folders for a particular folder at once ? I don't want to use GetFolderByServerRelativePath recursively.

1 Answers1

0

If don't want to use GetFolderByServerRelativePath, you can use CSOM with CAML Query like this:

            string password = "pwd";
            string account = "user@Tenant.onmicrosoft.com";
            var secret = new SecureString();
            foreach (char c in password)
            {
                secret.AppendChar(c);
            }
            using (ClientContext ctx = new ClientContext("https://Tenant.sharepoint.com/sites/sitename"))
            {
                ctx.Credentials = new SharePointOnlineCredentials(account,secret);
                Web web = ctx.Web;
                ctx.Load(web);
                ctx.ExecuteQuery();
                List list = web.Lists.GetByTitle("Documents");
                ctx.Load(list);
                ctx.ExecuteQuery();
                Folder folder = web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + "/shared%20documents/");
                ctx.Load(folder);
                ctx.ExecuteQuery();

                CamlQuery camlQuery = new CamlQuery();
                camlQuery.ViewXml = @"<View Scope='RecursiveAll'>
                                     <Query>
                                     </Query>
                                 </View>";
                camlQuery.FolderServerRelativeUrl = folder.ServerRelativeUrl;
                ListItemCollection listItems = list.GetItems(camlQuery);
                ctx.Load(listItems);
                ctx.ExecuteQuery();
                foreach (var item in listItems)
                {
                    Console.WriteLine(item.FieldValues["FileRef"].ToString());
                }
            }

will return all folders/subfolders/files together, hopefully, this is what you need.

Jerry
  • 3,480
  • 1
  • 10
  • 12
  • Thanks Jerry. Actually I was looking for an API that will simply return the content either in nested or flat format. I want to avoid recursion if possible. – kartik sharma Dec 16 '20 at 08:27
  • Use CAML Query camlQuery.ViewXml = @" "; I willl update answer, please check, this won't need to loop subfolders recursively – Jerry Dec 16 '20 at 09:01
  • @kartiksharma, have you tried to use CAML Query as above ? Did it work for you ? – Jerry Dec 17 '20 at 08:54