Restrict People Picker to SharePoint Online group programatically with CSOM and PowerShell
-
27-12-2020 - |
Question
I need to update an existing people picker column on a site, to narrow down the list of possible users that can be entered.
At present you can choose from All Users, I want to limit this to a particular SharePoint group. I need to do this on a large number of sites, so want to use PowerShell and CSOM.
I can get the relevant field with CSOM, but I don't know what value to change to limit the scope of the list of users. Can anyone help?
Here is what I have, to get the relevant field:
#————————————————————-
# LOAD CLIENT ASSEMBLIES
#————————————————————-
Add-Type -Path (Resolve-Path "$env:CommonProgramFiles\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll")
Add-Type -Path (Resolve-Path "$env:CommonProgramFiles\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll")
Add-Type -Path (Resolve-Path "$env:CommonProgramFiles\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.WorkflowServices.dll")
#————————————————————-
# INITIALIZE CONTEXT
#————————————————————-
$creds = Get-PnPStoredCredential -Name Creds -Type PSCredential
$context = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($creds.UserName, $creds.Password)
$context.Credentials = $credentials
#————————————————————-
# LOAD LIST
#————————————————————-
[string] $originaListTitle = "List Title"
$listOriginal = $context.Web.Lists.GetByTitle($originaListTitle)
$context.Load($listOriginal)
$context.ExecuteQuery() # This loads the necessary list ID
$Field = $context.Web.AvailableFields.GetByInternalNameOrTitle("FieldName")
$context.Load($Field)
$context.ExecuteQuery()
Alternatively, if I have missed something in PnP-Powershell, I am happy to use that instead.
Thanks
Solution
You can modify the field as below:
$groups = $context.Web.SiteGroups
$context.Load($groups)
$context.ExecuteQuery()
$group = $groups.GetByName("Group Name")
$context.Load($group)
$context.ExecuteQuery()
Write-host "Group id is - " $group.Id
$userField = [Microsoft.SharePoint.Client.ClientContext].GetMethod("CastTo").MakeGenericMethod([Microsoft.SharePoint.Client.FieldUser]).Invoke($context, $Field)
# enter the id of the sharepoint group
$userField.SelectionGroup = 6;
$userField.Update();
$context.Load($userField)
$context.ExecuteQuery()
So, your overall code would be:
#————————————————————-
# LOAD CLIENT ASSEMBLIES
#————————————————————-
Add-Type -Path (Resolve-Path "$env:CommonProgramFiles\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll")
Add-Type -Path (Resolve-Path "$env:CommonProgramFiles\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll")
Add-Type -Path (Resolve-Path "$env:CommonProgramFiles\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.WorkflowServices.dll")
#————————————————————-
# INITIALIZE CONTEXT
#————————————————————-
$creds = Get-PnPStoredCredential -Name Creds -Type PSCredential
$context = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($creds.UserName, $creds.Password)
$context.Credentials = $credentials
#————————————————————-
# LOAD LIST
#————————————————————-
[string] $originaListTitle = "List Title"
$listOriginal = $context.Web.Lists.GetByTitle($originaListTitle)
$context.Load($listOriginal)
$context.Load($listOriginal.Fields)
$context.ExecuteQuery() # This loads the necessary list ID
$Field = $listOriginal.Fields.GetByInternalNameOrTitle("FieldName")
$context.Load($Field)
$context.ExecuteQuery()
$groups = $context.Web.SiteGroups
$context.Load($groups)
$context.ExecuteQuery()
$group = $groups.GetByName("Group Name")
$context.Load($group)
#$context.Load($group.Id)
$context.ExecuteQuery()
#Write-host "Group id is - " $group.Id
# casting as user field
$userField = [Microsoft.SharePoint.Client.ClientContext].GetMethod("CastTo").MakeGenericMethod([Microsoft.SharePoint.Client.FieldUser]).Invoke($context, $Field)
# enter the id of the sharepoint group
$userField.SelectionGroup = 6;
#$userField.SelectionGroup = $group.Id;
$userField.Update();
$context.Load($userField)
$context.ExecuteQuery()
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange