SharePoint - Client Object Model - Get Sub Folder Items

Zakos picture Zakos · Feb 19, 2012 · Viewed 20.9k times · Source

each 'listItem' contains items , how to get them? I'm trying for a while but only fail ,

help , thanks

the camlQuery is from some tests and examples from the web , didn't help ( had many changes)

            ClientContext clientContext =  new ClientContext("http://xxx.xxx.com");
            List list = clientContext.Web.Lists.GetById(new Guid("{F91A0F26-2826-4B3B-AF30-ED7DE4494C7B}"));
            clientContext.Load(list);

            clientContext.ExecuteQuery();
            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml = @"<queryOptions><QueryOptions><ViewAttributes Scope='RecursiveAll'/><Folder></Folder></QueryOptions></queryOptions>";
            ListItemCollection listItems = list.GetItems(camlQuery);
            clientContext.Load(listItems);
            clientContext.ExecuteQuery();

            foreach (ListItem listItem in listItems)
            {
              each lisItem has children/items , how to get them?!

            }

Answer

Zakos picture Zakos · Feb 20, 2012

I have found the answer , thanks for the helpers... :) Items is my object I created. to get "folderServerRelativeUrl" value, you can get it from (string)listItem ["FileRef"] when you go over the folders from above foreach

  public Items GetFolderItems(string folderServerRelativeUrl, List list, ClientContext clientContext)
        {
            try
            {
                var result = new Items();  <-- my class
                var query = new CamlQuery();

                query.FolderServerRelativeUrl = folderServerRelativeUrl;

                query.ViewXml = "<View Scope=\"RecursiveAll\"> " +
                    "<Query>" +
                    "<Where>" +
                                "<Eq>" +
                                    "<FieldRef Name=\"FileDirRef\" />" +
                                    "<Value Type=\"Text\">" + folderServerRelativeUrl + "</Value>" +
                                 "</Eq>" +
                    "</Where>" +
                    "</Query>" +
                    "</View>";

                var folderItems = list.GetItems(query);
                clientContext.Load(folderItems);
                clientContext.ExecuteQuery();

                foreach (ListItem item in folderItems)
                {
                    // item[ "..." ];
                }

                return result;
            }
            catch (Exception)
            {
                return null;
            }
        }