EPiServer: Using the new Unified File System

I hope you find my research on the new EPiServer CMS Unified File System useful. Here are some example code to get you going. The most tricky part is to manually create a page folder if it does not exsist

Getting a handle to an existing file:
UnifiedFile file = HostingEnvironment.VirtualPathProvider.GetFile(virtualPath) as UnifiedFile;

Get or Create a Page Folder:

int folderId = (int) page.Property["PageFolderID"].Value;

string pageDirectoryRootVirtualPath = VirtualPathHandler.PageDirectoryRootVirtualPath;
IPageDirectory pageDirectory = HostingEnvironment.VirtualPathProvider.GetDirectory(pageDirectoryRootVirtualPath) as IPageDirectory;
string virtualPathFromFolderId = VirtualPathUtilityEx.Combine(pageDirectoryRootVirtualPath, VirtualPathUtility.AppendTrailingSlash(folderId.ToString()));

UnifiedDirectory directory = HostingEnvironment.VirtualPathProvider.GetDirectory(virtualPathFromFolderId) as UnifiedDirectory;
if (directory == null) {
    directory = pageDirectory. CreateSubdirectory(folderId.ToString(), page);
}

NOTE! Folder will not “exist” for real until a file is saved inside it. It is important to call CreateSubdirectory with the page as parameter to setup security correctly!

Getting a handle to a new file:

UnifiedFile file = HostingEnvironment.VirtualPathProvider.GetFile(virtualPath) as UnifiedFile;
if (file == null) {
    string virtualDir = VirtualPathUtility.GetDirectory(virtualPath);
    UnifiedDirectory directory = HostingEnvironment.VirtualPathProvider.GetDirectory(virtualDir) as UnifiedDirectory;
    return directory.CreateFile(VirtualPathUtility.GetFileName(virtualPath));
}
return file;

Example of reading UnifiedFile:
Bitmap image = null;
using (Stream stream = file.Open()) {
image = (Bitmap) Image.FromStream(stream);
}

Example of writing with UnifiedFile:

using (Stream stream = file.Open(FileMode.Create, FileAccess.Write)) {
image.Save(stream, encoderInfo, encoderParams);
}

Bookmark and Share
  1. Jonas’s avatar

    So one way to upload a file by a visitor from a (public) form would be to use the Unified File System as described above? What I mean is, there is no EPiServer xform way?

    Reply

  2. Fredrik Haglund’s avatar

    Jonas, there is no support from to upload files with xforms (as far as I know).

    If you want that functionality you would have to implement it on the page template either below the xform or on the thank you page.

    Use ASP.NET’s FileUpload component and then use the stream available on yourFileUpload.PostedFile.InputStream.

    Reply

  3. Jonas’s avatar

    Thanx for the answer. That is how I will solve the problem.

    Well, at least there is no support for file upload in the EPiServer xforms. There seems to be one according to x3c
    http://www.w3.org/MarkUp/Forms/2003/xforms-for-html-authors.html#FileSelect

    :)

    Reply

  4. Fredrik Haglund’s avatar

    Jonas, EPiServer XForms is their own framwork to let editors build forms without knowing anything about html. Do not misstake it for being the same as the proposed standard for forms in xhtml from w3c.

    Reply

  5. Morten Strand-Langbakk’s avatar

    Hi Fredrik.
    Great posts on the new Unified File System. I have avtually more a question than a comment on your blog.

    Do you know when in the event stack the filesummary for a UnifiedFile is created and stored ?

    I have hooked into the UnifiedDirectory.UnifiedFileAdded event to add som data to the filesummary in code, but at this point the file.Summary is still null, so I cannot add my summary data…

    file.Summary.Dictionary["Searchmetadata"] = “some metadata I want to have indexed”;

    Reply

  6. Morten Strand-Langbakk’s avatar

    Solved it.
    Whenever a file is added the UnifiedFile.UnifiedFileCheckedIn is also called.
    I put my code in here and evrything works as a charm :-)

    Reply

  7. Morten Leerhøy’s avatar

    Hi Frederik,

    Thanks for this post – it cleared up a few things for me – though I can’t seem to get the UnifiedFile.UnifiedFileCheckedIn event to fire when adding a file with:

    UnifiedFile uf = pagedir.CreateFile(FileUpLoad.FileName);
    Stream fs = uf.Open(FileMode.Create);
    System.IO.BinaryWriter sw = new BinaryWriter(fs);
    sw.Write(FileUpLoad.FileBytes);
    sw.Flush();
    sw.Close();
    fs.Close();

    any suggestions?

    Reply

  8. Andrew’s avatar

    Hi. Fredrik
    When I create file in VPP dynamically from stream it stays checked out and I fail to read it. I’m receiving following error: “File has no checked in version and can only be access by the author”. Could you help me with fixing this?

    Reply

  9. Fredrik Haglund’s avatar

    Hi Andrew!

    I need to see some code to help you. Post in the forum and get back: http://world.episerver.com/Forum/

    Reply

  10. Andrew’s avatar

    Thanks, I have figure out this myself.
    To have ability to check-in or check-out UnifiedFile file, it should be converted to IVersionFile using method “TryAsVersioningFile”.

    Reply

  11. Jørgen Helgheim’s avatar

    Hi Fredrik

    Thanks for this post. It was very useful!

    But I got errors (GDI+) when using The suggested Bitmap save method. Instead I used Morten Leerhøy’s stream alternative. It worked fine! Deleted the last line of code: fs.Close();. It raised an exception, because it is already closed by sw.Close();

    Reply

  12. K’s avatar

    Hi
    When trying to get a file by:
    UnifiedFile file = HostingEnvironment.VirtualPathProvider.GetFile(virtualPath) as UnifiedFile;

    I get access denied if i dont set “bypassaccesscheck = true” in web.config. I wonder where i can set credentials for this access if its even possible? Does setting bypassaccesscheck to true make the files unsecure in any way or whats the consequenses?

    thanks in advance

    Reply

  13. Fredrik Haglund’s avatar

    K: Setting bypassaccesscheck to true in web.config for a Virtual Path Provider turns off security. A visitor that does not have access to edit or admin mode can usually not change the files.

    I suggest that you read the Administrators Manual (http://world.episerver.com/en/Documentation/Categories/Documentation-Type/Manuals/) and find the chapter about file security.

    /Fredrik

    Reply

  14. HesaPesa’s avatar

    using EPiServer.Web.Hosting;

    Reply