Skip to main content

Posts

Showing posts from February, 2010

How to make a List column read-only

Use the below code:

using (SPSite mySite = new SPSite("siteURL"))
{
using (SPWeb myWeb = mySite.OpenWeb())
{
SPList myList = myWeb.Lists["ListName"];
SPField myField = myList.Fields["ColName"];
myField.ReadOnlyField = true;
myField.Update();
}
}

With this code, the column becomes read-only. This column does not appear while adding new item or editing existing item, and also in the list of columns in List settings. But, you can add this column to a view, so, user can see the data. Also, programmatically, you can add/update a value to this column (That means, it is set to read-only from SharePoint UI point of view)

One use of this approach is when you want to introduce auto-generated number column.