This solution will let you control the visibility of individual fields in NewForm, DispForm and EditForm based on membership in a specific SharePoint group.
This is an updated version of this post.
I have updated it to support newer versions of jQuery, and to use spjs-utility.js.
If you plan to hide empty fields in NewForm or EditForm, these fields cannot be required to contains information as the user would not be able to save the form – and as the field is invisible, the user will not see the empty field validation message.
- Download the latest version of spjs-utility.js, and save it locally. You can put it in a folder created using SharePoint designer, or in a shared document library where all users have read permission.
- If you prefer to use a local version of jQuery, get it here and save it alongside the file above.
- Add the below code to a CEWP or a HTML form web part, and update the script src to reflect your local copy of spjs-utility.js and jQuery.
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script type="text/javascript" src="https://files.spjsworks.com/files/spjs-utility/07.03.2012/spjs-utility.js"></script> <script type="text/javascript"> var fields = init_fields_v2(); // The group ID var groupId = 146; // This number must be changed to match your local group ID // Show this array for members in the above group var arrToShowForMembersInTheGroup = ['Title','ForOwnersOnly']; // And this array for everybody else var arrToShowForEverybodyElse = ['Title']; var isInGroup = isCurrentUserInGroup(groupId,false); if(isInGroup){ hideAllButThese(arrToShowForMembersInTheGroup); }else{ hideAllButThese(arrToShowForEverybodyElse); } /************************************ Do not edit below this line ************************************/ function isCurrentUserInGroup(groupId,returnGroupName){ var ui, userLoginName, ug, result; ui = getUserInfo_v2(_spUserId); userLoginName = ui['Name']; ug = getGroupCollectionFromUser(userLoginName); result = false; $.each(ug,function(i,obj){ if(obj.groupId===groupId){ if(returnGroupName){ result = obj.groupName; }else{ result = true; } return false; } }); return result; } function getGroupCollectionFromUser(userLoginName,userListBaseUrl){ if(userListBaseUrl===undefined){ userListBaseUrl = L_Menu_BaseUrl; } var result = []; spjs_wrapSoapRequest(userListBaseUrl + '/_vti_bin/usergroup.asmx', 'http://schemas.microsoft.com/sharepoint/soap/directory/GetGroupCollectionFromUser', '<GetGroupCollectionFromUser xmlns="http://schemas.microsoft.com/sharepoint/soap/directory/"><userLoginName>' + userLoginName + '</userLoginName></GetGroupCollectionFromUser>', function(data){ $('Group', data).each(function(idx, itemData){ result.push({"groupId":parseInt($(itemData).attr('ID'),10),"groupName":$(itemData).attr('Name')}); }); }); return result; } function hideAllButThese(arrToShow){ $.each(fields,function(fin){ if($.inArray(fin,arrToShow)===-1){ $(fields[fin]).hide(); }else{ $(fields[fin]).show(); } }); } </script>
Find the group ID of the group you want to use by going to Site Settings > People and Groups, select your group and look at the URL:
http://www.contoso.com/_layouts/people.aspx?MembershipGroupId=6
Change the FieldInternalNames in the array “arrToShowForMembersInTheGroup” and “arrToShowForEverybodyElse” to match your setup.
Look here to learn how to manually retrieve FieldInternalNames, or here to find a solution that shows you all in one go.
If you have trouble, post a comment below.
Alexander