[NullReferenceException: Object reference not set to an instance of an object.]
EPiServer.Web.Hosting.Versioning.VersioningFileSummary..ctor(FileItem file, FileOperations fileOp)
This might happen if the file on disk is removed but the reference to it is still in the EPiServer database. It can also happen if the versioning data has become corrupt for some reason.
I solved it by creating the following simple page/tool:
ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FixFiles.aspx.cs" Inherits="QD.Web.FixFiles" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" OnClick="Button1_Click" runat="server" Text="Deleting files where source don't exist" />
<asp:Label ID="StartIndexOnItemLit" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
Code behind
using System;
using System.Text;
using System.Web.Hosting;
using System.Web.UI;
using EPiServer.Web.Hosting;
namespace QD.Web
{
public partial class FixFiles : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
StartIndexOnItemLit.Text = "";
var builder = new StringBuilder();
VirtualPathHandler instance = VirtualPathHandler.Instance;
foreach (VirtualPathProvider a in VirtualPathHandler.Instance.VirtualPathProviders.Keys)
{
if (a is VirtualPathUnifiedProvider)
{
VirtualDirectory dir = instance.GetDirectory((a as VirtualPathUnifiedProvider).VirtualPathRoot, true);
foreach (object element in dir.Children)
{
CheckItem(builder, element);
}
}
}
StartIndexOnItemLit.Text = "<ul>" + builder + "</ul>";
}
private void CheckItem(StringBuilder builder, object element)
{
if (element is UnifiedDirectory)
{
CheckDir((element as UnifiedDirectory), builder);
}
if (element is UnifiedFile)
{
CheckFil((element as UnifiedFile), builder);
}
}
private void CheckDir(UnifiedDirectory dir, StringBuilder builder)
{
try
{
builder.AppendLine("<ul>");
foreach (object a in dir.Children)
{
CheckItem(builder, a);
}
builder.AppendLine("</ul>");
}
catch (Exception error)
{
builder.AppendLine(dir.Name + " " + dir.VirtualPath + " " + error.Message + " " + error.StackTrace);
}
}
private void CheckFil(UnifiedFile fil, StringBuilder builder)
{
try
{
IUnifiedSummary a = fil.Summary;
}
catch (Exception error)
{
builder.AppendLine("<li>deleting file since source don't exists " + fil.VirtualPath + "</li>");
fil.Delete();
}
}
}
}
No comments:
Post a Comment