当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 读取/更新人员或组字段(人员选择器)值

作者:精品下载站 日期:2024-12-14 14:45:30 浏览:11 分类:玩电脑

SharePoint Online:使用 PowerShell 读取/更新人员或组字段(人员选择器)值


要求:使用 PowerShell 在 SharePoint Online 中获取或设置“个人或组”字段值。

添加或编辑项目时,人员或组字段提供可搜索的人员和组列表。当您键入用户名的前四个字符时,它的作用就像一个自动完成字段。以下是我的 PowerShell 脚本集合,用于检索和更新人员或组字段值。

[玩转系统] SharePoint Online:使用 PowerShell 读取/更新人员或组字段(人员选择器)值

SharePoint Online - 使用 PowerShell 获取人员选择器字段值:

若要获取 SharePoint Online 中的人员或组字段值,请使用此 PowerShell。


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/pmo"
$ListName="Projects"
$FieldName= "ProjectManager" #Internal Name
$ListItemID="5"

#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials

#Get the List & Item to update
$List = $Ctx.Web.lists.GetByTitle($ListName) 
$ListItem = $List.GetItemById($ListItemID)
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()

#Get the People picker Column of the list item
$FieldValue = [Microsoft.SharePoint.Client.FieldUserValue]$ListItem[$FieldName]

#Get the Display Name and Email Field
Write-host $FieldValue.LookupValue
Write-host $FieldValue.Email

PowerShell 在 SharePoint Online 中检索多值个人或组字段值:


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/pmo"
$ListName="Projects"
$FieldName= "ProjectMembers" #Internal Name
$ListItemID="25"

#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials

#Get the List & Item to update
$List = $Ctx.Web.lists.GetByTitle($ListName) 
$ListItem = $List.GetItemById($ListItemID)
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()

#Get the Column Values
$FieldValues = [Microsoft.SharePoint.Client.FieldUserValue[]]$ListItem[$FieldName]

Write-host -f Yellow "Found $($FieldValues.Count) Users in the Given Field Value!"

#Get Each User from the collection
ForEach($FieldValue in $FieldValues)
{
    #Get the Display Name and Email Field
    Write-Host -f Green $FieldValue.LookupValue : $FieldValue.Email
}

您可以从 Person 或 Group Column 值获取 SPUser 对象,如下所示:


    
#Get the "Created By" column value of the Item
$CreatedBy = $ListItem.FieldValues["Author"]

#Get the User Object
$User = $Ctx.Web.GetUserById($CreatedBy.LookupId)
$Ctx.Load($User)
$Ctx.ExecuteQuery()

$User | Select Email, Title, LoginName

使用 PowerShell 更新人员或组字段值:

以下是用于更新 SharePoint Online 中的个人或组列的 PowerShell。


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/pmo"
$ListName="Projects"
$FieldName= "ProjectManager" #Internal Name
$ListItemID="25"
$UserID="[email protected]"

#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials

#Get the List & Item to update
$List = $Ctx.Web.lists.GetByTitle($ListName) 
$ListItem = $List.GetItemById($ListItemID)
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()

#Get the User to Update
$SPOUser = $Ctx.Web.EnsureUser($UserID)
$Ctx.Load($SPOUser)
$Ctx.ExecuteQuery()

If($SPOUser -ne $Null)
{
    #Update the People picker column
    $ListItem[$FieldName] = $SPOUser
    $ListItem.Update()
    $Ctx.ExecuteQuery()
    Write-Host -f Green "List Item has been updated!"
}

PowerShell 在 SharePoint Online 中设置多值人员选取器字段值

要设置允许多个值的人员或组字段,请使用以下 PowerShell:


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/pmo"
$ListName="Projects"
$FieldName= "ProjectMembers" #Internal Name
$ListItemID="25"
$UserIDs=@("[email protected]","[email protected]","[email protected]")

#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials

#Get the List & Item to update
$List = $Ctx.Web.lists.GetByTitle($ListName) 
$ListItem = $List.GetItemById($ListItemID)
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()

#Resolve each user ID and get the User Objects
$UserValueColl = @()
ForEach($UserID in $UserIDs)
{
    #Get the User to Update
    $SPOUser = $Ctx.Web.EnsureUser($UserID)
    $Ctx.Load($SPOUser)
    $Ctx.ExecuteQuery()

    If($SPOUser -ne $Null)
    {
        $SPOUserValue = New-Object Microsoft.SharePoint.Client.FieldUserValue
        $SPOUserValue.LookupId = $SPOUser.id
        $UserValueColl += $SPOUserValue
    }
}

IF($UserValueColl.length -gt 0)
{
    $UserValueCollCollection = [Microsoft.SharePoint.Client.FieldUserValue[]]$UserValueColl
    #Update the Multi-People picker column
    $ListItem[$FieldName] = $UserValueCollCollection
    $ListItem.Update()
    $Ctx.ExecuteQuery()
    Write-Host -f Green "List Item has been updated!"
}

使用 PnP PowerShell 更新人员或组字段值

人员或组字段可以使用用户的电子邮件 ID 或站点用户信息列表中的用户或组的“id”进行更新。使用以下格式设置人员选择器字段值:@{“PersonField”=“[email protected]”,“1”},其中“1”是人员的 ID。


#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/pmo"
$ListName = "Projects"
$FieldName = "Project_x0020_Manager"
$ItemID = 2
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
 
#Update person or group field value using PnP PowerShell
Set-PnPListItem -List $ListName -Identity $ItemID -Values @{$FieldName = "[email protected]"}

同样,要使用用户 ID 更新多人或组列,请使用以下命令:


Set-PnPListItem -List $ListName -Identity $ItemID -Values @{$FieldName = 13, 23}

此处,“13”和“23”是 SharePoint Online 网站集中的用户 ID。我的另一篇文章使用 PowerShell 检索和更新 SharePoint 本地的人员选取器字段值:PowerShell to Get or Set Person or Group (People Picker) Field Values in SharePoint

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯