How to ignore both folders and their contents using CAML query in Sharepoint 2019?

Puneeth, C 81 Reputation points
2022-04-08T12:16:26.44+00:00

I am using Sharepoint 2019 and I am trying to fetch only items excluding all the folders and all the items inside those folders.
My current code is given below as is:

List tempList = targetWeb.Lists.GetByTitle(listName);
context.Load(tempList);
context.ExecuteQuery();

    FieldCollection fColl = tempList.Fields;
    context.Load(fColl);
    context.ExecuteQuery();

    CamlQuery camlQuery = new CamlQuery();
    string queryText = "<View><Query><OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy></Query><RowLimit Paged=\"TRUE\">2</RowLimit></View>";
    camlQuery.ListItemCollectionPosition = position;
    camlQuery.ViewXml = queryText;
    // Executing the query
    ListItemCollection currentCollection = tempList.GetItems(camlQuery);               
    context.Load(currentCollection);
    context.ExecuteQuery();

But this fetching the folders as well into currentCollection. When I used scope=RecusiveAll. it is fetching the items inside the folders and I do not want that either.


Please let me know how to proceed.
Thanks for the help in advance

Microsoft 365 and Office | SharePoint | Development
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,486 Reputation points Microsoft External Staff
    2022-04-11T06:56:30.8+00:00

    Hi @Puneeth, C ,
    You can try following caml query to get files in library root folder.

      List list = context.Web.Lists.GetByTitle("YOURLISTNAME");  
                        context.Load(list.RootFolder);  
                        context.Load(list.RootFolder.Files);  
                        var items = list.GetItems(  
                            new CamlQuery()  
                            {  
                                ViewXml = @"<View><ViewFields><FieldRef Name='Title' /><FieldRef Name='FileLeafRef' /><FieldRef Name='ID' /><FieldRef Name='ContentType' /></ViewFields><Query>   
                <Where><IsNotNull><FieldRef Name='File_x0020_Type' /></IsNotNull></Where>   
                </Query></View>"  
                            });  
                        context.Load(items, a => a.IncludeWithDefaultProperties(item => item.File, item => item.File.CheckedOutByUser));  
                        context.ExecuteQuery();  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.