EPiWiki.se  - EPiServer notes shared with others
 

NullReferenceException at VersioningFileSummary--ctor

[Edit]
Sometimes is File items created without default or checked out version (probably a concurrency problem with the version file system).
The file manager can’t be open and only reports:

...
System.NullReferenceException: Object reference not set to an instance of an object.
  at EPiServer.Web.Hosting.Versioning.VersioningFileSummary..ctor(FileItem file, FileOperations fileOp)
  at EPiServer.Web.Hosting.Versioning.VersioningFileSystem.GetFileSummary(IFileHandler file)
...

Sample code to detect this error.


To fix this error out comment the lines:

//file.DefaultRevision = lastRevision;
//session.Save(file);


<%@ Page Language="C#" AutoEventWireup="true" %>

<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="EPiServer.Web.Hosting" %>
<%@ Import Namespace="EPiServer.BaseLibrary" %>
<%@ Import Namespace="EPiServer.Web.Hosting.Versioning.Store" %>
<%@ Import Namespace="System.Web.Hosting" %>
<html>


<script runat="server">

    private void ListDir(string fromVirtualPath, ISession session)
  {
      fromVirtualPath = VirtualPathUtility.AppendTrailingSlash(fromVirtualPath);
  
      List<string> children = new List<string>();
      string objectStorePath =
        "/filesystem" + fromVirtualPath.Replace("~", "");
      if (objectStorePath.EndsWith("/"))
      {
          objectStorePath = objectStorePath.Substring(
            0,
            objectStorePath.Length - 1);
      }
      
      IItem item = session.LoadPath(objectStorePath);
      if (item == null)
      {
        throw new Exception("Can't find object store path " + objectStorePath);
      }
      
      foreach(IItem i in session.RelatedItemsFrom(item.Id))
      {
string virtualPathName = "";
          if (i is FileItem)
          {
              try
              {
                  UnifiedFile f = VirtualPathHandler.Instance.GetFile(
                    fromVirtualPath + i.Name, true) as UnifiedFile;

virtualPathName = fromVirtualPath + i.Name;
                  Response.Flush();
              }
              catch (Exception ex)
              {
                  Response.Write("Can't create UFS object for file "
                    + fromVirtualPath + i.Name + " because, " + ex + "<br/>");
              }

              FileRevision lastRevision = null;
              foreach (FileRevision r in session.RelatedItemsFrom(i.Id))
              {
                  if (r is FileRevision)
                  {
                    if (lastRevision == null || lastRevision.Created < r.Created)
                    {
                        lastRevision = r;
                    }
                  }
               }
              
              FileItem file = (FileItem)i;
               if (file.DefaultRevision \=\= null && file.CheckedoutRevision \=\= null)
               {
                   if (lastRevision != null)
                   {
  Response.Write(virtualPathName + "<br/>");
                       Response.Write("-Detecting non default revision setting it to the last find revision: " + lastRevision.Name + "<br/>");
                     //file.DefaultRevision = lastRevision;
                     //session.Save(file);
                   }
                   else
                   {
                       Response.Write("ERROR: Can't find any revision to use as default revision<br/>");
                   }
               }
          }
          else if (i is DirectoryItem)
          {
              ListDir(fromVirtualPath + i.Name, session);
          }
      }

      Response.Flush();
  }

    protected void Execute(string path)
    {
        ISession session = EPiServer.BaseLibrary.Context.Repository.CreateSession();
        ListDir(path, session);
        session.Close();
    }
</script>

<body>
    <form id="form1" runat="server">
    <div>
        <%
            Execute("~/Global/");
        %>
    </div>
    </form>
</body>
</html>


Version author:
Toni

EPiServer version

All