Sitefinity: File Manager Default Folder

By in
No comments

One of the toughest roadblocks I’ve had to deal with while training and supporting users of sitefinity is the issue of uploading files and images through the editor. The default behavior is to start the user in the root of the site, allowing them access to all files and folders, possibly even granting them the ability to delete the whole site! Having trustworthy users is certainly a plausible workaround, but unfortunately not a guaranteed one. And besides that, having to navigate into images -> department -> subfolder1 -> subsubfolder2 -> newimages gets old quickly, especially if you have more than one image that needs to be added.

Fortunately, after a few weeks of code-rummaging through the sitefinity backend, I’ve found a way to improve the situation. It’s not perfect, but it’s another step closer, and isn’t that what it’s all about? No? Well it was still fun to do… so here goes!

Note that these changes are for the FileManager located INSIDE OF RADEDITOR and NOT the default CMS File Manager accessible from the tabstrip. I’m sure that similiar functionality could be achieved following these examples, but I was not in need of this feature and therefore lazied out of it :P)

There are three areas that have to be updated, the ItemSelectorTemplate, which is where we set the default ‘startup’ folder for the File Manager; the Document and Image EditorDialog, which is where we set the default upload folder to receive files, and finally the EditorTemplate, which is where we set View/Upload/Delete permissions for the RadEditor itself. I’ve split this series into two parts; today we focus on the Default Start Folder.

The Default Start Folder – ItemSelectorTemplate

The related user control that we’ll need to modify is located in the folder /Sitefinity/Admin/ControlTemplates/Libraries/Dialogs/ItemSelectorTemplate.ascx. The first thing you need to do is replace this control with a standard UserControl (one that includes code-behind). As I outlined in this forum post about resizing gallery images (which will be posted here in my blog soon, I hope!), the best way is to copy everything in the control, delete the file, then add a new UserControl using the Visual Studio dialog, making sure to use the exact same filename as the original file. Then paste the content from the old control, and you’re good to go!

Item selection (such as images or documents) is handled by a single sitefinity control called urlWebEditor. Now, this was the tough part of all of this (well for me anyway), as I had to spend a few hours scouring through the Sitefinity core, trying to figure out just how the hell this Item Selector dialog worked and where it is configured…

Fortunately you don’t have to worry about all that! All you need to know is that it uses the Telerik.FileManager.ManageFiles class, which is a control added to the urlWebEditor control collection during load. All we have to do is locate this control in the urlWebEditor control collection, and change its properties based on the role of the user. Since this is the only control loaded into the collection, we can grab it by its index:

protected void Page_Load(object sender, EventArgs e)
{
    Telerik.FileManager.ManageFiles mgr = urlWebEditor.Controls[0] as Telerik.FileManager.ManageFiles;
}

First, we need a way to determine what Mode we are in, either Images or Documents. (NOTE: This is an optional step which you may or many not require based on how you organize your files. In my case, I have images stored in one location, and documents stored in another, with each location having matching subfolders to separate images from documents. If you have all both your files and images together in one location, you could easily modify this to bypass this step).

This is accomplished by noting that the urlWebEditor is contained within a ContainerTemplate, which is then located inside of the ItemSelector control itself. Since it’s the ItemSelector which determines the current mode, we have to reflect back up the chain to grab an instance of the parent’s parent, and cast it to the type we want to find the current mode. I’ve wrapped this functionality into a local property:

ItemSelector.ViewMode CurrentMode
{
    get {
        ItemSelector parent = this.Parent.Parent as ItemSelector;
        return parent.ShowMode;
    }
}

Now we can use the results of this property to set our file path.

protected void Page_Load(object sender, EventArgs e)
{
    Telerik.FileManager.ManageFiles mgr = urlWebEditor.Controls[0] as Telerik.FileManager.ManageFiles;
    string path = CurrentMode == ItemSelector.ViewMode.Images ? "~/images" : "~/docs";
    if (Page.Page.User.IsInRole("departmentA"))
        path += "/departmentA";
    else if (Page.User.IsInRole("deptartmentB"))
        path += "/deptB";
    
    mgr.SelectedFolder = new System.IO.DirectoryInfo(Server.MapPath(path));
}

Obviously, you should change the path based on the relationship between roles and your file system, the basic idea is pretty straightforward. As you can see, once you have the default path, it’s simply a matter of setting the SelectedFolder property of the urlWebEditor control.

And that’s it! The file manager will now start in the default folder you set, based on the user role! Tomorrow we’ll tackle part two, and set the default upload folder for incoming files. Stay tuned and thanks for reading!

The following two tabs change content below.

selaromdotnet

Senior Developer at iD Tech
Josh loves all things Microsoft and Windows, and develops solutions for Web, Desktop and Mobile using the .NET Framework, Azure, UWP and everything else in the Microsoft Stack. His other passion is music, and in his spare time Josh spins and produces electronic music under the name DJ SelArom. His other passion is music, and in his spare time Josh spins and produces electronic music under the name DJ SelArom.