I am often asked about field level security in SharePoint. Well, SharePoint does not have field level security but it does have the ability to specify which forms a field will show up on. For example, we can have a field show up on the New form and all view forms but not on the edit form. This is by no means security, but it does allow for certain scenarios, like after a value is entered it cannot be changed. So, this post will show you how to change those settings on fields.
There are 2 different ways to set these properties. The first is through the list definition. When we create a list definition we can certain attributes on the Field element tag as follows.
- ShowInEditForm - Determines if this field shows up in the Edit form for a specific field.
- ShowInNewForm - Determines if this field show up in the New form for adding a new item to this list.
- ShowInVersionHistory - Determines if this field shows up in the version details of a list item.
- ShowInViewForms - Determines if this field shows up in the any View forms for a specific item.
This post is going to show you the second way, in code, to set these properties. Through the SharePoint API you can also set these properties of a specific field. These are optional value fields, meaning that they can be null. So the first time you look at these, they might have a null value in them.
The first thing we need to do is get a reference to the list that contains the field that we want to set these values on.
// Create the site that contains our list
SPSite oSite = new SPSite(strSiteUrl);
// Open the web object
SPWeb oWeb = spSite.OpenWeb();
// Get the list that contains the field we want to change the settings on
SPList oList = oWeb.Lists["Contacts"];
Now that we have our list, we need to get the field that we want to set the settings on.
// Get the field that we want to change the settings on
SPField oField = oList.Fields["Title"];
Now we can set the properties of our field according to our requirements. There are a couple more properties available in the API than there are in the List Definition schema.
// Set the field properties
oField.ShowInDisplayForm = true; // Determines in this field shows on the display item form.
oField.ShowInEditForm = false; // Determines if this field shows on the edit item form.
oField.ShowInListSettings = true; // Determines if this field shows on the list settings page.
oField.ShowInNewForm = true; // Determines if this field shows on the new item form.
oField.ShowInVersionHistory = true;// Determines if this field shows on the version history pages.
oField.ShowInViewForms = true; // Determines in this field shows on the all view forms pages.
Now all we have to do is call the update method on the field object to actually update the settings and save the changes back to the database.
// Update the field
oField.Update();
Continue reading if you want to see how to accomplish the same tasks in SWAT (SharePoint Work Acceleration Toolkit). You can find SWAT here http://www.idevfactory.com
Set Field Settings
In the Farm Tree window, rick click on the site that you want to set the web parts properties for and click Show Site Details. This will load all the details for that site.

In the Site Details section, select the Site Lists tab.

In the Site Lists tab, right click on the list that has the field that you want to set the properties on and click Show List Details.

On the List Details window, right click on the field that you want to set the properties for and click Show Field Settings.

On the Field Details window, select the specific settings that you want to change and click Update.

Enjoy!!!
Posted
10-04-2007 10:13 AM
by
ethan