Controlling SPField Display

On occasion, you may encounter the requirement when creating fields programmatically that you want to hide the field display depending on what form type that ListFormWebPart is located on (i.e. whether you are in NewForm, EditForm, etc.). Through the SharePoint UI, you will find restricting such activity not available (or I haven’t found the way, however would be very interested in seeing such).

Before we get into the properties that will allow you to restrict the pages as such, let’s take a step back and examine some brief code.

Normally, we can modify (you could easily retrofit the following code into a state that would allow you to do this during field creation) SPField for an SPList object very easily by hydrating some fairly orthodox SharePoint objects and performing some functions. Let’s start out by declaring two readonly variables, one will hold the name of the web that we are going to open, and the other will name the list. Obviously, you can declare these at the inner scope if you wanted to just as embedded strings, however I like to break them out for better clarity in the code.

This is neither new nor earthshattering code (and you could optionally use a separate SPField object). However, let’s assume a new circumstance, whereby you wish to restrict the SPField’s that are displayed on the ListFormWebPart’s state pages to display only certain fields. Where could this be applicable? While I can only speak for myself, the properties that I am going to introduce you have been helpful to me when I really don’t find it necessary that my users have direct data entry rights into something, such as when the data that is being pushed into the list is coming from a secondary source such as a WebPart. Essentially, you can think of these types of fields as hidden fields.
Revisiting the code, we can simply use the following properties then on the field, and then update the field appropriately:

ShowInNewForm Represents a Boolean value that tells the field to display when new list items are created

ShowInDisplayForm Represents a Boolean value that tells the field to display when list items are displayed

ShowInEditForm Represents a Boolean value that tells the field to display when list items are being edited

Those are the three major ones that we are accustomed to. However, there are other display control properties that can be use if you wish:

ShowInListSettings Represents a Boolean value that tells the field to display when the list is becing customized (the list settings are being adjusted)

ShowInVersionHistory Represents a Boolean value that tells whether the field will be displayed when a user is viewing the version history of a list item

ShowinViewForms Represents a Boolean value that tells whether the field will be displayed when a user is on a page used to view list data

So, there are variety of properties that you can tap into. That being said, modifying the code to take advantage of such is fairly straightforward. For example, if you wanted to restrict the NewForm page from displaying the field, you can simply do: