title: Get Library and Folder from document Id
body: <h2>Information given</h2>
  
  <p>I am using version 16.0.0.0 of the Microsoft.SharePoint.Client library.
  There are several different document libraries in the SharePoint site. In code, we are given a list of document URLs (containing document GUIDs) and need to determine in which Document Library each is located, including the sub-folder if it is in a folder in the library.</p>
  
  <p>Document Id (GUID) - gathered from document url query parameter "sourcedoc"
    ex: "<a href="http://mycompany.sharepoint.com/spsite/_layouts/15/WopiFrame.aspx?action=default&amp;file=testfile.docx&amp;sourcedoc=" rel="nofollow">http://mycompany.sharepoint.com/spsite/_layouts/15/WopiFrame.aspx?action=default&amp;file=testfile.docx&amp;sourcedoc=</a>{6A290A65-4759-41ED-A13E-3333B45DF133}"</p>
  
  <h2>Information needed</h2>
  
  <ul>
  <li>Document Library Name</li>
  <li>Document Library URL</li>
  <li>Folder Name (if any)</li>
  <li>Folder URL (if any)</li>
  </ul>
  
  <h2>Current Code</h2>
  
  <pre><code>using SP = Microsoft.SharePoint.Client;
  
  public class LibraryAndFolderInfo
  {
      public LibraryAndFolderInfo();
  
      public string FolderName { get; set; }
      public string FolderUrl { get; set; }
      public string LibraryName { get; set; }
      public string LibraryBaseUrl { get; set; }
  }
  
  public class SPDataAccess
  {
      public LibraryAndFolderInfo GetLibraryAndFolderInfo(Guid documentGuid)
      {
          SP.File file = Web.GetFileById(documentGuid);
  
          Context.Load(file);
  
          if (file != null)
          {
              Context.Load(file.ListItemAllFields);
              Context.ExecuteQuery();
              SP.ListItem item = file.ListItemAllFields;
  
              Context.Load(item.ParentList);
              Context.ExecuteQuery();
  
              SP.List list = item.ParentList;
              Context.Load(list);
              Context.ExecuteQuery();
  
              Context.Load(item.Folder);
              Context.ExecuteQuery();
  
              SP.Folder folder = item.Folder;
              Context.Load(folder);
              Context.ExecuteQuery();
  
              LibraryAndFolderInfo lib = new LibraryAndFolderInfo();
              lib.LibraryName = list.Title;
              lib.LibraryBaseUrl = list.DefaultViewUrl;
              lib.FolderName = folder.Name;
              lib.FolderUrl = folder.ServerRelativeUrl;
  
              return lib;
          }
  
          return null;
      }
  
      protected SP.ClientContext Context { get; set; }
  }
  </code></pre>
  
  <p><br/>
  The code currently fails at this line: <code>Context.Load(item.ParentList);</code> with the following error:</p>
  
  <blockquote>
    <p>The property or field 'Title' has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.</p>
    
    <p>at Microsoft.SharePoint.Client.ClientObject.CheckUninitializedProperty(String propName)<br/>
    at Microsoft.SharePoint.Client.List.get_Title()<br/>
    at MyNamespace.SPDataAccess.GetLibraryAndFolderInfo(Guid documentGuid) in c:\path\SPDataAccess.cs:line 27</p>
  </blockquote>
  
  <p>This line seems to be attempting to retrieve the Title of the list as it is being loaded and failing. Is this a correct assumption?</p>
  
  <p>I am not super familiar with how exactly to load properties, so everything after the failing line is my best guess as to how it would work.</p>
  
  <p>What is this code supposed to look like? Has anyone else tried to get this information from a document?</p>