title: programmatically get children of folder sharepoint
body: <p>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.</p>
  
  <p>My document library structure is </p>
  
  <ul>
  <li>Document Library </li>
  <li>Document 1</li>
  <li>Document 2</li>
  <li>Document 3
  <ul>
  <li>Folder 1
  <ul>
  <li>Document 4 </li>
  </ul></li>
  <li>Folder 2
  <ul>
  <li>Document 5</li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>I need to be able to get the child documents from within the folders and not just the documents within the document library.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>namespace SharePointMVC.Controllers
  {
      [HandleError]
      public class HomeController : Controller
      {
          public ActionResult Index()
          {
              DefaultModel model = new DefaultModel();
              List&lt;DocumentModel&gt; documents = new List&lt;DocumentModel&gt;();
              List&lt;FolderModel&gt; folders = new List&lt;FolderModel&gt;();
              List&lt;object&gt; itemModels = new List&lt;object&gt;();
  
              using (ClientContext context = new ClientContext("MYSPSITE"))
  
              {
                  List list = context.Web.Lists.GetByTitle("MYDOCUMENTLIBRARY");
                  context.Load(list);
                  CamlQuery query = new CamlQuery();
                  query.ViewXml = "&lt;View/&gt;";
                  ListItemCollection listitems = list.GetItems(query);
  
                  context.Load(list);
                  context.Load(listitems);
                  context.ExecuteQuery();
  
                  foreach (ListItem listItem in listitems)
                  {
                      IEnumerable&lt;object&gt; items = ProcessListItems(listItem);
                      itemModels.AddRange(items);
                  }
  
                  model.Documents = documents;
              }
  
              return View(model);
          }
  
          public IEnumerable&lt;object&gt; ProcessListItems(ListItem listItem)
          {
              List&lt;object&gt; items = new List&lt;object&gt;();
              if (listItem.FileSystemObjectType == FileSystemObjectType.Folder)
              {
                  FolderModel FolderModel = new FolderModel();
                  foreach (ListItem childListItem in listItem.childItems)
                  {
                      IEnumerable&lt;object&gt; childItems = ProcessListItems(childListItem);
                  }
                  items.Add(FolderModel);
              }
              else
              {
                  DocumentModel documentModel = new DocumentModel();
                  items.Add(documentModel);
              }
          }
  
          public ActionResult About()
          {
              return View();
          }
      }
  }
  </code></pre>
  
  <p>Any help will be greatly appreciated!</p>