title: How to detect when a Sharepoint file is in a subfolder
body: <p>I'm displaying files from a Sharepoint 2010 document library on my web page.  I'm successfully showing a list of the files and their icons.  However, some of the files are in subfolders, and instead of showing the file icon, I'd like to show a folder icon.</p>
  
  <p><strong>How do I detect when a file is in a subfolder?</strong></p>
  
  <p>I could parse each file's ServerRelativeUrl to determine the folder structure.  I'm hoping there is another way.</p>
  
  <hr>
  
  <p>Here is the Sharepoint library:</p>
  
  <p><a href="https://i.stack.imgur.com/J0cvu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/J0cvu.png" alt="library"></a></p>
  
  <hr>
  
  <p>And here is the code which produces the list of files in that library:</p>
  
  <pre><code>using (ClientContext clientContext = new ClientContext(SharepointSite))
      {
          var query = new CamlQuery
          {
              ViewXml = "&lt;View Scope='RecursiveAll'&gt;" +
                          "&lt;Query&gt;" +
                           "&lt;Where&gt;" +
                              "&lt;Eq&gt;" +
                                 "&lt;FieldRef Name='FSObjType' /&gt;" +
                                 "&lt;Value Type='Integer'&gt;0&lt;/Value&gt;" +
                              "&lt;/Eq&gt;" +
                           "&lt;/Where&gt;" +
                          "&lt;/Query&gt;" +
                        "&lt;/View&gt;"
          };
  
          var sourceList = clientContext.Web.Lists.GetByTitle("Test Library");
          var files = sourceList.GetItems(query);
          clientContext.Load(files);
          clientContext.ExecuteQuery();
          foreach (var file in files)
          {
              var id = file.Id;
              var filename = file["FileLeafRef"].ToString();
              var iconName = clientContext.Web.MapToIcon(filename, string.Empty, IconSize.Size16);
              clientContext.ExecuteQuery();
              var imgUrl = "http://sharepointsite/_layouts/images/" + iconName.Value;
              Image iconImage = new Image {ImageUrl = imgUrl};
  
              clientContext.Load(file.ParentList);
              clientContext.ExecuteQuery();
              var listUrl = file.ParentList.DefaultDisplayFormUrl;
              HyperLink docLink = new HyperLink
              {
                  Text = filename,
                  NavigateUrl = listUrl + "?ID=" + id //ToDo:  fix
              };
  
              HtmlTableRow row = new HtmlTableRow();
              HtmlTableCell cell1 = new HtmlTableCell();
              cell1.Controls.Add(iconImage);
              HtmlTableCell cell2 = new HtmlTableCell();
              cell2.Controls.Add(docLink);
              row.Cells.Add(cell1);
              row.Cells.Add(cell2);
              tbFiles.Rows.Add(row);
          }
      }
  </code></pre>
  
  <hr>
  
  <p>which results in:</p>
  
  <p><a href="https://i.stack.imgur.com/zSzHu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zSzHu.png" alt="files"></a></p>
  
  <p>Only the first file in the list is actually in the top-most library.  The rest are in "Test Folder".</p>