This SharePoint tutorial, we will discuss how to make a field or a column of a SharePoint list as ReadOnly. You might encounter a situation where you need to make a column as read-only when the user trying to submit an item or edit an item in SharePoint 2013/2016 or SharePoint Online.
There are different ways you can make a SharePoint list column as read-only.
SharePoint Tutorial Contents
To use JavaScript code to make the SharePoint list field as read-only, just edit the page and then put the below code inside a Script editor webpart.
<script type="text/javascript">
function SetReadOnlycolumn()
{
var elements=document.getElementById(‘Your column name in the view source’);
elements.readOnly=true;
}
_spBodyOnLoadFunctionNames.push("SetReadOnlycolumn()");
</script>
SPUtility.js is a JavaScript library that you can download from GitHub and use it in your SharePoint 2016/2013/2010 or MOSS 2007 site. This also works in SharePoint Online. By using this SharePoint library we can modify list forms like NewForm.aspx and EditForm.aspx.
Edit your page and put the below code inside a script editor web part. Make sure to give reference to sputility.min.js which we have downloaded from GitHub.
<script type="text/javascript">
function MakeFieldReadOnly()
{
SPUtility.GetSPField(‘Name of your List Field’).MakeReadOnly();
}
_spBodyOnLoadFunctionNames.push("MakeFieldReadOnly");
</script>
We can hide a SharePoint list column by using the Server-side object model code in SharePoint 2013/2016. Below is the code sample to make the SharePoint list field read-only.
SPList list = web.Lists["List Name"];
SPField myField = list.Fields["Name of your List Field"];
myField.ReadOnlyField = true;
myField.Update();
You may like following SharePoint tutorials:
- How to use PreSaveAction() in newform.aspx in SharePoint Online/2013/2016 List
- Different ways to show/hide SharePoint List form fields
- SharePoint modern list column formatting examples
- Hide SharePoint list columns based on user permissions
- Color Code SharePoint List Column Value based on Condition using Calculated Column
- How to hide or rename title column from SharePoint list or library
- SharePoint 2013 list column status wise Change row Color using jQuery
- SharePoint 2013/2016/Online List Column Validation Examples
- Change column ordering in list forms in SharePoint Online/2013/2016
This SharePoint tutorial, we learned various ways to make a column or field read-only in the SharePoint list using JavaScript, SPUtility.js and using the SharePoint server object model.
Bhawana Rathore is a Microsoft MVP (3 times in Office Apps & Services) and a passionate SharePoint Consultant, having around 10 years of IT experience in the industry, as well as in .Net technologies. She likes to share her technical expertise in EnjoySharePoint.com and SPGuides.com