World is now on Opti ID! Learn more

Vladimir Terziyski
Mar 26, 2010
  9511
(0 votes)

Using EPiServer VirtualPathUnifiedProvider

Searching the Internet for EPiServer's VirtualPathUnifiedProvider, I didn't find much information.
So here I'm going to show you some basic examples how you can use it in your EPiserver projects.

VirtualPathUnifiedProvider inherits from asp.net VirtualPathProvider, and adds new functionality. It serves as a base class to all unified providers like VirtualPathNativeProvider, VirtualPathVersioningProvider. It is not ment to be used directly therefore it is abstract and you can use it to implement your own file system, for example a database file system. The most important classes which this provider works with are UnifiedDirectory, UnifiedFile, UnifiedSummary, UnifiedVersion.

Accessing files and folders.

VirtualPathUnifiedProvider vpp =
            System.Web.Hosting.HostingEnvironment.VirtualPathProvider as VirtualPathUnifiedProvider;

        if (vpp != null)
        {
            // get all file names in a directory
            List<string> fileNames = new List<string>();
            UnifiedDirectory dir = vpp.GetDirectory("/upload") as UnifiedDirectory;
            if (dir != null)
            {
                System.Collections.IEnumerable allFiles = dir.Files;
                foreach (UnifiedFile file in allFiles)
                {
                    fileNames.Add(file.Name);
                }
            }
            // get all subdirectories
            List<string> subdirectoryNames = new List<string>();
            if (dir != null)
            {
                System.Collections.IEnumerable allSubDirs = dir.Directories;
                foreach (UnifiedDirectory subDir in allSubDirs)
                {
                    subdirectoryNames.Add(subDir.Name);
                }
            }
        }

The check here for vpp is neccessary because the current registered provider can be of another type.
GetDirectory is base method of VirtualDirectory. If it doesn't find such directory it's result will vary depending of your custom provider implementation. For example it can be null, or MapPathBasedVirtualDirectory.

Creating file and subdirectories is straightforward:
if (dir != null)
{
// create new file
    UnifiedFile newFile = dir.CreateFile("CustomContentFile.txt");
    using (Stream fs = newFile.Open(FileMode.Create, FileAccess.ReadWrite, FileShare.None))
    {
        using (StreamWriter writer = new StreamWriter(fs))
        {
            writer.WriteLine("Custom content");
     }
}
  // Create and copy subdirectory
if (dir != null)
{
    UnifiedDirectory subdir1 = dir.CreateSubdirectory("subdirectory1");
    UnifiedDirectory subdir2 = dir.CreateSubdirectory("subdirectory2");
    subdir2.CopyTo(VirtualPathUtility.Combine(subdir1.VirtualPath, subdir2.Name));
  }
}

FileMode options here can vary. It can be either Create or CreateNew. FileMode.Open can be used if the file already exists and you want to write to it, instead of creating a new one. When copying a directory, the new path has to be specified. This is why the destination path of subdir1 is combined with the name of subdir2.

System.Web.VirtualPathUtility is used for file and directory related operations. It has various helper methods like Combine, GetDirectory, GetExtension, GetFileName, IsAbsolute, etc. 

This example renames already existing file:

UnifiedFile customFile = vpp.GetFile("/Upload/CustomContentFile.txt") as UnifiedFile;
if (customFile != null)
{
    string destinationDir = VirtualPathUtility.GetDirectory(customFile.VirtualPath);
    customFile.MoveTo(VirtualPathUtility.Combine(destinationDir, "NewFilename.txt"));
}

MoveTo can be used either for renaming or moving a file. If destination directory is the same as the source file directory - the file name will be changed, otherwise the file will be moved to the new location.

These are some of the basic operations, which you can do with VirtualPathUnifiedProvider. However, if you create a custom implementation, its behavior can be slightly different. One great way to test it is by EPiServer's file manager. If everything works fine there, then these examples  should work also.

Mar 26, 2010

Comments

Please login to comment.
Latest blogs
Make Global Assets Site- and Language-Aware at Indexing Time

I had a support case the other day with a question around search on global assets on a multisite. This is the result of that investigation. This co...

dada | Jun 26, 2025

The remote server returned an error: (400) Bad Request – when configuring Azure Storage for an older Optimizely CMS site

How to fix a strange issue that occurred when I moved editor-uploaded files for some old Optimizely CMS 11 solutions to Azure Storage.

Tomas Hensrud Gulla | Jun 26, 2025 |

Enable Opal AI for your Optimizely products

Learn how to enable Opal AI, and meet your infinite workforce.

Tomas Hensrud Gulla | Jun 25, 2025 |

Deploying to Optimizely Frontend Hosting: A Practical Guide

Optimizely Frontend Hosting is a cloud-based solution for deploying headless frontend applications - currently supporting only Next.js projects. It...

Szymon Uryga | Jun 25, 2025

World on Opti ID

We're excited to announce that world.optimizely.com is now integrated with Opti ID! What does this mean for you? New Users:  You can now log in wit...

Patrick Lam | Jun 22, 2025

Avoid Scandinavian Letters in File Names in Optimizely CMS

Discover how Scandinavian letters in file names can break media in Optimizely CMS—and learn a simple code fix to automatically sanitize uploads for...

Henning Sjørbotten | Jun 19, 2025 |