Files or folders not visible in the SharePoint UI


Here's how it goes:

Certain files or folders are not visible in the SharePoint UI ("Normal View"), but are still perfectly accessible from the "Explorer View". I managed to find out the fact that if you try to get any SPListItem information from them programatically, you'll notice that this information is NULL. There's also NO setter defined for say the SPFile class:

Microsoft.SharePoint.SPFile.Item : SPListItem { get; }

The following code could be a good starting point on identifying what files and/or folders are involved:

using System; 
using System.IO; 
using Microsoft.SharePoint; 

namespace SearchForMissingFiles { 
  class Program { 
  
    static void ProcessFolder(SPFolder currentFolder, bool doRepair) { 
      try {
	    Console.WriteLine(" Entering folder '" + currentFolder.Url + "'"); 
		for (int i = 0; i < currentFolder.Files.Count; i++) { 
		  SPFile aFile = currentFolder.Files[i]; 
		  if (aFile.Item == null) { 
		    Console.WriteLine(" !Found file '" + aFile.Name + "' with no SPItem information!"); 
			if (doRepair) { 
			  // attempt repair sequence
  		    } } }
		for (int i = 0; i < currentFolder.SubFolders.Count; i++) { 
		  SPFolder subFolder = currentFolder.SubFolders[i]; 
		  if (subFolder.Name != "Forms") { 
		    bool repairedSubfolder = false; 
			if (subFolder.Item == null) { 
			  Console.WriteLine(" !Found folder '" + subFolder.Name + "' with no SPItem information!"); 
			  if (doRepair) { 
			    // attempt repair sequence
				repairedSubfolder = true; 
			  } } 
			if (!repairedSubfolder) 
			  ProcessFolder(subFolder, doRepair);
			} } }
	  catch (Exception ex) {
	    Console.WriteLine(" !" + ex.GetType() + ": " + ex.Message + "!");
	  } }
	  
	static void Main(string[] args) { 
	  if ((args.Length == 1) || ((args.Length == 2) && (args[1] == "/repair"))) {
  	    using (SPSite mySite = new SPSite(args[0]))
		using (SPWeb myWeb = mySite.OpenWeb())
		  foreach (SPList aList in myWeb.Lists)
		    if (aList.BaseTemplate == SPListTemplateType.DocumentLibrary) {
  			  Console.WriteLine("Processing document library '" + aList.Title + "'");
			  ProcessFolder(myWeb.GetFolder(aList.Title), (args.Length == 2));
			} }
	  else {
	    Console.WriteLine("Invalid parameter.");
		Console.WriteLine("Usage: SearchForMissingFiles.exe [/repair]");
	  } } } }

Of course, a full back-up of your environment would be strongly recommended before running the code.


Skip to main content