|Number of votes:||5|
Last Friday I saw a request on the forums if it was possible to change the order of the media and the shared blocks components so that the media component would be the one shown by default. I responded that I did not know a god way but since there seems to be several people wanting this I did a quick investigation if there was a decent ways to do this. I had three ideas to investigate:
When a view is created for the EPiServer UI, there are several steps and extension points. Basically the view itself defines the main structure of the view and the possible plug-in areas. If we look at the home view of CMS it looks something like this:
And after the view system has been created and components have been plugged in a default installation will look something like this:
For each view that is created there will be a recursive look through to see if there are components that should be plugged into the view. When components are created there are two entities that you should be aware of:
As a partner developer, you would usually only care about the ComponentDefinition and not the actual components created since this is mostly a bearer of data for the current view.
If you want to know more how the view is set up I suggest reading the following SDK article: http://sdkbeta.episerver.com/SDK-html-Container/?path=/SdkDocuments/EPiServerFramework/7/Knowledge%20Base/Developer%20Guide/User%20Interface/View%20Composition/View%20Composition.htm&vppRoot=/SdkDocuments//EPiServerFramework/7/Knowledge%20Base/Developer%20Guide/
The first idea I had was to get hold of the singleton instance of the File Manager Component Definition in an initialization module and just change the sort order of the instance. The code I tried looks like this:
Note: I’m using "EPiServer.Cms.Shell.UI.Components.MediaComponent" instead of typeof(MediaComponent).FullName to not have to take a dependency on the CMS UI add-on. This is not needed in EPiServer 7.6 and above since it's been moved to the bin folder.
Unfortunately the code above does not compile since SortOrder has a protected setter so we have to move to the next potential solution.
The next idea I had was to replace the media component by registering a custom component in the IOC container (http://sdkbeta.episerver.com/SDK-html-Container/?path=/SdkDocuments/EPiServerFramework/7/Knowledge%20Base/Developer%20Guide/User%20Interface/View%20Composition/How%20To/Replace%20a%20component%20globally.htm&vppRoot=/SdkDocuments//EPiServerFramework/7/Knowledge%20Base/Developer%20Guide/).
The idea is that whenever a component should be created, it’s possible to let the IOC container create an instance of custom type registered in the container. The code to do this would look something like this:
Though doable I noticed that I have to create my custom component without any reference to the original component definition. This would simply cause too much copy and paste of settings to make a copy of the standard media component and with the risc of breaking when upgrading EPiServer. So I decided to move on to the third option…
Last idea I had was to register an IViewTransformer. An IViewTransformer can be used to modify the view and there are actually quite a few used in the system:
The idea here is to modify the sort order of the file manager component before the sort component transformer does the actual sorting. The code looks as follows:
Actually, on this third attempt we have a working solution.
There is one small caveat though that you should know about:
If the user has already personalized the Assets Pane, the sorting will be overridden by the personalization since this is applied after the sorting. This shouldn’t be a real problem as long as you implement the sorting before the users start using the system. If you add this on a system in use you might want to clear your views as described here: http://world.episerver.com/Blogs/Linus-Ekstrom/Dates/2013/1/Resetting-your-EPiServer-views/