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.
- [GuiPlugIn(
- Area = PlugInArea.AdminMenu,
- DisplayName = "Kopiere filer",
- Url = "~/Custom/AdminPages/CopyFiles.aspx")]
- public partial class CopyFiles : SystemPageBase
- {
- protected override void OnInit(EventArgs e)
- {
- base.OnInit(e);
- }
- protected void Start_Click(object sender, EventArgs e)
- {
- var source = System.Web.Hosting.HostingEnvironment.VirtualPathProvider.GetDirectory(CopyFrom.Text) as UnifiedDirectory;
- var target = System.Web.Hosting.HostingEnvironment.VirtualPathProvider.GetDirectory(CopyTo.Text) as UnifiedDirectory;
- StatusText.Text = DoCopy(source, target);
- }
- string DoCopy(UnifiedDirectory source, UnifiedDirectory target)
- {
- string result = "<dir><strong>"+source.Name+"</strong>";
- foreach (var fil in source.GetFiles())
- {
- string combined = Path.Combine(target.VirtualPath, fil.Name);
- var newFile = System.Web.Hosting.HostingEnvironment.VirtualPathProvider.GetFile(combined) as UnifiedFile;
- if (newFile == null)
- {
- result += "<div>Copy file " + fil.Name;
- fil.CopyTo(combined);
- newFile = System.Web.Hosting.HostingEnvironment.VirtualPathProvider.GetFile(combined) as UnifiedFile;
- if (fil.Summary != null)
- {
- foreach (var key in fil.Summary.Dictionary.Keys)
- {
- newFile.Summary.Dictionary[key] = fil.Summary.Dictionary[key];
- }
- newFile.Summary.SaveChanges();
- result += " update Summary";
- }
- result += "</div>";
- }
- else
- {
- result += "<div>File " + fil.Name+" exists</div>";
- }
- }
- foreach (var dir in source.GetDirectories())
- {
- string combined = Path.Combine(target.VirtualPath, dir.Name);
- var newDir = System.Web.Hosting.HostingEnvironment.VirtualPathProvider.GetDirectory(combined) as UnifiedDirectory;
- if (newDir == null)
- {
- newDir=target.CreateSubdirectory(dir.Name);
- }
- result += DoCopy(dir, newDir);
- }
- result += "</dir>";
- return result;
- }
- public override EPiServer.Security.AccessLevel RequiredAccess()
- {
- return AccessLevel.Edit;
- }
- public override AccessLevel QueryAccess()
- {
- PageData page = DataFactory.Instance.GetPage(PageReference.StartPage, LanguageSelector.MasterLanguage());
- if (page != null)
- {
- return page.QueryAccess();
- }
- return AccessLevel.Read;
- }
- protected override void OnPreInit(EventArgs e)
- {
- base.OnPreInit(e);
- this.MasterPageFile = ResolveUrlFromUI("MasterPages/EPiServerUI.master");
- }
- }
and have this litle front end file
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CopyFiles.aspx.cs" Inherits="Custom.AdminPages.CopyFiles" %>
- <asp:Content ContentPlaceHolderID="FullRegion" Runat="Server">
- <div>
- CopyFrom:<asp:TextBox id="CopyFrom" runat="server" /><br />
- CopyTo:<asp:TextBox id="CopyTo" runat="server" /><br />
- <asp:Button ID="Button1" Text="Start" runat="server"
- onclick="Start_Click" />
- <hr />
- <hr />
- <asp:Literal ID="StatusText" runat="server" />
- <asp:Literal ID="DebugLit" runat="server" EnableViewState="false" />
- </div>
- </asp:Content>
Well that will come in handy
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 :)
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
Does it change the underlying links in the database? Ie if going from a nativeprovider to the versioningprovider?
Its a copy, so it will not change the existing files. But the new ones are in the correct version
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.
No you cant do that. You get an error (picture i blog post)
This is why I have made this code :)
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.
Thats the whole reasone for this code. Have some files in a versioning provider and wants to move them to an other site :)
Crap. It didn't work for a Sharepointkonnektor VPP to neither native or versioning provider.
The same error as doing it via the FileManager.
What if I want to copy the files and folders from versioning to native VPP folder?