Wednesday, July 10, 2013

How to Make SharePoint List Column (Form Field) Read Only

How to make a column read only in SharePoint 2010? Well, There are many ways. Here are some:

  1. Make column read only in SharePoint list programmatically
  2. use jQuery to make SharePoint field read only
  3. Using SharePoint designer to set field read only in list forms.

Programmatically Set SharePoint list field to Read-Only using C# object model code or PowerShell script: 

To make a SharePoint field read-only, We can set the " ReadOnlyField" property of SPField to "True" using either PowerShell script or C# code. Here is how: 
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Get the Web
$web = Get-SPWeb "http://sharepoint.crescent.com/sites/pmo"
#Get the List
$List = $Web.Lists["Project Metrics"]
#Get the Field 
$Field = $List.Fields["Project ID"]
#Set the field to Read only
$Field.ReadOnlyField = $true
$Field.Update()
See the detailed implementation at: Hide Columns in SharePoint List NewForm, EditForm and DispForms.

But the problem is: As soon as you set "ReadOnlyField" to true, field will be hidden from all list forms and list settings. Instead, make the "ShowInNewForm" and "ShowInEditForm" Properties to False to hide them from NewForm.aspx and EditForm.aspx but still make it visible on list settings and DispForm.aspx.

Alternatively, you can create a Field with ReadOnly Property and add the field wherever required.
$SPSite = Get-SPSite "http://sharepoint.crescent.com"
$SPWeb = $SPSite.RootWeb
$SPcolumn = '<Field Type="Number" DisplayName="Project ID" Required="FALSE" 
               ReadOnly="TRUE" ShowInDisplayForm="TRUE"ShowInViewForms="TRUE" EnforceUniqueValues="FALSE" 
                  Indexed="FALSE" Min="1" Max="10000" Group="Crescent PMO Site Columns" 
                    ID="{b81c7da6-1317-46fa-a32b-9f446c30b6e9}" StaticName="ProjectID" Name="ProjectID"></Field>'
$SPWeb.Fields.AddFieldAsXml($SPcolumn)
See more at: Create Site Column Feature for SharePoint 2010

However, these methods not satisfying our requirement. What we want is the field to be present in all SharePoint list forms with "Read-only" mode! So, lets see help from jQuery solution.

Make a SharePoint List form field read only with jQuery 

To make a column read only, Place the below code in a text file, upload to any SharePoint library, Add a CEWP to the "NewForm.Aspx" and/or "EditForm.aspx" or wherever required. Specify this script as "Content Link" from content editor web part properties pane.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"
       type="text/javascript"></script>

<script type="text/javascript">

$(document).ready(function()
{
//Set the Field to Read only and change its background colour
$("input[title='Metrics ID']").attr("readonly","true").css('background-color','#F6F6F6');
//You can disable the field also:  $("input[Title='Metrics ID']").attr("disabled", "disabled");
});

</script>
Output goes like this: SharePoint 2010 read only field in edit form
sharepoint make field readonly in editform
To disable a Choice field (Drop Down) use:
$("Select[Title='Department']“).attr("disabled", "disabled");

To Disable Multiline Plain text fields
$("input[Title='Description']").attr("disabled", "disabled");

How to make a field read only in SharePoint Designer:

To make a SharePoint list column read only in EditForm.aspx, follow these steps:
  1. Open the SharePoint site in SharePoint Designer
  2. Navigate to the List. Under the "Forms" section, click on "New"
    how to make a field read only in sharepoint designer
  3. Create a new EditForm by giving it a name, and choosing other properties in the below screen.
    sharepoint designer 2010 read only field
  4. Edit the newly created Edit form in SharePoint designer, Click on the target property to select
  5. Change the "DisplayMode" property from "Edit" to "Display" in the properties pane.
    sharepoint designer 2010 make field read only
  6. Save and close.
Output: Read only field in SharePoint list form using SharePoint Designer
sharepoint designer 2010 read only column
Years back, there was a similar requirement for SharePoint 2007 and I used JavaScript: Make Read-Only fields in SharePoint List Forms using Javascript



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Five Challenges in SharePoint Security
...And How to Solve Them. Free White Paper
*Sponsored


Check out these SharePoint products:

8 comments :

  1. Hello,

    i used the jquery solution and it worked fine. How must be the code if i wanna set more than one column as read only?
    Hope you can help me
    Thanks in advance
    Tj

    ReplyDelete
  2. Hi,

    that's a great solution. Is it possible to make look-up columns and people picker as read only. This are the only types of columns where the code doesn't work.

    Thx for your help

    ReplyDelete
  3. I used your SharePoint Designer solution and it worked perfectly. THANK YOU!

    ReplyDelete
  4. Using Jquery it works..but its still possible to alter the text by enabling the field from read only to editable using developer toolbar and then the field value can be changed. Do you have any idea how to prevent it?

    ReplyDelete
    Replies
    1. True Sager,

      jQuery/JavaScript methods are weak! Use either PowerShell/C# methods to set field Properties to read-only.

      Delete
  5. I need to disable a text box based on the dropdown selected. Can u help me in this??

    ReplyDelete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...