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

Était-ce utile?

La 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()
Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top