Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Anders Hattestad
Aug 8, 2014
  4340
(3 votes)

Error when copy a page structure

If you experience that you get an error when trying to copy or export/import a page tree the reason could be that you are missing some files in the VPP (7.0) or Appdata(7.5).

What you then need is to find what files you are missing. I have made a small utility that loops thru all properties, checks the IReferenceMap, and finds all documents and check if the file actually exists on the disk.

It can be added as a gagdet:
image

An will be shown like:
image

The code is simple and is

    [IFrameComponent(
        Url = "~/Custom/Plugins/CheckFiles.aspx", 
        ReloadOnContextChange = true, 
        PlugInAreas = "/episerver/cms/assets",
        Title = "Check files used", 
        Categories = "cms", MinHeight = 100, MaxHeight = 500)]
    //Update: In EPiServer 7.5, the base class to use is: EPiServer.Shell.WebForms.ContentWebFormsBase
    public partial class CheckFiles : ContentBaseWebForm
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            PageName.InnerHtml = CurrentContent.Name;
            PageName.Attributes["title"] = "" + CurrentContent.ContentLink;
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (UrlRewriteProvider.Module != null)
            {
                UrlRewriteProvider.Module.FURLRewriteResponse = false;
            }
        }

        protected void BtnClicked_Click(object sender, EventArgs e)
        {
            var statusDic = new Dictionary<Guid, GuidStatus>();
            Check(CurrentContent, statusDic, "", DoChildren.Checked, CheckLinkedItems.Checked);// CheckContent(CurrentContent, statusDic, CurrentContent.Name + "[" + CurrentContent.ContentLink + "]", DoChildren.Checked); 
            var txt="";
            var dontExists = statusDic.Values.Where(p => !p.Exists).ToList(); ;
            txt+="Dont exists:"+dontExists.Count;
            foreach (var item in dontExists)
            {
                txt+="<div>* "+item.UsedWhere+":<br />"+ item.Name+" <div>"+item.Debug+"</div></div>";
            }
         
            DebugLit.Text = txt;
        }
        
        GuidStatus CheckGuid(Guid guid)
        {
            var result = new GuidStatus();
            result.Guid = guid;
            var map = EPiServer.Web.PermanentLinkMapper.Instance.Find(guid);

            if (map is EPiServer.Web.PermanentContentLinkMap)
            {
                result.TargetType = "PAGE";
                try {
                    var p=EPiServer.DataFactory.Instance.Get<IContent>((map as EPiServer.Web.PermanentContentLinkMap).ContentReference);
                    result.Exists = true;
                    result.Content = p;
                    result.Name = p.Name + " [" + (map as EPiServer.Web.PermanentContentLinkMap).ContentReference + "]";
                    
                }
                catch (System.Exception error)
                {
                    result.Exists = false;
                    result.Debug = error.Message;
                }
            }
            if (map is EPiServer.Web.PermanentFileLinkMap)
            {
                result.TargetType = "FILE";
                var filMap = (map as EPiServer.Web.PermanentFileLinkMap);
                if (filMap != null && filMap.MappedUrl != null)
                {
                    var filPath = Uri.UnescapeDataString(filMap.MappedUrl.ToString());
                    result.Name = filPath;
                    if (System.Web.Hosting.HostingEnvironment.VirtualPathProvider.FileExists(filPath))
                    {
                        var fil = System.Web.Hosting.HostingEnvironment.VirtualPathProvider.GetFile(filPath);
                        try
                        {
                            result.Exists = false;
                            using (var stream = fil.Open())
                            {
                                if (stream.ReadByte() > -1)
                                {
                                    result.Exists = true;

                                }

                            }
                        }
                        catch (System.Exception error)
                        {
                            result.Exists = false;
                            result.Debug = error.Message;
                        }

                    }


                }
            }
            return result;
        }

        string IContentName(IContent content)
        {
            return content.Name + " [" + content.ContentLink + "]";
        }

        void Check(IContent content, Dictionary<Guid, GuidStatus> status, string path, bool addChildren,bool checkLinkedItems)
        {
            var contentName = IContentName(content);
            if (!status.ContainsKey(content.ContentGuid))
            {
                status.Add(content.ContentGuid, new GuidStatus() { Exists = true, Name = contentName, UsedWhere = path });
            }
            Check(content as IContentData, status, path + contentName, addChildren);
           
            if (checkLinkedItems)
            {
                var toDo = status.Values.Where(p => p.Parse).ToList();
                foreach (var toDoItem in toDo)
                {
                    toDoItem.Parse = false;
                }
                foreach (var toDoItem in toDo)
                {
                    if (toDoItem.Content != null)
                    {
                        Check(toDoItem.Content, status, toDoItem.Name, false,false);
                    }
                }
            }
            if (addChildren)
            {
               
                var children = EPiServer.DataFactory.Instance.GetChildren<IContent>(content.ContentLink);
              
                foreach (var child in children)
                {

                    Check(child, status, "", addChildren, checkLinkedItems);
                  
                }
               
       
            }
           
        }
        void Check(IContentData content, Dictionary<Guid, GuidStatus> status, string path, bool addChildren)
        {
           
            foreach (var prop in content.Property)
            {
                Check(prop, status, path+"."+prop.Name, addChildren);
            }
           
        }

        void Check(PropertyData prop, Dictionary<Guid, GuidStatus> status, string path, bool addChildren)
        {
           var refMap = prop as IReferenceMap;
            if (refMap != null)
            {

                var guids = refMap.ReferencedPermanentLinkIds;
                if (guids.Count > 0)
                {
                   

                
                    foreach (var filGuid in refMap.ReferencedPermanentLinkIds)
                    {
                      
                        if (!status.ContainsKey(filGuid))
                        {
                            var check = CheckGuid(filGuid);
                            check.UsedWhere = path;
                            status.Add(filGuid, check); ;
                            if (check.Content != null)
                            {
                                check.Parse = true;
                                //Check(check.Content, status, path + " =>", false);
                            }
                        }
                     
                    }
                   
                }


            }
            else if (prop is IContentData)
            {
               
                Check((prop as IContentData), status, path , false);
               
            }
          
        }

        public class GuidStatus
        {
            public Guid Guid { get; set; }
            public string Name { get; set; }
            public string UsedWhere { get; set; }
            public string TargetType { get; set; }
            public bool Exists {get;set;}
            public string Debug { get; set; }
            public IContent Content { get; set; }
            public bool Parse { get; set; }


        }
       
    }

And the front code is

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CheckFiles.aspx.cs" Inherits="Itera.Custom.PlugIns.CheckFiles" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 id="PageName" runat="server" />
        <asp:Button ID="BtnClicked" runat="server" Text="Check" OnClick="BtnClicked_Click" />
        <asp:CheckBox ID="DoChildren" runat="server" Text="Check children also" />
        <asp:CheckBox ID="CheckLinkedItems" runat="server" Text="Check linked items" />
        <div>
            <asp:Literal id="DebugLit" runat="server" />
        </div>
    </div>
    </form>
</body>
</html>
Aug 08, 2014

Comments

Please login to comment.
Latest blogs
Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog