none
How to get a Person or Group column value using CSOM and PowerShell RRS feed

  • Question

  • I'm trying to do this from PowerShell using the CSOM and I can't get it to work. I'm getting a null-valued expression error on the $manager assignment

    $poslist = $web.Lists.GetByTitle("Points of Sale")
    $ctx.Load($poslist)
    $ctx.ExecuteQuery()
    
    write-host $poslist.Title
    $caml = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
    $items = $poslist.GetItems($caml)
    $ctx.Load($items)
    $ctx.ExecuteQuery()
    
    foreach ($item in $items)
    {
        $manager =  $item["Manager"]
        Write-Host $item["Title"] $item["Brand"] $item["Name"] $manager
    }


    Stephane


    Stephane

    Thursday, March 9, 2017 6:54 PM

Answers

  • Hi,

    You could use $user.LookupId/$user.LookupValue, here is sample code for your reference.

    Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"  
    Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  
    
    $siteURL = "http://sp:12001" 
    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)  
    try{      
    	$poslist = $ctx.web.Lists.GetByTitle("Employee")
    	$ctx.Load($poslist)
    	$ctx.ExecuteQuery()
    
    	write-host $poslist.Title
    	$caml = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
    	$items = $poslist.GetItems($caml)
    	$ctx.Load($items)
    	$ctx.ExecuteQuery()
    
    	foreach ($item in $items)
    	{
    	    $user =  $item["MUser"]
    		Write-Host $user.LookupId
    		Write-Host $user.LookupValue		
    	    #Write-Host $item["Title"] $item["Brand"] $item["Name"] $user
    	}
    }  
    catch{  
        write-host "$($_.Exception.Message)" -foregroundcolor red  
    } 

     

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Friday, March 10, 2017 2:12 AM
  • hello, are you sure that field's internal name is "Manager"? Can it be so that it's title and internal names are different? It is safer to use internal name in the code. You can check fields names by the following code:

    var fields = list.Fields;
    ctx.Load(fields);
    ctx.ExecuteQuery();
    
    foreach (var field in fields)
    {
        Console.WriteLine(field.InternalName);
    }


    Blog - http://sadomovalex.blogspot.com
    Dynamic CAML queries via C# - http://camlex.codeplex.com

    • Marked as answer by S Bouillon Friday, March 10, 2017 3:54 PM
    Friday, March 10, 2017 1:01 PM

All replies

  • Hi,

    You could use $user.LookupId/$user.LookupValue, here is sample code for your reference.

    Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"  
    Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  
    
    $siteURL = "http://sp:12001" 
    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)  
    try{      
    	$poslist = $ctx.web.Lists.GetByTitle("Employee")
    	$ctx.Load($poslist)
    	$ctx.ExecuteQuery()
    
    	write-host $poslist.Title
    	$caml = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
    	$items = $poslist.GetItems($caml)
    	$ctx.Load($items)
    	$ctx.ExecuteQuery()
    
    	foreach ($item in $items)
    	{
    	    $user =  $item["MUser"]
    		Write-Host $user.LookupId
    		Write-Host $user.LookupValue		
    	    #Write-Host $item["Title"] $item["Brand"] $item["Name"] $user
    	}
    }  
    catch{  
        write-host "$($_.Exception.Message)" -foregroundcolor red  
    } 

     

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Friday, March 10, 2017 2:12 AM
  • Thanks for your time and effort Lee,

    I tried that, but both $User.LookupId and $User.LookupValue are blank/null, while the field does contain a Person reference.

    Stephane


    Stephane

    Friday, March 10, 2017 10:57 AM
  • hello, are you sure that field's internal name is "Manager"? Can it be so that it's title and internal names are different? It is safer to use internal name in the code. You can check fields names by the following code:

    var fields = list.Fields;
    ctx.Load(fields);
    ctx.ExecuteQuery();
    
    foreach (var field in fields)
    {
        Console.WriteLine(field.InternalName);
    }


    Blog - http://sadomovalex.blogspot.com
    Dynamic CAML queries via C# - http://camlex.codeplex.com

    • Marked as answer by S Bouillon Friday, March 10, 2017 3:54 PM
    Friday, March 10, 2017 1:01 PM
  • Bingo !

    Turns out the Manager field's original name was "Contact" and Zone Manager is "Zone_x0020_Manager"

    Thanks all for your support

    Stephane


    Stephane

    Friday, March 10, 2017 3:54 PM