4

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

            }
Zakos
  • 1,492
  • 2
  • 22
  • 41

3 Answers3

12

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;
            }
        }
Zakos
  • 1,492
  • 2
  • 22
  • 41
3

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>";
1

Thank you for this great answer. Just to let you know that the folderServerRelativeUrl variable should be from the root Url (except the http://server). I did have a list named "Vendors" inside documentcenter. That is, siteUrl is http://server/documentcenter and the list name is Vendors. I had to pass "/documentcenter/Vendors/..." as my folderServerRelativeUrl. But that is OK. I am still grateful to you.

Stack Overflow folks, it is so sad that I could not add a comment underneath his answer. I had to submit this comment as an answer. Help me if this could be done in a different way. I do not want to edit his answer.

~Sharmin

user007
  • 1,504
  • 2
  • 18
  • 51
  • Please read the faq - it explains when you can comment. Hint: you are only missing a single rep – kleopatra Jan 29 '13 at 17:29
  • @kleopatra, Thanks for the hint. But I still believe that a common user who is a new user to stackoverflow should not wait to get some reputation to enter comments. Another user who is not aware of this will come and look at my answer where as the real answer is down below. – user007 Jan 30 '13 at 17:39
  • @SharminJose; it is a UI issue. The UI is there to interface with the user; if there is a problem, the UI should say so. For new users, the "add comment" button should be visible and display a message about insufficient reputation once clicked. Asking every user to read and fully understand the FAQ is valid, but there are simple ways to guide the user there, rather than having a ten thousand angry StackExchange users bark "RTF" all the time. @kleopatra, I am not talking about you; you were quite graceful. – Naikrovek Feb 28 '13 at 13:50
  • @Naikrovek, I was just saying that entering comments should be allowed to any one. To me it looks odd when anyone can enter an answer but only reputation allows a user to enter comments. I just did not want to edit the answer of the other user, but definitely wanted to add the above message. Again, it's just my desire to have that functionality and I am not saying I perfectly conveyed it. By the way what is "RTF"? – user007 Feb 28 '13 at 16:09