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>
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
Hi, Can I use this or something similar to hide field based on which SharePoint group logged in user is.
Thanks!
Hi,
Use this solution: https://spjsblog.com/2012/05/07/showing-or-hiding-form-fields-based-on-membership-in-a-sharepoint-group/
Alexander