05.07.2012 An updated version can be found here
This article describes how to show or hide a form field based upon membership or not membership in a SharePoint group.
This solution uses the script created in this article to access the user info on the current user. It is a precondition that you read the previous article before continuing with this one.
As always we begin 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 subsite named “test” with a subsite named “English” with a document library named “Javascript”):
I use some code (”interaction.js” and stringBuffer.js”) created by Erucy and published on codeplex.
The jQuery-library is found here. The pictures and the sourcecode refers to jquery-1.3.2.min. If you download another version, be sure to update the script reference in the sourcecode.
The file “AccessUserProfileInWSS.js” is created in the previous article.
The sourcecode for the IsUserInGroup.js” looks like this:
/* isUserInGroup - Used to check if a user is in a spesific SharePoint-group * --------------------------------------------- * Created by Alexander Bautz * alexander.bautz@gmail.com * https://spjsblog.com * LastMod: 20.09.2009 * --------------------------------------------- Use: * userId : Only supplied if the user to query is not the logged in user. * groupId : ID of the group to check membership in * returnGroupName (true) : Returns the groupName of the group if the user is in it - returns false if user is not in the group. * returnGroupName (false) : Returns "true" or "false" * * Refer these scripts: * interaction.js // Erucy - http://spjslib.codeplex.com * stringBuffer.js // Erucy - http://spjslib.codeplex.com * jQuery // http://jQuery.com * AccessUserProfileInWSS.js // https://spjsblog.com/2009/09/20/accessing-user-profile-information-in-wss-3-0-with-javascript/ */ function isUserInGroup(UserId,groupId,returnGroupName){ if(UserId=='')UserId = _spUserId; var ui = getUserInfo(UserId); var userLoginName = ui['Name']; var ug = getGroupCollectionFromUser(userLoginName); for(i=0;i<ug.length;i++){ var id = ug[i].split('|'); if(id[0]==groupId){ if(returnGroupName){ return id[1]; }else{ return true; } } } return false; } function getGroupCollectionFromUser(userLoginName){ var result = []; innerPost(wsBaseUrl + '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($(itemData).attr('ID') + "|" + $(itemData).attr('Name')); }); }); return result; } function init_fields(){ // Modified version og the function created by Erucy - http://spjslib.codeplex.com/ var res = {}; $("td.ms-formbody").each(function(){ if($(this).html().indexOf('FieldInternalName="')<0) return; var start = $(this).html().indexOf('FieldInternalName="')+19; var stopp = $(this).html().indexOf('FieldType="')-7; var nm = $(this).html().substring(start,stopp); res[nm] = this.parentNode; }); return res; }
Save this as a text file and rename to “IsUserInGroup.js”, then upload to the library as shown above.
Then you add a CEWP below the list form in NewForm (and if you like – DispForm and EditForm) with this sourceCode:
CEWP sourcecode:
<script type="text/javascript" src="/test/English/Javascript/jquery-1.3.2.min.js"></script> <script type="text/javascript" src="/test/English/Javascript/interaction.js"></script> <script type="text/javascript" src="/test/English/Javascript/stringBuffer.js"></script> <script type="text/javascript" src="/test/English/Javascript/AccessUserProfileInWSS.js"></script> <script type="text/javascript" src="/test/English/Javascript/IsUserInGroup.js"></script> <script type="text/javascript"> fields = init_fields(); var arrToShowOnlyForOwnerGroup = ['OnlyForOwnerGroup1','OnlyForOwnerGroup2']; // FieldInternalNames var isInGroup = isUserInGroup('',38,false) if(!isInGroup){ for(i=0;i<arrToShowOnlyForOwnerGroup.length;i++){ $(fields[arrToShowOnlyForOwnerGroup[i]]).hide(); } } </script>
The variable “arrToShowOnlyForOwnerGroup” is an array of “FieldInternalNames” of the fields to hide for “non owners”. Look here for a quick guide for obtaining the “FieldInternalName” of your fields.
“38” is the ID of the group “Owners”. You find your group ID by looking at the URL under Site Actions > Site settings > People and Groups > your group – look at the URL:
/_layouts/people.aspx?MembershipGroupId=38.
For group members the NewForm looks like this:
For all others the NewForm looks like this:
Just remember not to set the field as required from SharePoint UI – if the user cannot see the field he can not fill it! To learn how to add dynamic required fields – se fieldutility.js from Erucy on codeplex.
Alexander