Last active
September 6, 2024 22:01
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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