Try our conversational search powered by Generative AI!

Gadget automatically added for all users

Vote:
 

I would like two gadgets (none of witch are a part of my code base) to be added as default (left pane, below page tree) for all users:

  • The Versions gadget
  • The LanguageManager gadget (Episerver.Labs.LanguageManager)

Is that possible?

#192089
May 06, 2018 23:01
Vote:
 

Hi,

Yes you can do that by implementing an IViewTransformer and add the components if they haven't been added already.

A disclamer. I have not compiled the code after I added the LanguageManager code.

using System.Linq;
using System.Security.Principal;
using EPiServer.Cms.Shell.UI.Components;
using EPiServer.Shell;
using EPiServer.Shell.ViewComposition;
using EPiServer.Labs.LanguageManager;

namespace Alloy.Sample.Business.Initialization
{
    [ViewTransformer]
    public class DefaultComponentsTransform : IViewTransformer
    {
        private readonly IComponentManager _manager;

        public DefaultComponentsTransform(IComponentManager manager)
        {
            _manager = manager;
        }

        public int SortOrder => 300;

        public void TransformView(ICompositeView view, IPrincipal principal)
        {
            var container = view.RootContainer.FindContainerByPlugInArea(PlugInArea.Navigation);
            if (container == null)
            {
                return;
            }

            var exist = container.Components.Any(c => c.DefinitionName == typeof(VersionsComponent).FullName);
            if (!exist)
            {
                var definition = _manager.GetComponentDefinition(typeof(VersionsComponent).FullName);
                var component = definition.CreateComponent();
                component.SortOrder = 500; //

                container.Add(component);
            }

            // LanguageManager inherits from ComponentDefinitionBase which has a CreateComponent() method so there should be no need to
            // get the definition from the IComponentManager
            exist = container.Components.Any(c => c.DefinitionName == typeof(LanguageManagerComponent).FullName);
            if (!exist)
            {
                var definition = _manager.GetComponentDefinition(typeof(VersionsComponent).FullName);
                var component = new LanguageManagerComponent().CreateComponent();
                component.SortOrder = 510; //

                container.Add(component);
            }
        }
    }
}
#192101
May 07, 2018 13:29
Vote:
 

Very enlightening, thank you.

How can it be that even if a component/gadget is removed from the UI the container still holds that component?

For instance if I add a component i under navigation or assets, then remove it a reloads page, the component is still present in the above code.

#192357
May 15, 2018 15:39
Vote:
 

The solution by Magnus seems to have a little problem. If the user has specifically chosen to remove the components his setting will override the code, i.e. the components will not be (re-)added. Is there a way to 'override the override', i.e. to make sure that the components are always added?

NB. The mentioned user setting appears to be saved in the server database, but is not accessible via IProfileRepository.

#192418
May 16, 2018 14:51
Vote:
 

Hi,
The personalized settings are stored in the DDS.
And it works something like this.
1. We have a HomeView with a default config
2. When it is created we run all view configurations based on the sortOrder
3. We have an internal PersonalizationViewTransformer than takes applies the settings stored for the current user
So depending on when your view transformation run you will have different values in the containers.

If you e.g always want to add the Version component, even though the user has removed it, you need to have a sortOrder
that is higher than the sort order of out PersonalizationViewTransformer, which right now have the magic number of 10000.

When I test to set 10001 for the sort order of the example above I noticed that we have a bug :(. I have reported a bug for it.
It will re-add the removed component, but you get an error in the console.

And I suggest that you honor the Editors decision. If they have removed the component, they probably do not want it :)

#193203
May 25, 2018 8:05
Vote:
 

Hi Magnus!
I tried adding the component with SortOrder = 10001, but it does not get added.
However. if I add it manually it is now added twice..
Best regards,
Søren

#193271
May 28, 2018 10:39
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.