16.10.2010 The function getFieldValue is posted in another – more updated – version here.
29.04.2010 Updated the code for the file “GetFieldValueOrSetAsReadonly.js” to support “SPFieldUserMulti” and for getting the field value in DispForm.
I have had several requests for a solution for setting fields as read only in EditForm. I have created this script to achieve this.
This script enables you to get the field value for any SharePoint field type in EditForm, and to set the field as read only by grabbing it’s value, hiding the <TD>, and adding a clean <TD> with the value in it’s place.
As always we start like this:
Create a document library to hold your scripts (or a folder on the root created in SharePoint Designer). In this example i have made a document library with a relative URL of “/test/English/Javascript” (a sub site named “test” with a sub site named “English” with a document library named “Javascript”):

The jQuery-library is found here. The pictures and the sourcecode refers to jquery-1.4.2.min. If you use another version, please update the reference in the code.
Read here how to add a CEWP to EditForm, and how to get the FieldInternalNames for your fields.
Add this code to a CEWP below the EditForm:
<script type="text/javascript" src="/test/English/Javascript/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="/test/English/Javascript/GetFieldValueOrSetAsReadonly.js"></script>
<script type="text/javascript">
// Array of FieldInternalNames to make readonly
var arrToMakeReadOnly = ['Title','MySelect','MyPeoplePicker','Lookup','Bool','Hyperlink','DateTime','Multiline'];
// The function call must be wrapped in the "$(document).ready" to work with complex field types
$(document).ready(function(){
readOnlyFieldArray(arrToMakeReadOnly);
});
</script>
The above code sets all fields in the array “arrToMakeReadOnly” as readonly. To simply get a fields value, do it like this:
<script type="text/javascript" src="/test/English/Javascript/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="/test/English/Javascript/GetFieldValueOrSetAsReadonly.js"></script>
<script type="text/javascript">
// The function call must be wrapped in the "$(document).ready" to work with complex field types
$(document).ready(function(){
var myValue = getFieldValue("Insert the FieldInternalName of your field here");
});
</script>
To get a fields value in DispForm, do it like this:
<script type="text/javascript" src="/test/English/Javascript/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="/test/English/Javascript/GetFieldValueOrSetAsReadonly.js"></script>
<script type="text/javascript">
var myValue = getFieldValue("Insert the FieldInternalName of your field here","disp");
</script>
The sourcecode for the file “GetFieldValueOrSetAsReadonly.js”:
/* Get field value for all field types, or set as readonly in EditForm
* ---------------------------------------------
* Created by Alexander Bautz
* alexander.bautz@gmail.com
* http://sharepointjavascript.wordpress.com
* v1.1
* LastMod: 29.04.2010
* LastChange: Supporting "SPFieldUserMulti" and for getting the field value in DispForm
* ---------------------------------------------
* Include reference to jquery - http://jquery.com
* ---------------------------------------------
* Call from a CEWP below the list form in EditForm like this:
<script type="text/javascript" src="/test/English/Javascript/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="/test/English/Javascript/readOnlyForSharepointFields.js"></script>
<script type="text/javascript">
// Array of FieldInternalNames to make readonly
var arrToMakeReadOnly = ['Title','MySelect','MyPeoplePicker','Lookup','Bool','Hyperlink','DateTime','Multiline'];
// The function call must be wrapped in the "$(document).ready" to work with complex field types
$(document).ready(function(){
redOnlyField(arrToMakeReadOnly);
});
</script>
*/
function readOnlyFieldArray(arrayOfFieldInternalNames){
$.each(arrayOfFieldInternalNames,function(i,fin){
var fieldValue = getFieldValue(fin);
if(!fieldValue)return false;
thisField=$(fields[fin]);
// Strip off any "formvalidation star"
thisField.find('.ms-formlabel span.ms-formvalidation').remove();
// Set as "readOnly"
thisField.find('td.ms-formbody').hide().after("<td class='ms-formbody' style='width:450px'>"+fieldValue+"</td>");
});
}
function getFieldValue(fin,edit_OR_disp){
// If not already done - init all fields
if(typeof(fields)=='undefined')fields = init_fields();
// Return if FieldInternalName is not found
if(fields[fin]==undefined)return;
var thisField = $(fields[fin]);
// If "edit_OR_disp" is undefined, default to "edit"
if(edit_OR_disp==undefined)edit_OR_disp='edit';
if(edit_OR_disp=='disp'){ // If "disp"
var valRaw = $(fields[fin]).find('.ms-formbody').text();
return (valRaw.replace(/[ \xA0]+$/,'')=='')?'':valRaw.replace(/[ \xA0]+$/,'');
}else{ // If "edit"
var fieldType = $(fields[fin]).attr('FieldType');
if(fieldType==undefined){
alert("The attribute \"FieldType\" is missing.\nEnsure the function init_fields() used is the one included in the file \"GetFieldValueOrSetAsReadonly.js\".");
return false;
}
returnVal = '';
switch(fieldType){
case 'SPFieldText':
case 'SPFieldNumber':
case 'SPFieldCurrency':
returnVal = thisField.find('input').val();
break;
case 'SPFieldChoice':
if(thisField.find('input:radio').length>0){
returnVal = thisField.find('input:radio:checked').next().text();
}else{
returnVal = thisField.find('select').val();
}
break;
case 'SPFieldMultiChoice':
var multiChoice = [];
thisField.find('input:checkbox:checked').each(function(i,opt){
opt = $(opt);
multiChoice.push(opt.next().text());
});
returnVal = multiChoice.join('<br />');
break;
case 'SPFieldUser':
var myPeoplePicker = thisField.find("div[id='divEntityData']");
returnVal = myPeoplePicker.attr('displaytext');
returnVal = (returnVal!=undefined)?returnVal:'';
break;
case 'SPFieldUserMulti':
var userMulti = [];
thisField.find("div[id='divEntityData']").each(function(i,div){
thisVal = $(div).attr('displaytext');
if(thisVal!=undefined){
userMulti.push(thisVal);
}
});
returnVal = userMulti.join('<br />');
break;
case 'SPFieldLookup':
if(thisField.find('select').length>0){
returnVal = thisField.find('select option:selected').text();
}else{
returnVal = thisField.find('input').val();
}
break;
case 'SPFieldLookupMulti':
var lookupMulti = [];
thisField.find("select:last option").each(function(i,opt){
opt = $(opt);
lookupMulti.push(opt.text());
});
returnVal = lookupMulti.join('<br />');
break;
case 'SPFieldBoolean':
returnVal = (thisField.find('input').attr('checked')==true)?true:false;
break;
case 'SPFieldURL':
var link = thisField.find('input:first').val();
var descr = thisField.find('input:last').val();
returnVal = "<a href='"+link+"'>"+descr+"</a>";
break;
case 'SPFieldDateTime':
var date = thisField.find('input:first').val();
var hour = thisField.find('select:first option:selected').val()
hour = (hour==null)?'':hour.match(/^[\d]+/)+":";
var AMPM = thisField.find('select:first option:selected').val()
AMPM = (AMPM==null)?'':AMPM.match(/AM|PM/);
var minutes = thisField.find('select:last option:selected').val();
minutes = (minutes==null)?'':minutes;
returnVal = date+" "+hour+minutes+" "+AMPM;
break;
case 'SPFieldNote':
returnVal = thisField.find('textarea:first').val();
break;
case 'customHeading':
returnVal = '';
break;
default:
returnVal = "Unknown fieldType: <strong>"+fieldType+"</strong>, please check the script.";
}
if(returnVal==='')returnVal=" ";
return returnVal;
}
}
function init_fields(){
var res = {};
$("td.ms-formbody").each(function(){
var myMatch = $(this).html().match(/FieldName="(.+)"\s+FieldInternalName="(.+)"\s+FieldType="(.+)"\s+/);
if(myMatch!=null){
// Display name
var disp = myMatch[1];
// FieldInternalName
var fin = myMatch[2];
// FieldType
var type = myMatch[3];
if(type=='SPFieldNote'){
if($(this).find('script').length>0){
type=type+"_HTML";
}
}
if(type=='SPFieldLookup'){
if($(this).find('input').length>0){
type=type+"_Input";
}
}
// Build object
res[fin] = this.parentNode;
$(res[fin]).attr('FieldDispName',disp);
$(res[fin]).attr('FieldType',type);
}
});
return res;
}
Save as “GetFieldValueOrSetAsReadonly.js”, mind the file extension, and upload to the scriptlibrary as shown above.
The function init_fields() used in this code is the new one posted here: Revised function init_fields()
It has additional functionality not found in previous versions. Be sure to update the function if adding this to existing code.
Ask if anything is unclear
Regards
Alexander
August 19, 2011 at 1:03 pm |
Why doesn’t it work with Mozilla Firefox??
It alerts ‘The attribute “FieldType” is missing.’
August 19, 2011 at 1:12 pm
Hi,
I have updated the function init_fields() as Firefox has made some changes in newer versions.
Alexander
August 19, 2011 at 1:16 pm
Thanks for the quick response!
However, I’ve put the updated version of init_fields() and still doesn’t work.
Is there a workaround??
Would be very grateful if there is one..:)
August 19, 2011 at 1:24 pm
Hi,
Do you have multiple instances of the function in the page? – it has been used in many of my solutions.
If not, which version of Firefox do you use?
Alexander
August 19, 2011 at 1:37 pm
Alexander,
By putting an alert in init_fields() it seems it runs only once.
I’ve tested in Firefox 5.0.1 and 3.6.2
Many thanks again for the help!
August 19, 2011 at 1:47 pm
Hi,
This is strange. I have used this in Firefox for a long time and only recently (after v5 i think) needed to update the function init_fields.
Is the form you are using a standard SharePoint form, or has it been modified in SharePoint Designer?
Alexander
August 19, 2011 at 2:29 pm
sorry alexander,
the comment landed out of this thread..
August 19, 2011 at 2:06 pm |
The only modification I did was adding the first piece of code to the EditForm.aspx under
I didn’t add any webpart.
August 19, 2011 at 2:37 pm
Have you put the code below the form webpart?
If it is placed above it will not work.
Alexander
August 19, 2011 at 2:53 pm
I’ve added finally a CEWB below my editForm webpart.
Where should I put the code?
In source editor of the CEWB??
August 19, 2011 at 3:50 pm
If in SP2007, yes, but even better (and a must in SP2010) is to use the content link property. You then put the file in a document library or a folder created in SharePoint Designer and link to it in the “Content link” field of the CEWP
Alexander
August 19, 2011 at 4:32 pm
Thanks for the help,Alexander but as it seems I can’t make it work..
I’ve put the code in a js file in a folder I created in SPD and linked it through Content link property of the CEWP.
But again in IE its ok in Firefox it doesn’t work..
August 19, 2011 at 4:53 pm
Which operating system do you use for Firefox?
Try to step trough the init_fields function and see if you can find anything. Try alerting “type” and see if it gets it at all.
Alexander
October 19, 2011 at 9:58 pm |
Hi, when using your code to hide a rich text field it does not make it readonly but display message: “Unknown fieldType: SPFieldNote_HTML, please check the script.” I like to know if you can show me how to make a rich text field read only.
October 28, 2011 at 9:15 pm
Hi,
Follow the link in the top of the article to find an updated version of the function “getFieldValue”.
Alexander
February 9, 2012 at 2:08 am |
Trying this method currently on a lookup 20+ items, getting “Unknown fieldType: SPFieldLookup_Input, please check the script.” any advice?
February 9, 2012 at 10:33 am
Read the note in the top of the article – these scripts have been updated.
Alexander
February 10, 2012 at 8:56 am
Alexander,
Thank you for the prompt response. Being a complete noob, I really did review the updated version pages and comments. but i posted on the older page.
I am still unclear how to apply readonly with either function getfieldvalie or setfieldvalie. I was using this:
$(document).ready(function(){
readOnlyFieldArray(arrToMakeReadOnly);
}
until the a field in my array changed from under 20 to 20+ and became a complex lookup field.
Could you please provide me with a basic example of the script to set a complex lookup field as readlonly with the updated functions? (or direct me to where I may have overlooked it.)
February 19, 2012 at 5:41 pm
Hi,
Sorry for the late reply.
Get the latest version of spjs-utility.js here. Use this code in a CEWP in the form (below the form web part) where you want to set the field as read only:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> <script type="text/javascript" src="/test/English/Javascript/spjs_utility - blog/spjs-utility.js"></script> <script type="text/javascript"> var fields = init_fields_v2(); doReadOnly(['MyLookupField']); function doReadOnly(array){ var fieldValue, thisField; $.each(array,function(i,fin){ thisField = $(fields[fin]); fieldValue = getFieldValue(fin); thisField.find('td.ms-formbody').hide().after("<td class='ms-formbody' style='width:450px'>"+fieldValue+" </td>"); }); } </script>Alexander
February 21, 2012 at 12:07 am
Thank you Alexander,
I actually got this to function based on your comments to Maureen (below) and failed to update my inquiry. I removed the declaration for GetFieldValueOrSetAsReadOnly.js, and then simply pulled the readOnlyFieldArray function fromm there into my script. Didnt realize at the time there was a duplication of the older GetFieldValue function causing the issue. Works perfectly! Thanks a million!
February 12, 2012 at 8:34 pm |
This works great except for a lookup field that has values with spaces (i.e Mobility Support, Mobility Development, etc.) then when I try to disable the field that contains one of those values iget the message “Unknown fielType: SPFieldLookup_Input, please checkthe script. lookup fields that have values that do not contain spaces work fine. I am trying to use this in a demo I am giving tomorrow (Monday 2/13) so if you are on line and can help me with a solution I would greatly appreciate it. By the way, great documentation and easy to follow.
February 12, 2012 at 8:40 pm
Hi,
You must follow the link in the top of the article and get an updated version of the function “getFieldValue”.
Alexander
February 12, 2012 at 11:36 pm |
I am sorry, I am new to all of this and am not understanding. Rieght now in my CEWP I have this code:
// Array of FieldInternalNames to make readonly
//Using Internal field names for Activity and Team Member
var arrToMakeReadOnly = ['Acitvity','WorkItem','_x0074_m2'];
// The function call must be wrapped in the “$(document).ready” to work with complex field types
$(document).ready(function(){
readOnlyFieldArray(arrToMakeReadOnly);
});
when I go and look at the updated code am I just supposed to include it in this code or replace this code altogether with below:
fields = init_fields_v2();
// Get the loginName from a people picker
var myPickerVal = getFieldValue(‘MyPeoplePicker’,”,’; ‘,’loginName’);
alert(myPickerVal);
// Get value from a multiline textfield
var myRichTextValue = getFieldValue(‘MultiLineRich’);
alert(myRichTextValue);
February 12, 2012 at 11:43 pm |
oops I think I understand now…you are replacing the GetFieldValueOrSetAsReadOnly.js file with the spjs-utility.js file correct?
February 12, 2012 at 11:54 pm |
OK that did not work so I am not understanding….I feel stupid and hate bothering you but do nto understand what is going on. now even one lookup that was getting disabled is not.. I am sorry to be a bother but need to understand
February 13, 2012 at 9:46 am
In the file spjs-utility.js you find an updated version of the function “getFieldValue”. You could either get the updated version and put it in the file “GetFieldValueOrSetAsReadOnly.js”, or move the function “readOnlyFieldArray” into your CEWP and refer “spjs-utility.js” in stead of “GetFieldValueOrSetAsReadOnly.js”.
Alexander