Anders Hattestad
Aug 28, 2012
  6566
(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.

Sajesh Morje
Sajesh Morje 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 For you Intranet

Having been at Optimizely and in the CMS industry for nearly 16 years I have seen clients’ intranet requirements go from a simple site just to hous...

Robert Folan | Sep 22, 2023

Vulnerability in EPiServer.GoogleAnalytics v3 and v4

Introduction A potential security vulnerability was detected for Optimizely Google Analytics addon (including EPiServer.GoogleAnalytics and...

Bien Nguyen | Sep 20, 2023

Overriding Optimizely’s Content Recommendations Block to Implement Custom Recommendations

Introduction The Content Recommendations add-on for Optimizely CMS dynamically recommends content from your site tailored to the interests of each...

abritt | Sep 13, 2023 | Syndicated blog

Developer contest! Install the AI Assistant and Win Bose QC45 Headphones!

We are thrilled to announce a developer contest where you have the chance to win a pair of Bose Headphones. The goal is to be the first developer t...

Luc Gosso (MVP) | Sep 7, 2023 | Syndicated blog

Send Optimizely notifications with SendGrid API, not SMTP

If your Optimizely site already sends transaction emails through an email platform API, why not do the same with Optimizely notification emails?

Stefan Holm Olsen | Sep 6, 2023 | Syndicated blog

Optimizely Configured Commerce Custom POST API

Introduction When creating custom API controllers for an Optimizely B2B project it’s possible you’ll want to create POST calls. Following the...

Dylan Barter | Sep 6, 2023