currently i have ABC list with People column type[Person or Group]
i am trying to get user name from column using CSOM in Powershell script for SharePoint Online..
so please suggest some solutions..
It depends whether user field is single or multi valued.
List item for People column type[Person or Group]
has the following type in CSOM API:
Single
: FieldUserValue class
Multi
: array of FieldUserValue class
In turn FieldUserValue class
does not expose login property (the list of properties could be found here)
The following example demonstrates how to retrieve login property for AssignedTo
(multi-valued) or Author
(single-valued) user fields in Tasks
list:
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
Function Get-SPOContext([string]$Url,[string]$UserName,[string]$Password)
{
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
return $context
}
$UserName = "[email protected]"
$Password = Read-Host -Prompt "Enter the password"
$Url = "https://contoso.sharepoint.com"
$listTitle = "Tasks" #set List title
#$fieldName = "AssignedTo" #multi valued
$fieldName = "Author" #single valued
$itemId = 1 #set List Item Id
$context = Get-SPOContext -Url $Url -UserName $UserName -Password $Password
$list = $Context.Web.Lists.GetByTitle($listTitle)
$item = $list.GetItemById($itemId)
$context.Load($item)
$context.ExecuteQuery()
$userValue = $item.FieldValues[$fieldName]
if($userValue -is [Microsoft.SharePoint.Client.FieldUserValue]){ #Single user field value
$userItem = $context.Web.GetUserById($userValue.LookupId)
$context.Load($userItem)
$context.ExecuteQuery()
Write-Host $userItem.LoginName
}
else {
foreach($userValuePart in $userValue) {
$userItem = $context.Web.GetUserById($userValuePart.LookupId)
$context.Load($userItem)
$context.ExecuteQuery()
Write-Host $userItem.LoginName
}
}
$context.Dispose()
This will work for you.
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
Function Get-SPOContext([string]$Url,[string]$UserName,[string]$Password)
{
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
return $context
}
Function Get-ListItems([Microsoft.SharePoint.Client.ClientContext]$Context, [String]$ListTitle) {
$list = $Context.Web.Lists.GetByTitle($listTitle)
$qry = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
$items = $list.GetItems($qry)
$Context.Load($items)
$Context.ExecuteQuery()
return $items
}
$UserName = "[email protected]"
$Password = Read-Host -Prompt "Enter the password"
$Url = "https://contoso.sharepoint.com/"
$context = Get-SPOContext -Url $Url -UserName $UserName -Password $Password
$items = Get-ListItems -Context $context -ListTitle "Tasks"
foreach($item in $items)
{
$user = $item["AssignedTo"]
$userObj = New-Object Microsoft.SharePoint.SPFieldUserValue($web, $user)
$accountName = $userObj.User.UserLogin
$accountName.substring($accountName.IndexOf("\")+1)
Write $accountName.substring($accountName.IndexOf("\")+1)
}
$context.Dispose()