Share via


CAML query to get all list items including inside folders from SharePoint list without displaying folder

Question

Thursday, August 1, 2019 10:52 AM

Hi All,

Need help on CAML query to get all list items including items inside folders.

For ex:

SharePoint List

Folder(2018)  contains sub folders Jan, Feb to Dec each sub-folder has items

item 1 - 

item 2 - 

Get items Item 1, Item 2 and all items from sub folders.

Please suggest CAML query.

Thanks

All replies (7)

Thursday, August 1, 2019 11:16 AM

Hi Sri2108,

you could use the PowerShell CAML Query to get all items from a List.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Set config variables
$WebURL="http://intranet.crescent.com/"
$ListName ="Projects"
 
#Get Web and List Objects
$Web = Get-SPWeb $WebURL
$List = $Web.Lists[$ListName]
 
#Define the CAML Query
$Query = New-Object Microsoft.SharePoint.SPQuery
$Query.Query = "@
<Where>
    <Eq>
        <FieldRef Name='Status' />
        <Value Type='Choice'>Active</Value>
    </Eq>
</Where>"
 
#Get List Items matching the query
$ListItems = $List.GetItems($Query)
 
Write-host "Total Number of Items: "$ListItems.count
 
#Loop through Each Item
ForEach($Item in $ListItems)
{
    #Do something
    Write-host $Item["Title"]
}

You can also try with CAML Viewer Tool which is freely available at Codeplex for download. 

Here is the link: http://spcamlviewer.codeplex.com/. It readily gives you CAML query for ur requirement.

Thanks & Regards,

sharath aluri


Thursday, August 1, 2019 3:00 PM

Hi Sri,

Please find the below console application to get the data from the document library recursively:

using Microsoft.SharePoint.Client;
   
  
 namespace RetrieveFiles
 {
     class Program
     {
         static void Main(string[] args)
         {
             GetFiles();
         }
  
         public static void GetFiles()
         {
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
  
             string sourceSiteUrl = "<< Your Site Name>>";
  
             string userName = "<<User Name>>";
             string password = "*********";
  
             var clientContext = authMgr.GetSharePointOnlineAuthenticatedContextTenant(sourceSiteUrl, userName, password);
  
             Web web = clientContext.Web;
             clientContext.Load(web);
             clientContext.Load(web.Lists);
             clientContext.Load(web, wb => wb.ServerRelativeUrl);
             clientContext.ExecuteQuery();
  
             List list = web.Lists.GetByTitle("MyRecordLibrary");
             clientContext.Load(list);
             clientContext.ExecuteQuery();
  
             Folder folder = web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + "/MyRecordLibrary/Folder1/");
             clientContext.Load(folder);
             clientContext.ExecuteQuery();
  
             CamlQuery camlQuery = new CamlQuery();
             camlQuery.ViewXml = @"<View Scope='Recursive'>
                                     <Query>
                                     </Query>
                                 </View>";
             camlQuery.FolderServerRelativeUrl = folder.ServerRelativeUrl;
             ListItemCollection listItems = list.GetItems(camlQuery);
             clientContext.Load(listItems);
             clientContext.ExecuteQuery();
  
         }
  
     }
 }

As I am using in the above code block we should say "Recursive in CAML query"

camlQuery.ViewXml = @"<View Scope='RecursiveAll'>
                                     <Query>
                                     </Query>
                                 </View>";

This code will get the files to nth degree without any issues with the depth of the folder structure.

Regards,

Krishna


Thursday, August 1, 2019 3:02 PM

Also you can use as below in case you are using C# code:

camlQuery.ViewXml = "<View Scope=\"RecursiveAll\"> " +
                    "<Query>" +
                    "<Where>" +
                                << Your conditions >>
                    "</Where>" +
                    "</Query>" +
                    "</View>";

Hope this helps.

Regards,

Krishna


Friday, August 2, 2019 2:39 AM

Hi Sri2108 ,

Has the problem been solved?

If you think the replies are helpful to you, please remember to mark them as answers. It will help others who meet the similar question in this forum.

Thank you for your understanding.

Best regards

Itch Sun

Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact [email protected].

SharePoint Server 2019 has been released, you can click here to download it.
Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.


Friday, August 9, 2019 9:11 AM

Hi  ,

If you think the replies are helpful to you, please remember to mark them as answers. It will help others who meet the similar question in this forum.

Thank you for your understanding.

Best regards

Itch Sun

Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact [email protected].

SharePoint Server 2019 has been released, you can click

here to download it.
Click

here to learn new features. Visit the dedicated

forum to share, explore and talk to experts about SharePoint Server 2019.


Tuesday, August 13, 2019 9:13 AM

Hi

Looking forward to hearing from you

Best regards

Itch Sun

Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact [email protected].

SharePoint Server 2019 has been released, you can click

here to download it.
Click

here to learn new features. Visit the dedicated

forum to share, explore and talk to experts about SharePoint Server 2019.


Monday, September 2, 2019 9:42 AM

Hi ,

If you think the replies are helpful to you, please remember to mark them as answers. It will help others who meet the similar question in this forum.

Thank you for your understanding.

Best regards

Itch Sun

Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact [email protected].

SharePoint Server 2019 has been released, you can click

here to download it.
Click

here to learn new features. Visit the dedicated

forum to share, explore and talk to experts about SharePoint Server 2019.