0

I have the following CAML query where i am trying to get all the files and folders that are directly and indirectly added to the folder named FolderA:-

camlQuery6.ViewXml = "<View Scope=\"RecursiveAll\"><Query><Where><Eq><FieldRef Name=\"FileDirRef\" /><Value Type=\"Text\">" + context.Web.ServerRelativeUrl + "/ArchDocs/FolderA"</Value></Eq></Where></Query></View>";
ListItemCollection collListItem6 = context.Web.GetList(context.Web.ServerRelativeUrl + "/ArchDocs").GetItems(camlQuery6);
context.Load(collListItem6, items => items.Include(
      item => item.Id,
      item=>item["FileDirRef"],
      item => item["Title"],
      item => item["DealStage"]));

the above CAML will only return the main folder under /sites/projects/ArchDocs/FolderA, but will not return any of the sub-folders and files.. so can anyone advice how i need to modify the CAML, or CAML does not support this?

2
  • Are you executing this from a CSOM app?
    – willman
    Feb 27, 2020 at 0:45
  • @willman yes specifically inside a remote event receiver
    – John John
    Feb 27, 2020 at 0:53

1 Answer 1

1

The query is only returning items directly in that folder because your CAML is set to only return items where the FileDirRef (or Folder Path) is equal to /sites/projects/ArchDocs/FolderA, anything that is in a subfolder would have a different FileDirRef, such as /sites/projects/ArchDocs/FolderA/SubFolder1, which would not be equal to the path you specified. What you want to do, is instead of the <Eq> operator element, you want to use <BeginsWith>. Note, you also appear to have syntax error with your string concatenation context.Web.ServerRelativeUrl + "/ArchDocs/FolderA"</Value>

Give this a try:

camlQuery6.ViewXml = "<View Scope=\"RecursiveAll\"><Query><Where><BeginsWith><FieldRef Name=\"FileDirRef\" /><Value Type=\"Text\">" + context.Web.ServerRelativeUrl + "/ArchDocs/FolderA" + "</Value></BeginsWith></Where></Query></View>";
ListItemCollection collListItem6 = context.Web.GetList(context.Web.ServerRelativeUrl + "/ArchDocs").GetItems(camlQuery6);
context.Load(collListItem6, items => items.Include(
      item => item.Id,
      item=>item["FileDirRef"],
      item => item["Title"],
      item => item["DealStage"]));
1
  • ok worked well for me thanks, but all the docs i read used <Eq> instead of <BeginWith>.. thanks
    – John John
    Feb 27, 2020 at 1:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.