Pages

Monday, February 10, 2014

Set Field Created through UI to readonly

SharePoint fields are excellent form of reusable asset. There are different types of fields that can be created in the SharePoint environment. As a developer we tend to create some fields as readonly which we could achieve easily while creating the solution using FieldXML in the element.xml file. But there are time when we had to create fields from the SharePoint UI which do not provide the flexibility of setting the field readonly. We would come across these scenarios when we want to have a field that will be used in Workflow or even some times we do not what to show the fields in Edit and Newform of the list or library. It would be an overkill if we create a solution for deploying a readonly field.

There is always poweshell who is our best friend who will never let us down in need. A small script would help achieve this. Below is the script that I wrote to set a list field to readonly.



Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Get the Web
$web = Get-SPWeb "http://sp2010"
#Get the List
$List = $Web.Lists["Daily Email"]
#Get the Field
$Field = $List.Fields["Email User"]
#Set the field to Read only
$Field.ReadOnlyField = $true
$Field.Update()

Note that I am using Display name of the field instead of internal name. Please feel free to extend this  script if you like. Also make sure you are running this script as Admin user.

I will share the next thing I experiment and until then Happy SharePointing.As always if you have any suggestion or question feel free to ask in the comment section so we all can learn together.

No comments:

Post a Comment