Anders Hattestad
Aug 8, 2014
  4434
(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
Optimizely Forms: You cannot submit this form because an administrator has turned off data storage.

Do not let this error message scare you, the solution is quite simple!

Tomas Hensrud Gulla | Oct 4, 2024 | Syndicated blog

Add your own tools to the Optimizely CMS 12 admin menu

The menus in Optimizely CMS can be extended using a MenuProvider, and using the path parameter you decide what menu you want to add additional menu...

Tomas Hensrud Gulla | Oct 3, 2024 | Syndicated blog

Integrating Optimizely DAM with Your Website

This article is the second in a series about integrating Optimizely DAM with websites. It discusses how to install the necessary package and code t...

Andrew Markham | Sep 28, 2024 | Syndicated blog

Opticon 2024 - highlights

I went to Opticon in Stockholm and here are my brief highlights based on the demos, presentations and roadmaps  Optimizely CMS SaaS will start to...

Daniel Ovaska | Sep 27, 2024