Category Archives: User information

Showing or hiding list fields based on membership in a SharePoint group

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”):
IMG

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:
[javascript]
/* 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;
}
[/javascript]

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:
IMG

CEWP sourcecode:
[javascript]
<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>
[/javascript]

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:
IMG

For all others the NewForm looks like this:
IMG

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

Accessing user profile information in WSS 3.0 with javascript

18.09.2011 I have posted a new solution which makes this one obsolete. You find the new one here


09.09.2010 Updated the function “getUserInfo”. It no longer requires the list GUID for the user list to be specified in the script.

31.10.2009: Small update for default picture in the example CEWP.

This article describes how to access the “user profile” under WSS 3.0 via javascript. The method used is a query trough the webservice lists.asmx.

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.

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”):
IMG

You can call this script from any page. In this example i will place it on Default.aspx.

The sourcecode for the “AccessUserProfileInWSS.js” looks like this:
[javascript]
/* getUserInfo – Returns "user info" data from user list in WSS 3.0 (not MOSS user profile)
* ———————————————
* Created by Alexander Bautz
* alexander.bautz@gmail.com
* https://spjsblog.com
* LastMod: 20.09.2009
* ———————————————

Refer these scripts:
interaction.js // Erucy – http://spjslib.codeplex.com
stringBuffer.js // Erucy – http://spjslib.codeplex.com
jQuery // http://jQuery.com

Use:
var ui = getUserInfo(); // If UserId is not specified it assumes it’s logged in user (_spUserId)
alert(ui.Title);
*/

function getUserInfo(UserId){
wsBaseUrl = ‘/_vti_bin/’;
var uiObj = {};
if(typeof(UserId)=="undefined" || UserId==”)UserId = _spUserId;
var arrOfFields = [‘ID’, ‘Name’, ‘Title’, ‘EMail’, ‘Department’, ‘JobTitle’, ‘Notes’, ‘Picture’,
‘IsSiteAdmin’, ‘Created’, ‘Author’, ‘Modified’, ‘Editor’, ‘SipAddress’, ‘Deleted’];
var item = getItemById(‘UserInfo’,UserId,arrOfFields);
if(item != null){
for(i=0;i<arrOfFields.length;i++){
if(item[arrOfFields[i]]!=null){
uiObj[arrOfFields[i]] = item[arrOfFields[i]];
}else{
uiObj[arrOfFields[i]] = ”;
}
}
return uiObj;
}else{
for(i=0;i<arrOfFields.length;i++){
uiObj[arrOfFields[i]] = "User with id " + UserId + " not found.";
}
return uiObj;
}
}
[/javascript]

Save this as a text file and rename to “AccessUserProfileInWSS.js”, then upload to the library as shown above.

You call the script from a standard CEWP like this:
IMG

Sourcecode for CEWP:
[javascript]
<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">

var ui = getUserInfo(); // If UserId is not specified it assumes it’s logged in user (_spUserId)
picSrc = ‘/_layouts/images/person.gif’;
if(ui.Picture!=”){
picSrc = ui.Picture.split(‘, ‘)[0]
}
var str = ”;
str += "Accountname: " + ui.Name + "<br>";
str += "Full name: " + ui.Title + "<br>";
str += "E-mail: " + ui.EMail + "<br>";
str += "Department: " + ui.Department + "<br>";
str += "<img alt=” src=’" + picSrc + "’ />";

document.write(str);
</script>
[/javascript]

And you get a result like this:
IMG

I will follow up this article with an article describing how to hide or show form fields based on membership – or not membership – in a SharePoint group.

Alexander