How to hide a SharePoint list column from a list form (New, Edit, and Display)

Managing the Content Type of a SharePoint List will provide you with the option to Show or Hide a column (aka Field) from the “New.aspx”, “Edit.aspx”, and “Display.aspx” list forms. If you didn’t want to show a column on any of the forms, you could select the column and ‘hide’ it from all forms.

Unfortunately, if you manage the content type of a list form, you are not able to choose which of these forms you can hide a column from. By default, hiding a column will be hidden on all forms.

In this post, I will show you how you can view all columns in your list and then choose which form to hide the column from.

I created this solution as a feature, using SharePoint Application Pages. Once the feature is activated, a new “Manage List Columns” custom action is added to the lists “General Settings” section on all lists and libraries.

General Settings

General Settings

Selecting the “Manage List Columns” custom action will take you to an application page that shows all columns in your list.

Column Listing

Column Listing

By default, all columns are being displayed in all forms (New, Edit, and Display). If you want to hide a column from one of the forms, you can select it and choose the form to hide the column from.

Modify the Column

Modify the Column

After you update the column, you can now see that this column is hidden from the form you selected to hide it from.

Column Modification Results

Column Modification Results

In this example, you see that the “Description” column is hidden from the “Display.aspx” form, but is still displayed on the “New.aspx” and “Edit.aspx” forms.

Display Form

Display Form hiding 'Description' field

 
New Form showing 'Description' field

New Form showing 'Description' field

 
Edit Form

Edit Form showing 'Description' field

Choosing to modify the list content type will allow you to ‘hide’ a column from all forms. You don’t have the option to choose which form to hide it from. This solution gives you that option.


The code to display or hide a column is shown below:

        public string OM_UpdateSPFieldShowFormAction(Guid _listGuid, Guid _fieldGuid, string _Form, bool _ShowForm) {
            string err = string.Empty;

            try {
                SPSecurity.RunWithElevatedPrivileges(delegate() {
                    SPSite site = SPContext.Current.Site;
                    SPWeb web = site.OpenWeb(SPContext.Current.Web.ID);
                    SPList list = web.Lists[_listGuid];
                    SPField field = list.Fields[_fieldGuid];

                    web.AllowUnsafeUpdates = true;
                    if (field.Sealed) {
                        isSealed = true;
                        field.Sealed = false;
                    }
                    switch (_Form) {
                        case "New":
                            if (_ShowForm) {
                                field.ShowInNewForm = true;
                            } else {
                                field.ShowInNewForm = false;
                            }
                            break;
                        case "Display":
                            if (_ShowForm) {
                                field.ShowInDisplayForm = true;
                            } else {
                                field.ShowInDisplayForm = false;
                            }
                            break;
                        case "Edit":
                            if (_ShowForm) {
                                field.ShowInEditForm = true;
                            } else {
                                field.ShowInEditForm = false;
                            }
                            break;
                        default:
                            break;
                    }
                    if (isSealed) {
                        field.Sealed = true;
                    }
                    field.Update();
                    list.Update();
                    web.AllowUnsafeUpdates = false;

                });
            } catch (Exception ex) {
                err = ex.ToString();
            } finally {
                isSealed = false;
            }
            return err;
        }

Hope this helps.

Leave a Reply