title: Why is SPContext.Current.Listitem Null? *Edited for clarity*
body: <p>This is the code that's in a custom webpart <strong>intended to be added to a document set</strong> </p>
  
  <p>I can't test it against a doc set because I can only get "SP foundation" loaded on my dev laptop, and so Doc Sets are not available.   Instead, I have created a document library, added a folder, and put 2 docs in the folder. I'm trying to execute this from within the folder against the docs therein.</p>
  
  <p>I added the button that fires this code to the library page.
  Here is the code that fires when the button is pressed:    </p>
  
  <pre><code>   void btnSubmit_Click(object sender, EventArgs e)
      {
  **SPFolder folder = SPContext.Current.ListItem.Folder; **
  char[] splitter = { '/' }; 
  string[] folderName = folder.Name.Split(splitter); 
  string filePrefix = @"Stock Analysis Demo/" + folderName[0] + "/" + folderName[0]; 
  
  SPFile template = folder.Files[filePrefix + " - Template.docx"]; 
  SPFile file; 
  byte[] byteArray = template.OpenBinary(); 
    using (MemoryStream mem = new MemoryStream()) 
  { 
    mem.Write(byteArray, 0, (int)byteArray.Length); 
  
    using (WordprocessingDocument myDoc = WordprocessingDocument.Open(mem, true)) 
    { 
       MainDocumentPart mainPart = myDoc.MainDocumentPart; 
  
       foreach (Word.SdtElement sdt in mainPart.Document .Descendants&lt;Word.SdtElement&gt;    ().ToList()) 
       { 
          Word.SdtAlias alias = sdt.Descendants&lt;Word.SdtAlias&gt;().FirstOrDefault(); 
          if (alias != null) 
          { 
             string sdtTitle = alias.Val.Value; 
  
              if (sdtTitle == "Word:Document") 
             { 
                file = folder.Files[filePrefix + " - " + sdt.InnerText + ".docx"]; 
                AddAltChunk(mainPart, sdt, file); 
  </code></pre>
  
  <p>I get an unhandledexception on SPContext.Current.ListItem is null.  <strong>First line at the double-asterisk</strong></p>
  
  <p>I have debugged and can get some of the SPContext methods to render info, its just that no "Listitem" is current.</p>
  
  <p>I suspect, account a comment, that selecting a "document set" <strong>makes the document set the current item</strong>, and if so, the absence of a selected doc set prior to calling this code is why the current item is null.</p>
  
  <p>...and furthermore, folders do not share the same properties as doc sets, so merely "opening" a folder does not make for a "current item"</p>
  
  <p>Is this correct?    </p>
  
  <p>So how the heck do I test my custom webpart code against a doc set if I can't connect remotely to the actual site, and I can't get doc sets on my dev lap top?</p>
  
  <p>OR........</p>
  
  <p>Selecting a Doc Set does not set a current item.   So what would be a better way to loop thru the documents within this folder?</p>