London Dev Meetup Rescheduled! Due to unavoidable reasons, the event has been moved to 21st May. Speakers remain the same—any changes will be communicated. Seats are limited—register here to secure your spot!

Anders Hattestad
Aug 28, 2012
  7154
(3 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

Petter Klang
Petter Klang Aug 28, 2012 01:41 PM

Well that will come in handy

valdis
valdis Aug 29, 2012 08:57 AM

This is handy for sure. Could you pack this a nuget and adding a package I would have feature appear by black magic somewhere?

And make it async that user can see the progress of files to be copied :)

Anders Hattestad
Anders Hattestad Aug 29, 2012 09:00 AM

Thanks

Only made it to do a spesific job, Guess I will not work anymore on it, since the folder I needed to copy was not that large :), but you are free to modify it

Aug 30, 2012 04:07 PM

Does it change the underlying links in the database? Ie if going from a nativeprovider to the versioningprovider?

Anders Hattestad
Anders Hattestad Aug 30, 2012 09:10 PM

Its a copy, so it will not change the existing files. But the new ones are in the correct version

Aug 31, 2012 10:57 AM

Another way to do it if you have access to the actual files is to just create a new vpp of the desired type and use the builtin drag and drop functionality to copy the files to the new vpp.

Anders Hattestad
Anders Hattestad Aug 31, 2012 11:00 AM

No you cant do that. You get an error (picture i blog post)
This is why I have made this code :)

Aug 31, 2012 03:17 PM

I didn't mean drag and drop between the vpps then you get an error. But say you have a native provider and access to the underlying fileshare.

Then you can get the files to you desktop and use the computer to epi vpp drag and drop functionality.

Sorry for not being specific enough.

Anders Hattestad
Anders Hattestad Aug 31, 2012 08:12 PM

Thats the whole reasone for this code. Have some files in a versioning provider and wants to move them to an other site :)

Sep 17, 2012 04:46 PM

Crap. It didn't work for a Sharepointkonnektor VPP to neither native or versioning provider.
The same error as doing it via the FileManager.

Jun 18, 2015 09:41 AM

What if I want to copy the files and folders from versioning to native VPP folder?

Please login to comment.
Latest blogs
Optimizely Product Recommendation Troubleshooting

In today’s fast-paced digital landscape, personalization is everything . Customers expect relevant, tailored experiences whenever they interact wit...

Sanjay Kumar | Apr 28, 2025

Natural Language Q&A in Optimizely CMS Using Azure OpenAI and AI Search

In Part 2, we integrated Azure AI Search with Azure Personalizer to build a smarter, user-focused experience in Optimizely CMS. We used ServiceAPI ...

Naveed Ul-Haq | Apr 25, 2025 |

Identifying Spike Requests and Issues in Application Insights

Sometimes within the DXP we see specific Azure App Instances having request spikes causing performance degredation and we need to investigate. I fi...

Scott Reed | Apr 25, 2025

Optimizely Frontend Hosting Beta – Early Thoughts and Key Questions

Optimizely has opened the waitlist for its new Frontend Hosting capability. I’m part of the beta programme, but my invite isn’t due until May, whil...

Minesh Shah (Netcel) | Apr 23, 2025