World is now on Opti ID! Learn more

Anders Hattestad
Aug 28, 2012
  4869
(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
Make Global Assets Site- and Language-Aware at Indexing Time

I had a support case the other day with a question around search on global assets on a multisite. This is the result of that investigation. This co...

dada | Jun 26, 2025

The remote server returned an error: (400) Bad Request – when configuring Azure Storage for an older Optimizely CMS site

How to fix a strange issue that occurred when I moved editor-uploaded files for some old Optimizely CMS 11 solutions to Azure Storage.

Tomas Hensrud Gulla | Jun 26, 2025 |

Enable Opal AI for your Optimizely products

Learn how to enable Opal AI, and meet your infinite workforce.

Tomas Hensrud Gulla | Jun 25, 2025 |

Deploying to Optimizely Frontend Hosting: A Practical Guide

Optimizely Frontend Hosting is a cloud-based solution for deploying headless frontend applications - currently supporting only Next.js projects. It...

Szymon Uryga | Jun 25, 2025

World on Opti ID

We're excited to announce that world.optimizely.com is now integrated with Opti ID! What does this mean for you? New Users:  You can now log in wit...

Patrick Lam | Jun 22, 2025

Avoid Scandinavian Letters in File Names in Optimizely CMS

Discover how Scandinavian letters in file names can break media in Optimizely CMS—and learn a simple code fix to automatically sanitize uploads for...

Henning Sjørbotten | Jun 19, 2025 |