0

This question already has an answer here:

My scenario: I need to update 2 columns of items in a certain subfolder.But here the subfolder can contain subfolder and so on which i need to update too.I am using csom to try to accomplish that.I cant figure out how to recursively get all the items and once i get those item how to differentiate between files and folders

I can't get the item name to pass a relative url in the recursive function.

        private static  void GetFolderItems(string folderServerRelativeUrl, List list, ClientContext clientContext)
    {
        try
        {

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

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


            foreach (ListItem item in folderItems)
            {
                if (item.FileSystemObjectType == FileSystemObjectType.File)
                {
                    item["Title"] = "Test";
                    item.Update();
                    list.Update();
                    clientContext.ExecuteQuery();

                }
                else if (item.FileSystemObjectType == FileSystemObjectType.Folder)
                {
                    // item is a folder
                    String SubFolderUrl = folderServerRelativeUrl + "/" + item["Title"]; 

                    GetFolderItems(SubFolderUrl, list,clientContext);

                }
            }


        }
        catch (Exception)
        {
            Console.WriteLine("error occured");
        }
    }

marked as duplicate by wjervis, Gautam Sheth, Gaurravs, Dikesh Gandhi, Nadeem Yousuf Mar 14 '18 at 13:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

Where do you have the problem? Once you have the list instance, call the context manager (ClientContext class) in order to load the properties you need. Then, just iterate over all folders to update them and all the items inside.

// Context.Current is the proper ClientContext instance
Context.Current.Load(listInstance.RootFolder);
Context.Current.Load(listInstance.RootFolder.Folders);

// This sentence returns the folder searching by its name
var folder = listInstance.RootFolder.Folders.FirstOrDefault(f => f.Name == title);

EDIT: Retrieving list items

Here you have a similar question, Get Items Under Folder CAML, I think this will help you a lot.

Besides, here you have the code I used in order to retrieve all items from a list. Maybe, changing this method and adding the proper CAML sentences you will be able to retrieve items from a folder.

public IEnumerable<ListItem> GetItems(string title, params string[] fields)
{
    var list = Get(title);
    var query = CamlQuery.CreateAllItemsQuery(10000, fields);
    var items = list.GetItems(query);

    Context.Current.Load(list);
    Context.Current.Load(items);

    Context.Current.ExecuteQuery();

    foreach(ListItem item in items)
    {
        yield return item;
    }
}
  • Using the first 2 line i got an idea how to iterate over the folders,now i want to know how to get the items of that folder and update it? – Keshav Taurah Mar 14 '18 at 9:11
  • Edit my answer adding a link to a similar question you asked and some code I used to retrieve all items from a list. – jaloplo Mar 14 '18 at 9:26
  • how do i differentiate between files and folders then? – Keshav Taurah Mar 14 '18 at 11:14
  • I think that changing the CAML query from my code and pasting the one that appears as the answer in the link provided will be enough. You will iterate through the folders and query for the items in there. – jaloplo Mar 14 '18 at 11:26
  • i got all the folders and files in the current directory but not files inside those folders – Keshav Taurah Mar 14 '18 at 11:29
1

You can get the subfolders using RecursiveAll:

    List DocumentsList = clientContext.Web.Lists.GetByTitle(list);
    CamlQuery camlQuery = new CamlQuery();
    camlQuery = new CamlQuery();
    camlQuery.ViewXml = "<View Scope=\"RecursiveAll\"> " +
                "<Query>" +
                "<Where>" +
                            "<Eq>" +
                                "<FieldRef Name=\"FileDirRef\" />" +
                                "<Value Type=\"Text\">/sites/test/list/folder1</Value>" +
                             "</Eq>" +
                "</Where>" +
                "</Query>" +
                "</View>";
    ListItemCollection listItems = DocumentsList.GetItems(camlQuery);
    clientContext.Load(listItems);
    clientContext.ExecuteQuery();
    foreach (ListItem item in listItems)
    {
       console.writeline(item["Title"]); 
    }
  • do i get the subfolders items using this query? – Keshav Taurah Mar 14 '18 at 9:18
  • Yes. you can get all files and folders using RecursiveAll – Deepmala Mar 14 '18 at 9:22
  • how do i get each item Title? – Keshav Taurah Mar 14 '18 at 11:11
  • Updated the previous response – Deepmala Mar 15 '18 at 4:05
  • when i use the code,it doesnt return me the name of the folders or files.I just get the name of only one powerpoint thats it. – Keshav Taurah Mar 15 '18 at 4:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.