2

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

  • Can you write CSOM c# code ? – Gautam Sheth Jul 13 '17 at 16:26
1

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()
  • Thanks, but I am still getting an error from this. Firstly, you switched from $context to $clientContext, but I have changed this back. Secondly, I am getting an error at the end, on the last $context.ExecuteQuery(). I am getting Exception calling "ExecuteQuery" with "0" argument(s): "This functionality is unavailable for fields not associated with a list." Any ideas what might be wrong? – Gizmo3k Jul 14 '17 at 8:07
  • 1
    can you check edited code ? Also, instead of getting the field from web, can you get it from list ? – Gautam Sheth Jul 14 '17 at 8:13
  • 1
    Got it, thanks! That works great! Also, we don't need $context.Load($group.Id) - that gives an error, but isn't required. If you want to edit that bit out, I will accept this as the answer, and thank you very much for sorting this for me! – Gizmo3k Jul 14 '17 at 8:27
  • done, glad it worked for you :) – Gautam Sheth Jul 14 '17 at 8:34
  • 1
    Oh, you don't need $context.Load($listOriginal.Fields) either! – Gizmo3k Jul 14 '17 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.