File Selection Drop Down List

by: Allan Thræn

I just got a request for a code-sample on how to display a list of files from the unified file system in a drop-down list in EPiServer CMS 5, so I composed a small example (thanks for your kind help, Ruwen!).

First I define the drop-down-list - this part is easy (and boring):

<asp:DropDownList runat="server" ID="DDL1" AutoPostBack="true"  OnSelectedIndexChanged="DDL1_SelectedIndexChanged" />

When a user selects a file, I want his download to start immediately - so I set the event "OnSelectedIndexChanged" and turn on auto postback. In my code-behind I make sure to include a Using EPiServer.Web.Hosting;  cause all that's the namespace where all the fun stuff is.

Here is my code-behind:

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Select the virtual path providers. Providers can be seen / defined in Web.Config.
                VirtualPathUnifiedProvider provider = (VirtualPathUnifiedProvider)VirtualPathHandler.GetProvider("SiteDocuments");
                if (provider.DirectoryExists(provider.VirtualPathRoot))
                {
                    UnifiedDirectory vdir = (UnifiedDirectory)provider.GetDirectory(provider.VirtualPathRoot);
                    DDL1.DataTextField = "Name"; //Show the filenames in the dropdown list
                    DDL1.DataValueField = "VirtualPath"; //And keep their virtual path as the key
                    DDL1.DataSource = vdir.GetFiles();
                }
                this.DataBind();
            }
            
        }

        public void DDL1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Redirect to download of file
            Response.Redirect(DDL1.SelectedValue,true);
        }

The trick is to find the right VirtualPathProvider. If you look in web.config there's a couple defined:

<virtualPath customFileSummary="~/FileSummary.config">
  <providers>
    <add showInFileManager="false" virtualName="Page Files" virtualPath="~/PageFiles/" bypassAccessCheck="false" name="SitePageFiles" type="EPiServer.Web.Hosting.VirtualPathVersioningProvider,EPiServer" indexingServiceCatalog="Web" physicalPath="%USERPROFILE%\KnowledgeWeb\PageFiles"/>
    <add showInFileManager="true" virtualName="Global Files" virtualPath="~/Global/" bypassAccessCheck="false" name="SiteGlobalFiles" type="EPiServer.Web.Hosting.VirtualPathVersioningProvider,EPiServer" indexingServiceCatalog="Web" physicalPath="%USERPROFILE%\KnowledgeWeb\Globals"/>
    <add showInFileManager="true" virtualName="Documents" virtualPath="~/Documents/" bypassAccessCheck="false" maxVersions="5" name="SiteDocuments" type="EPiServer.Web.Hosting.VirtualPathVersioningProvider,EPiServer" physicalPath="%USERPROFILE%\KnowledgeWeb\Documents"/>
  </providers>

I  use SiteDocuments, cause that's where I want to get my files from. You can of course use the VirtualPathRoot as a base to construct virtual paths to sub-folders, etc. If you want to use PageFiles, you can get the PageFolderID (which is in the path) from the PageData object.

27 November 2007


Comments

Post a comment    
User verification Image for user verification  
Allan Thræn

About me

I am a product manager @ EPiServer, with a passion for the more geeky side of things. My technical interests are typically focused around user problems, user experience,  search, information management, artificial intelligence and  personalization

On top of this blog I have the blog Allan On Technology and I often crosspost.

DISCLAIMER: Unless otherwise stated in the posts, this blog expresses my personal opinions, experiments and views, not necessarilly the views of EPiServer AB.

 458 page views this week.

 

 

Syndications


Archive


Tag cloud

EPiTrace logger