A critical vulnerability was discovered in React Server Components (Next.js). Our systems remain protected but we advise to update packages to newest version. Learn More

Anders Hattestad
Aug 28, 2012
  4913
(0 votes)

Copy files and folders between VPP folders

Have made a plugin that copy files and folders between 2 VPP folders that are of different type.

The code is pretty straight forward, and thinks its strange that the buildt in copy / paste from the explorer view from episerver don’t support this.

image 

Code Snippet
  1. [GuiPlugIn(
  2. Area = PlugInArea.AdminMenu,
  3. DisplayName = "Kopiere filer",
  4. Url = "~/Custom/AdminPages/CopyFiles.aspx")]
  5. public partial class CopyFiles : SystemPageBase
  6. {
  7.     protected override void OnInit(EventArgs e)
  8.     {
  9.         base.OnInit(e);
  10.     }
  11.     protected void Start_Click(object sender, EventArgs e)
  12.     {
  13.             var source = System.Web.Hosting.HostingEnvironment.VirtualPathProvider.GetDirectory(CopyFrom.Text) as UnifiedDirectory;
  14.             var target = System.Web.Hosting.HostingEnvironment.VirtualPathProvider.GetDirectory(CopyTo.Text) as UnifiedDirectory;
  15.             StatusText.Text = DoCopy(source, target);
  16.  
  17.     }
  18.     string DoCopy(UnifiedDirectory source, UnifiedDirectory target)
  19.     {
  20.         string result = "<dir><strong>"+source.Name+"</strong>";
  21.         foreach (var fil in source.GetFiles())
  22.         {
  23.             string combined = Path.Combine(target.VirtualPath, fil.Name);
  24.             var newFile = System.Web.Hosting.HostingEnvironment.VirtualPathProvider.GetFile(combined) as UnifiedFile;
  25.             if (newFile == null)
  26.             {
  27.                 result += "<div>Copy file " + fil.Name;
  28.                 fil.CopyTo(combined);
  29.                 newFile = System.Web.Hosting.HostingEnvironment.VirtualPathProvider.GetFile(combined) as UnifiedFile;
  30.                 if (fil.Summary != null)
  31.                 {
  32.                     foreach (var key in fil.Summary.Dictionary.Keys)
  33.                     {
  34.                         newFile.Summary.Dictionary[key] = fil.Summary.Dictionary[key];
  35.                     }
  36.                     newFile.Summary.SaveChanges();
  37.                     result += " update Summary";
  38.                 }
  39.                 result += "</div>";
  40.             }
  41.             else
  42.             {
  43.                 result += "<div>File " + fil.Name+" exists</div>";
  44.             }
  45.         }
  46.         foreach (var dir in source.GetDirectories())
  47.         {
  48.             string combined = Path.Combine(target.VirtualPath, dir.Name);
  49.             var newDir = System.Web.Hosting.HostingEnvironment.VirtualPathProvider.GetDirectory(combined) as UnifiedDirectory;
  50.             if (newDir == null)
  51.             {
  52.                 newDir=target.CreateSubdirectory(dir.Name);
  53.             }
  54.             result += DoCopy(dir, newDir);
  55.         }
  56.         result += "</dir>";
  57.         return result;
  58.     }
  59.  
  60.     public override EPiServer.Security.AccessLevel RequiredAccess()
  61.     {
  62.         return AccessLevel.Edit;
  63.     }
  64.     public override AccessLevel QueryAccess()
  65.     {
  66.         PageData page = DataFactory.Instance.GetPage(PageReference.StartPage, LanguageSelector.MasterLanguage());
  67.         if (page != null)
  68.         {
  69.             return page.QueryAccess();
  70.         }
  71.         return AccessLevel.Read;
  72.     }
  73.  
  74.     protected override void OnPreInit(EventArgs e)
  75.     {
  76.         base.OnPreInit(e);
  77.         this.MasterPageFile = ResolveUrlFromUI("MasterPages/EPiServerUI.master");
  78.     }
  79.  
  80.        
  81. }

and have this litle front end file

Code Snippet
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CopyFiles.aspx.cs" Inherits="Custom.AdminPages.CopyFiles" %>
  2.  
  3. <asp:Content ContentPlaceHolderID="FullRegion" Runat="Server">
  4. <div>
  5.     CopyFrom:<asp:TextBox id="CopyFrom" runat="server" /><br />
  6.     CopyTo:<asp:TextBox id="CopyTo" runat="server" /><br />
  7.       
  8.     <asp:Button ID="Button1" Text="Start" runat="server"
  9.         onclick="Start_Click" />
  10.             
  11.     <hr />
  12.         
  13.     <hr />
  14.     <asp:Literal ID="StatusText" runat="server" />
  15.     <asp:Literal ID="DebugLit" runat="server" EnableViewState="false" />
  16. </div>
  17. </asp:Content>
Aug 28, 2012

Comments

Please login to comment.
Latest blogs
A day in the life of an Optimizely OMVP: Learning Optimizely Just Got Easier: Introducing the Optimizely Learning Centre

On the back of my last post about the Opti Graph Learning Centre, I am now happy to announce a revamped interactive learning platform that makes...

Graham Carr | Jan 31, 2026

Scheduled job for deleting content types and all related content

In my previous blog post which was about getting an overview of your sites content https://world.optimizely.com/blogs/Per-Nergard/Dates/2026/1/sche...

Per Nergård (MVP) | Jan 30, 2026

Working With Applications in Optimizely CMS 13

💡 Note:  The following content has been written based on Optimizely CMS 13 Preview 2 and may not accurately reflect the final release version. As...

Mark Stott | Jan 30, 2026

Experimentation at Speed Using Optimizely Opal and Web Experimentation

If you are working in experimentation, you will know that speed matters. The quicker you can go from idea to implementation, the faster you can...

Minesh Shah (Netcel) | Jan 30, 2026