Question

I have been asked to retrieve the contents of a document library and display them on a webpage with links to download using MVC. I can retrieve all the documents from the library with no issue. However when the documents are stored within subfolders in my document library my links only open the folder.

My document library structure is

  • Document Library
  • Document 1
  • Document 2
  • Document 3
    • Folder 1
      • Document 4
    • Folder 2
      • Document 5

I need to be able to get the child documents from within the folders and not just the documents within the document library.

Here is my code:

namespace SharePointMVC.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            DefaultModel model = new DefaultModel();
            List<DocumentModel> documents = new List<DocumentModel>();
            List<FolderModel> folders = new List<FolderModel>();
            List<object> itemModels = new List<object>();

            using (ClientContext context = new ClientContext("MYSPSITE"))

            {
                List list = context.Web.Lists.GetByTitle("MYDOCUMENTLIBRARY");
                context.Load(list);
                CamlQuery query = new CamlQuery();
                query.ViewXml = "<View/>";
                ListItemCollection listitems = list.GetItems(query);

                context.Load(list);
                context.Load(listitems);
                context.ExecuteQuery();

                foreach (ListItem listItem in listitems)
                {
                    IEnumerable<object> items = ProcessListItems(listItem);
                    itemModels.AddRange(items);
                }

                model.Documents = documents;
            }

            return View(model);
        }

        public IEnumerable<object> ProcessListItems(ListItem listItem)
        {
            List<object> items = new List<object>();
            if (listItem.FileSystemObjectType == FileSystemObjectType.Folder)
            {
                FolderModel FolderModel = new FolderModel();
                foreach (ListItem childListItem in listItem.childItems)
                {
                    IEnumerable<object> childItems = ProcessListItems(childListItem);
                }
                items.Add(FolderModel);
            }
            else
            {
                DocumentModel documentModel = new DocumentModel();
                items.Add(documentModel);
            }
        }

        public ActionResult About()
        {
            return View();
        }
    }
}

Any help will be greatly appreciated!

Was it helpful?

Solution

Try changing query.ViewXml = "<View/>"; to query.ViewXml = "<View Scope=\"Recursive\"/>";

This tell SharePoint to return items from all folders. If you want folder information returned as well, change Recursive to RecursiveAll.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top