A blog about understanding and extending Episerver. May contain traces of posts not concerning Episerver.

Create new version of file

With the old VPP file system you could create a new version of a file by right click the file and select Create new version. With the new file system this function has disappeared. You can however update a file by uploading a new file with the same name. A customer to us needed the old method back because of various reasons.

We wanted a quick solution for this feature, so we decided to add a custom template to our GenericMedia-type (inherited from MediaData) where the editor could upload a new version of the file. To accomplish this we needed to go through three steps.

Create a view

Create a Controller with a TemplateDescriptor

Create a UIDescriptor that tells GenericMedia to use OnPageEditView

1. View

First we create a view. We know that this View will get a model of GenericMedia, and that we need a form where we can upload a file. We also add a way to send feedback from the Controller via ViewData.

<input type="submit"value="Update file"onclick="return confirm('Are you sure you want to update this file?');"/>

</form>

2. Controller with TemplateDescriptor

Now we create a controller to the View. The Controller needs a Index-method where we can pass in a HttpPostedFileBase if this is posted from the View. If this is passed into the method we add some logic to update the file-data of the current content.

We also need to add a TemplateDescriptor-attribute to this controller. In the attribute we define that this controller should be used with RenderingTag.Edit, that it’s a MvcController and where we can find the view.

3. UIDescriptor

Finally we need an UIDescriptor to say that when editing a GenericMedia, we need to see the OnPageEditView. Otherwise we will always see the AllPropertiesView when editing the media. The UIDescriptor is defined by creating a class that inherits from UIDescriptor and with the attribute [UIDescriptorRegistration].

C#

1

2

3

4

5

6

7

8

9

10

[UIDescriptorRegistration]

publicclassGenericMediaUIDescriptor:UIDescriptor<GenericMedia>

{

publicGenericMediaUIDescriptor()

:base("icon-document")

{

DefaultView=CmsViewNames.OnPageEditView;

DisabledViews=newstring[]{};

}

}

At last

If you know a smoother way of doing this, please leave a reply in the comments. 🙂