Skip to content

Instantly share code, notes, and snippets.

@bassemfg
Last active October 5, 2018 04:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bassemfg/9443322f17f754354b1d47c469916118 to your computer and use it in GitHub Desktop.
Save bassemfg/9443322f17f754354b1d47c469916118 to your computer and use it in GitHub Desktop.
Get all files in all folders and sub folders of a SharePoint online document library recursively using CSOM and CAML
private List<ListItem> GetAllItems( ClientContext Context, List list, List<ListItem> ListItems)
{
camlQuery.ViewXml =
@"< View Scope = 'RecursiveAll'>
< Query >
<Where>
</Where>
<OrderBy>
<FieldRef Name='ID' />
</OrderBy>
</ Query >
</ View >";
ListItemCollection AllItems = list.GetItems(camlQuery);
Context.Load(AllItems);
Context.ExecuteQuery();
foreach (ListItem item in AllItems)
{
if (item.FileSystemObjectType == FileSystemObjectType.File)
{
if(!ListItems.Contains(item))
ListItems.Add(item);
}
if (item.FileSystemObjectType == FileSystemObjectType.Folder)
{
camlQuery.FolderServerRelativeUrl = item.FieldValues["FileRef"].ToString();
GetAllItems(Context, list, ListItems);
}
}
return ListItems;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment