When we need to make some fields as read only when users wants to edit the item.This thing we can do very easily by SharePoint Designer , open that edit form and create one custom form and edit the field in which we want to make as read only we can change displaymode as edit. But what if do not want to change by SharePoint Designer.
We have very easy way which named as “JavaScript”.
I need to make one field as read-only, here is sample code
Open the editform.aspx page for a list and add one “Content Editor Webpart”, add the below javascript code.
<script type="text/javascript">
function MakeReadOnly()
{
// find all the elements with tag Name as INPUT
var elements=document.body.getElementsByTagName("INPUT");
// loop through all the elements till we find an element with type text and title as name of our field
for (index=0; index < elements.length;++index)
{
if(elements[index].type=="text")
{
if(elements[index].title=="Address") //Field name
{
elements[index].readOnly=true;
}
}
}
}
_spBodyOnLoadFunctionNames.push("MakeReadOnly()");</script>
Hope this helps!!!
Disha Shah
HI Disha,
ReplyDeleteI have tried above your JS script for sharepoint webpart page. Its working great for text type custom fields. But tt doesn't work for system text fields.
Also, when field type changed to Number, Multitext, Date, Cost. It doesn't work for that.
Your help will be really appreciated. Thanks.
Hi Badal
DeleteThank you for appreciation! You can modify your javascript like below.
//this gets the field based on title identifier and tagname - a new function
function getTagFromIdentifierAndTitle(tagName, identifier, title) {
var len = identifier.length;
var tags = document.getElementsByTagName(tagName);
for (var i=0; i < tags.length; i++) {
var tempString = tags[i].id;
if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {
return tags[i];
}
}
return null;
}
Then write this line...
var control = getTagFromIdentifierAndTitle("input","TextField","FieldDisplayName");
control.readOnly=true;
Below table give you the idea what you have to mention for SharePoint Field Type
Single Line of Text- TextField- input
Multiple Lines of Text - TextField - input
Number - TextField - input
Currency - TextField - input
Choice (dropdown) -DropDownChoice - select
Lookup (single)* - Lookup - select
Lookup (multiple) - SelectCandidate; SelectResult - select
Yes/No - BooleanField - input
Hope this helps!
Thank You
Disha Shah
Thanks Disha for the prompt reponse. I have tried above function with the passing the multitext field Display and identifier in the function but unfortunately it is not working for me. Am I Missing something here? Please see below the details of function with passing parameter values:
Delete//this gets the field based on title identifier and tagname - a new function
function getTagFromIdentifierAndTitle(tagName, identifier, title) {
var len = identifier.length;
var tags = document.getElementsByTagName(tagName);
for (var i=0; i < tags.length; i++) {
var tempString = tags[i].id;
if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {
return tags[i];
}
}
return null;
}
var control = getTagFromIdentifierAndTitle("input","TextField","MultifieldColumnDisplayName");
control.readOnly=true;
Hi Badal
DeleteHere how your javscript code should look like inside script tag
//this gets the field based on title identifier and tagname - a new function
function getTagFromIdentifierAndTitle(tagName, identifier, title) {
var len = identifier.length;
var tags = document.getElementsByTagName(tagName);
for (var i=0; i < tags.length; i++) {
var tempString = tags[i].id;
if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {
return tags[i];
}
}
return null;
}
function MakeReadOnly()
{
var control = getTagFromIdentifierAndTitle("input","TextField","MultifieldColumnDisplayName");
control.readOnly=true;
}
_spBodyOnLoadFunctionNames.push("MakeReadOnly()");
Thank You
Disha Shah
There are many ways to make Read only fields in SharePoint:
ReplyDelete1. Using PowerShell to set SharePoint column read only.
2. Using jQuery/Javascript to make read only fields
3. Using SharePoint Designer to make SharePoint list field read only.
How to Make SharePoint List Column (Form Field) Read Only
what about readonly property in Newform
ReplyDeleteHi,
ReplyDeleteI have a Sharepoint List View with a couple of Lookup columns. I want to disable the hyperlinks in the view and make them read only. How can I achieve it?
Thanks