Retrieving SPO ChildItems with multiple depths using CSOM API

Lakshman Bana 61 Reputation points
2021-04-14T13:50:44.797+00:00

Hello,
Any documentation from SPO to fetch child items with files, and list containing list items etc. I saw bits and pieces on CAML but not sure how to construct a query with multiple levels deep and what attributes to use to fetch the items from SPO.

        ClientContext ctx = .... // using Pnp.Framework create an object
        :
        List itmc = ctx.Web.Lists.GetByTitle("ProjectList");
        ctx.Load(itmc);
        ctx.ExecuteQuery();

        for(int i = 1; i < itmc.ItemCount; i++)
        {
            ListItem ic = itmc.GetItemById(i);
            ctx.Load(ic);
            ctx.ExecuteQuery();
            Console.WriteLine("{0}, {1}", ic["Title"], ic["ItemChildCount"]);
        }

I get 7 in item count but ExecuteQuery fails for 5 & 6 with "Item does not exist. It may have been deleted by another user."
Why does it return count 7 if 5 & 6 were deleted?

The remaining 5 is get title and count for the ID 1 and the other four IDs prints title as "" and count as 0.

thanks in advance
lb

Microsoft 365 and Office | SharePoint | For business | Windows
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

Answer accepted by question author
  1. MichaelHan-MSFT 18,136 Reputation points
    2021-04-15T07:43:23.07+00:00

    Hi @Lakshman Bana ,

    You get 7 in item count, it is just the number of list items. However, it doesn't mean the item id is 1,2,3,4,5,6,7. The item id could be 1,2,3,4,7,8,9 or else.

    You should use the below instead:

            ListItemCollection listItems= list.GetItems(CamlQuery.CreateAllItemsQuery());  
    
            ctx.Load(listItems);  
            ctx.ExecuteQuery();  
    
            foreach(ListItem ic in listItems)  
            {  
                Console.WriteLine("{0}, {1}", ic["Title"], ic["ItemChildCount"]);  
            }  
    

    If an Answer is helpful, please click "Accept Answer" and upvote it.
    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful

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.