Office 365 – PnP PowerShell – The object is used in the context different from the one associated with the object

Get Field names from a list

Today I tried to load the Fields from a list using PnP PowerShell and CSOM.

$ctx = Get-PnPContext

$list = Get-PnPList -Web $web -Identity $listName

$ctx.Load($list.Fields)
$ctx.ExecuteQuery()

This quite quickly resulted in :

Exception calling “Load” with “1” argument(s): “The object is used in the context different from the one associated with the object.”
At line:1 char:1
+ $web.Context.Load($list.Fields)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : InvalidOperationException

Ok, maybe I need to use my $web.Context

$list = Get-PnPList -Web $web -Identity $listName

$web.Context.Load($list.Fields)
$web.Context.ExecuteQuery()

No, the same error still occurs.

Hmm, so the $list object has its own web  (ParentWeb) object

$list = Get-PnPList -Web $web -Identity $listName

$list.ParentWeb.Context.Load($list.Fields)
$list.ParentWeb.Context.ExecuteQuery()

and now I can get to my $list.Fields.

 

See Also

The object is used in the context different from the one associated with the object.

 

Advertisements

One thought on “Office 365 – PnP PowerShell – The object is used in the context different from the one associated with the object

  1. Thank you sir!
    I can use the same technique here for a file I have just restored from the RecycleBin

    $f = Get-PnPFolderItem -FolderSiteRelativeUrl “Documents/GDPR/test” -ItemName DeleteMe1.docx

    $f.Context.Load($f.ListItemAllFields)
    $f.Context.ExecuteQuery()
    $f.ListItemAllFields

    Catch you next time I am at the London PowerPlatform meet

    Like

Leave a Reply to westerdaled Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.