5

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..

5

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:

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 = "jdoe@contoso.onmicrosoft.com"
$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()
0

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 = "jdoe@contoso.onmicrosoft.com"
$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()
  • 1
    i have using your code ,getting Error :- New-Object : Cannot find an overload for "SPFieldUserValue" and the argument count: "2". – Akshay Dattatray Nangare Jan 13 '16 at 11:13

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.