Views: 5571
Number of votes: 3
Average rating:

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

Petter Klang
( By Petter Klang, 8/28/2012 1:41:49 PM)

Well that will come in handy

valdis
( By valdis, 8/29/2012 8:57:46 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
( By Anders Hattestad, 8/29/2012 9:00:19 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

Per Nergård
( By Per Nergård, 8/30/2012 4:07:26 PM)

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

Anders Hattestad
( By Anders Hattestad, 8/30/2012 9:10:19 PM)

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

Per Nergård
( By Per Nergård, 8/31/2012 10:57:33 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
( By Anders Hattestad, 8/31/2012 11:00:45 AM)

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

Per Nergård
( By Per Nergård, 8/31/2012 3:17:01 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
( By Anders Hattestad, 8/31/2012 8:12:02 PM)

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

Per Nergård
( By Per Nergård, 9/17/2012 4:46:18 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
( By Sajesh Morje, 6/18/2015 9:41:43 AM)

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

Please login to comment.