Hiding EPiServer tabs on property pages programmatically

Vote:
 

When editing EPiServer property pages, I want to hide certain tabs for a page depending upon the site. I found a snippet of code on the web which looks like the following (I've modified it slightly to suit my needs):

[EPiServer.Shell.ObjectEditing.EditorDescriptors.EditorDescriptorRegistration(TargetType = typeof(EPiServer.Core.ContentData))]
        public class SiteMetadataExtender : EPiServer.Shell.ObjectEditing.EditorDescriptors.EditorDescriptor
        {
            public override void ModifyMetadata(EPiServer.Shell.ObjectEditing.ExtendedMetadata metadata, System.Collections.Generic.IEnumerable<System.Attribute> attributes)
            {
                List<string> groupsToHide = new List<string> { "Tab1", "Tab2" }

                if (SiteDefinition.Current.Name == "Site1" || SiteDefinition.Current.Name == "Site2")
                {
                    foreach (EPiServer.Shell.ObjectEditing.ExtendedMetadata property in metadata.Properties)
                    {
                        if (property.GroupSettings != null && groupsToHide.Contains(property.GroupSettings.Name))
                        {
                            property.GroupSettings.DisplayUI = false;
                            return;

                        }
                    }
                }
            }
        }

This code gets fired when I open a property page, however through most iterations, property.GroupSettings is null and I am not seeing my custom tabs enumerated.

Does anyone know how to fix this? Is this even the proper code to handle this scenario?

Thanks.

#229634
Oct 20, 2020 15:38
Vote:
 

https://gregwiechec.com/2018/03/hide-tabs-and-properties-in-edit-mode/ would be your best bet. Create an ILayoutVisibilityResolver class and then appy it to the Tab/Properties as needed

#229679
Oct 21, 2020 10:00
Vote:
 

Thanks for your responses. I had seen the blog posts that you both posted which I had tried. But I revisited my code. Here is the new code which seems to work just fine:

[EditorDescriptorRegistration(TargetType = typeof(EPiServer.Core.ContentData))]
        public class SiteMetadataExtender : EditorDescriptor
        {
            public override void ModifyMetadata(ExtendedMetadata metadata, System.Collections.Generic.IEnumerable<System.Attribute> attributes)
            {
                List<string> groupsToHide = new List<string> { "Tab1", "Tab2", "Tab3", "Tab4" };

                if (SiteDefinition.Current.Name == "Site1" || SiteDefinition.Current.Name == "Site2")
                {
                    foreach (EPiServer.Shell.ObjectEditing.ExtendedMetadata property in metadata.Properties)
                    {
                        if (property.GroupName != null && groupsToHide.Contains(property.GroupName) && property.GroupSettings != null)
                        {
                            property.GroupSettings.DisplayUI = false;
                        }
                    }
                }
            }
        }
#229810
Oct 23, 2020 18:09
* 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.