Sitecore – Hide Field on Content Editor

Webmastering task is performed on the Content Editor whereby it allows content authors to create, update and delete content items. To reduce the risk of content authors making unwanted changes, you can easily make use of the Sitecore Security and User Access Editor. This will allow you to allow content authors not to be able to perform certain action on the item.

What about Fields? In order to prevent the webmasters from editing specific field, normally, you will do this from the User Access or Security Editor to disable the read and write access.

But you can actually do this in another way. Let say you have a field which is updated upon saving the item. For example, you have a field where all the selected Item Ids of a MultiList FieldType is being stored. Instead of setting up the Security, you can write a piece of code which hide the field when the content editor is loading the item.

Here is an example of the code snippet which allows you to hide the field

public void Process(GetContentEditorFieldsArgs args)
{
    Assert.ArgumentNotNull(args, "args");
    if (args.Sections == null || args.Sections.Count == 0)
    {
        return;
    }

    this.Args = args;
    if (this.Args.Item != null && this.Args.Item.Paths.FullPath.StartsWith("/sitecore/content", System.StringComparison.OrdinalIgnoreCase))
    {
        //Get Id of the different fields to hide. Example:
        string excludedFields = "{AC2F70FF-F2C3-44AE-8F2E-6BFE44162DDC}";
        
        if (!string.IsNullOrWhiteSpace(excludedFields))
        {
            //determine the template sections that needs to be hidden and remove that from the list
            this.Args.Sections.RemoveAll(p => excludedFields.Contains(p.ID.ToString()));

            //determine the fields that needs to be hidden and remove those from the content editor list
            this.FilterFieldsFromSections(excludedFields);

        }
    }
}

private void FilterFieldsFromSections(string excludedFields)
{
    if (this.Args.Sections != null && this.Args.Sections.Count == 0)
    {
        return;
    }
    
    for (int fieldIndex = 0; fieldIndex < this.Args.Sections.Count; fieldIndex++)     {         Editor.Section currentSection = this.Args.Sections[fieldIndex];         if (currentSection != null &&             currentSection.Fields != null)         {             currentSection.Fields.RemoveAll(p => p.ItemField != null && excludedFields.Contains(p.ItemField.ID.ToString()));
        }
    }
}

Then you will need to patch the pipeline getContentEditorFields in order to trigger the code:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <getContentEditorFields>
        <processor type="YourNamespaceWithClassName, YourAssemblyName"/>
      </getContentEditorFields>
    </pipelines>
  </sitecore>
</configuration>

Note that your pipeline needs to get triggered last else it will get overriden by Sitecore Pipeline getContentEditorFields.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s