1

How to get Field values of a list item using CSOM Powershell?

I am using below code:

foreach($field in $List.Fields)
{
    write-host $field
}

I am getting the values as output:

Microsoft.SharePoint.Client.Field
Microsoft.SharePoint.Client.FieldDataTime
Microsoft.SharePoint.Client.FieldLookup

But I want all field values like ID, Title, Created By etc.

5
  • You need to get the fields first so: $fields = $List.Fields; $ctx.load($fields); $ctx.ExecuteQuery()
    – Bunzab
    Commented Nov 30, 2018 at 9:19
  • @Bunzab Yes i have loaded the fields in my code. I just want the Created By and Modified By values of all the items using CSOM Commented Nov 30, 2018 at 9:55
  • In that case you need to get each item...
    – Bunzab
    Commented Nov 30, 2018 at 10:00
  • i am trying to get each item only but it is returning values as Microsoft.SharePoint.Client.FieldLookup instead of user names Commented Nov 30, 2018 at 10:12
  • $ListItem["Editor"].LookupValue solved my problem Commented Dec 3, 2018 at 5:48

3 Answers 3

2

Your code does not retrieve any list items. This is an example of how you do it:

$listItems = $list.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$ctx.load($listItems)

$ctx.executeQuery()
foreach($listItem in $listItems)
{
    Write-Host "ID - " $listItem["ID"] "Title - " $listItem["Title"]
}  

Credits to: https://www.c-sharpcorner.com/article/list-item-operations-using-csom-with-powershell-for-sharepoi/

2
  • $ListItemsCollection =$List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()) $ctx.load($ListItemsCollection) $ctx.executeQuery() foreach($ListItem in $ListItemsCollection) { $Author = $ListItem["Author"].LookupValue Write-Host $Author $Editor = $ListItem["Editor"].LookupValue Write-Host Editor } Commented Dec 3, 2018 at 5:44
  • Thanks @Denis. I was actually looking for $ListItem["Editor"].LookupValue which gives the values of user names instead of their types. Commented Dec 3, 2018 at 5:47
1

If you want to get list item values, you can use this

$web=Get-SPWeb "http://www.myspapp/sites/myspweb"
$list=$web.Lists["MyListNAme"]

foreach($item in $list.Items)
{
    Write-Host  $item["ID"] $item["Title"]
}

If you want to get column names only, you can use this

$web=Get-SPWeb "http://www.myspapp/sites/myspweb"
$list=$web.Lists["MyListNAme"]

foreach($field in $list.Fields)
{
    Write-Host $field
}
1
  • 2
    I am doing that in CSOM. The above code is for SSOM . Please assist Commented Nov 30, 2018 at 9:14
1

You need to get the item/s first (either by CAML query or by id) and the property values are accessible via:

$item.FieldValues

So you can get the values you are after like so:

$item.FieldValues.Title
$item.FieldValues.ID

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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