BitterCoder's Wiki

Edit

Determining Visible fields

This describes a simple way to find the fields which should be displayed for new or edit mode by default.

Edit

The Code

First off a simple function to determine if a field is hidden (we hide attachments as well, because the custom new/edit form web part deals with them specifically)


private static bool IsHidden(SPField field)
{
    return (field.Hidden || field.ReadOnlyField || field.Type == SPFieldType.Attachments);
}



Second a loop through all the fields, determining which are visible for the Edit or New modes - notice the odd logic, if ShowInNewForm/ShowInEditForm is null, then we just treat it as "true".


foreach (SPField field in list.Fields)
{
    if (!IsHidden(field))
    {
        if (!field.ShowInNewForm.HasValue || field.ShowInNewForm.Value)
        {
            // do something
        }
        if (!field.ShowInEditForm.HasValue || field.ShowInEditForm.Value)
        {
            // do something
        }
    }
}



online essay services

ScrewTurn Wiki version 2.0.2. Some of the icons created by FamFamFam.