1

On Sharepoint Online, using Powershell/CSOM, I am iterating through Doc Library folder set, retrieving the Folder.Files, iterating through the files, and trying to match a custom field value in the list item associated with that file.

Microsoft says File.ListItemAllFields should provide all of the fields and values I need, but it isn't retrieving the custom field value that I'm looking for.

Here is a simplified powershell snippet:

# Get Target List
$list = $lists.GetByTitle($Lib + " Library")
$ctx.Load($list)
$ctx.Load($list.RootFolder)
$ctx.ExecuteQuery()

# Get List Folders
$folders = $list.RootFolder.Folders 
$ctx.Load($folders)
$ctx.ExecuteQuery()

$folders = $folders | sort Name

foreach ($folder in $folders)
{
    $files = $folder.Files
    $ctx.Load($files)
    $ctx.ExecuteQuery()

    foreach ($file in $files)
    {
        $ctx.Load($file)
        $ctx.Load($file.ListItemAllFields)
        $ctx.ExecuteQuery()

        if($file["IPGroup"] -eq $IPGroup)
        { 
            "File {0} has IPGroup {1}..." -f $file.Name, $file["IPGroup"] | Write-Host -ForegroundColor Green; continue 
        }
    }
}

The custom field "IPGroup" is not being returned by the File.ListItemAllFields property.

What am I doing wrong (this time)?

1

Turns out that $item.get_item('fieldname') is all I needed!

This is how I modified the above code:

    $ctx.Load($file)
    $ctx.Load($file.ListItemAllFields)
    $ctx.ExecuteQuery()

    $item = $file.ListItemAllFields
    $ctx.Load($item)
    $ctx.ExecuteQuery()

    $name = $file.Name
    $group = $item.get_item('IPGroup')

    if($group -eq $IPGroup)
    { 
        "File {0} has IPGroup {1}..." -f $name, $group | Write-Host -ForegroundColor Green; continue 
    }

Hope this saves someone a few hours of headscratching :)

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.