EPiWiki.se  - EPiServer notes shared with others
 

Create versioned file system objects from a file structure

[Edit]
A simple script to generate unified file system objects direct from the object store structure.

<%@ 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))
      {
          if (i is FileItem)
          {
              try
              {
                  UnifiedFile f = VirtualPathHandler.Instance.GetFile(
                    fromVirtualPath + i.Name, true) as UnifiedFile;
                  Response.Write("Create Unified file object for "
                    + f.VirtualPath + "<br/>");
                  Response.Flush();
              }
              catch (Exception ex)
              {
                  Response.Write("Can't create UFS object for file "
                    + fromVirtualPath + i.Name + " because, " + ex + "<br/>");
              }
          }
          else if (i is DirectoryItem)
          {
              try
              {
                  UnifiedDirectory d = VirtualPathHandler.Instance.GetDirectory(
                    fromVirtualPath + i.Name,
                    true) as UnifiedDirectory;
                  Response.Write("Create Unified directory object for "
                    + d.VirtualPath + "<br/>");
              }
              catch (Exception ex)
              {
                  Response.Write("Can't create UFS object for directory "
                    + fromVirtualPath + i.Name + " because, " + ex + "<br/>");
              }
              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:
Mattias Lövström

EPiServer version

All