Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SharePoint - Client Object Model - Get Sub Folder Items

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?!

            }
like image 774
Zakos Avatar asked Dec 04 '22 18:12

Zakos


2 Answers

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;
            }
        }
like image 54
Zakos Avatar answered Mar 11 '23 03:03

Zakos


Another way to get all items of list folders:

camlQuery.ViewXml = @"<View Scope='RecursiveAll'>
                         <Query>
                            <Where>
                                <Eq>
                                   <FieldRef Name='FSObjType' />
                                   <Value Type='int'>0</Value>
                                 </Eq>
                             </Where>
                          </Query>
                       </View>";
like image 25
Hazem Abdelwahab Avatar answered Mar 11 '23 03:03

Hazem Abdelwahab