Make field read-only for other than the author

I got this request from @f_Techno:

@SPJavaScripts hello Alexander, I have task list in sps 2010. I want to dim DueDate after create the task. No one should change it except me.

This snippet must be places in the EditForm of the list or library where you want the field to be readonly. What this code does is to check if the logged in user is the author of the list item, and if not, make the field “StartDate” readonly. You can change the fieldnames in the example to target other fields.

<script type="text/javascript" src="/Scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="/Scripts/spjs-utility/spjs-utility.js"></script>
<script type="text/javascript">
var thisItemDBdata = spjs_getItemByID({"listName":_spPageContextInfo.pageListId,"id":GetUrlKeyValue("ID"),"viewFields":["Author"]});
if(thisItemDBdata !== null){
	var authorID = thisItemDBdata.Author.split(";#")[0];
	if(_spPageContextInfo.userId.toString() !== authorID){
		var fields = init_fields_v2();
		$(fields["StartDate"]).find("td.ms-formbody").hide().after("<td id='readOnly_StartDate' class='ms-formbody'> </td>");
		$(document).ready(function(){
			// Build the fields object again for SP 2013
			fields = init_fields_v2();
			var sDate = getFieldValue("StartDate");
			$("#readOnly_StartDate").html(sDate);
		});
	}
}
</script>

You find spjs-utiliy.js here.

Please note that this code in for SP 2010 or SP 2013. If you want to use it for SP 2007 you must change “_spPageContextInfo.pageListId” for the list DisplayName or GUID.

For more advanced options, check out Dynamic Forms for SharePoint her.

Alexander

2 thoughts on “Make field read-only for other than the author”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.