Use the below code:
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.
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.
Where should we insert this code
ReplyDeleteThis is a server side code, so it can be executed from an exe as a one-time activity.
ReplyDeleteWhere you insert this code, is it in power shell ISE??
ReplyDeleteNo. As I mentioned it is from an exe. Create an exe and refer sharepoint.dll. Then you can execute this exe to perform the action. This post is more than a decade old. As far as I remember this option was available only with server side code at that time. You may check if this can be achieved with client side code or power shell code. That will be easier than server side code. Which version of SharePoint you have? Is it on-prem or Online?
ReplyDelete