Good day,
I have a simple dropdown field with some choice in it (A,B,C)
On open form a would like to filter this dropdown on depending user membership.(sharepoint group or active directory security group)
For example if user01 open form and belong to group IT - show only A,B,C in dropdown
If user02 belong to group Admins - show in dropdown - B
For rest users show A,B
Hello @ixxxl,
You can check if a user is a member of a certain group using PnPjs and then set the values of the dropdown field using the code:
pnp.sp.web.siteUsers.getById(_spPageContextInfo.userId).groups.get()
.then(function(groupsData){
//var groups = [];
//Check if the user is a member of the desired groups and add them to the array
var vb=false;
for (var i = 0; i < groupsData.length; i++) {
//s+=groupsData[i].Title+"/n";
if(groupsData[i].Title == 'Site Owners') {
var datasource = [ 'A', 'B']
}
else if(groupsData[i].Title == 'Site Members'){
var datasource = [ 'A', 'C']
}
}
if(datasource){
fd.field('Choice').widget.setDataSource(datasource);
fd.field('Choice').widget.setDataSource(datasource);
}
});
@mnikitina
I was trying but some error
P.S.
I tried like this and it works, some of previos code and your code
function Line() {
// get all groups the current user belongs to
var userId = _spPageContextInfo.userId;
var userGroups = [];
pnp.sp.web.siteUsers.getById(userId).groups.get()
.then(function(groupsData){
for (var i = 0; i < groupsData.length; i++) {
userGroups.push(groupsData[i].Title);
}
//check if the user is a member of Managers user group
if (userGroups.indexOf('SupervizoriIT') >= 0) {
//Show grid container
var datasource = ['IT', 'Business', 'Depozit IT'];
alert('group')
} else {
var datasource = ['IT', 'Business']
alert(' NO group')
}
if (datasource) {
fd.field('Linia_x0020_Support').widget.setDataSource(datasource);
}
});
}
fd.spRendered(function() {
//Hiding the grid containig fields available to managers only
//$('.listcontrol').hide();
//call function on form load
Line();
});
@mnikitina
Now i use SharePoint groups.
But if i would like to setup with Active Directory security groups , how can i verify user membership in security active directory group?
Powershell script: may be something i can try in javascript on form load?
'''
$users = "TestUser1","TestUser2"
$group = "Domain Admins"
$members = Get-ADGroupMember -Identity $group -Recursive | Select -ExpandProperty Name
ForEach ($user in $users) {
If ($members -contains $user) {
Write-Host "$user exists in the group"
} Else {
Write-Host "$user not exists in the group"
}}
'''
Hello @ixxxl,
I'm sorry, I've missed your post.
You can create separate forms for different user groups and route between them based on SharePoint security groups.
@mnikitina
Ok with Sharepoint group is clarified, thank you.
But with security Active directory? Can i check if a user is from domain\group1 (security group AD)
SharePoint supports Sharepoint Groups only. But SharePoint groups are synchronized with Azure AD profiles.
@mnikitina
Thank you, I have active directory on prem, and security groups are only as a groups, you can't see people in group.