We have three types of List Forms in SharePoint.,
New Form, Edit Form and Display Form.
For some times or in some requirements, we need to hide some of the fields in List Forms. By OOB there is no page or option for hiding fields on separate form, but we can hide the fields in all forms in List.
I am just giving snippet to hide the Fields in each separate form, separately,
using (SPSite site = new SPSite(“http://localhost”))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists.TryGetList(“Custom”);
SPField field = list.Fields.GetFieldByInternalName(“field1”);
//To hide the field from New Form
field.ShowInNewForm = false;
//To hide the field from Edit Form
field.ShowInEditForm = false;
//To hide the field from Display Form
field.ShowInDisplayForm = false;
field.Update();
}
}
We can also show / hide the fields based on content Types. For Content Types, we can retrieve the field from Content Type.
SPField field = list.ContentTypes["ctype1"]. Fields.GetFieldByInternalName("field1");
and the add the further lines to hide the field in SharePoint Lists.
For revert back, we can set the ShowInNewForm property as true.
August 30, 2012 at 9:19 PM
This is nice. But where do I tap into to add the code?
September 28, 2012 at 1:15 AM
Hi, the code looks simple and lucid but we don’t know what to do with the code. If you can please give us step by step guide, we will be really grateful.
Thanks
Merin
September 28, 2012 at 10:27 PM
If you want to hide some field from the List or Library, you can use the property SPField.ShowInNewForm , SPField.ShowInEditForm, SPField.ShowInDisplayForm to hide the fields from the forms.
Step 1: Get the List or Content Type
Step 2: Get the Field from List or Content Type
Step 3: Set the value for the property
(SPField.ShowInNewForm , SPField.ShowInEditForm, SPField.ShowInDisplayForm) to show or hide the Field in respective forms.
Step 4:update the field.