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

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

By:

Date:

Get Field names from a list

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

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.

Is your business still running on paper trails, sprawling Excel files, or ageing Access databases? There’s a better way — and I can show you exactly what it looks like. I’m the Technical Director of Vantage 365, a Microsoft solutions consultancy working with clients across the UK, the Netherlands, and worldwide. For over 30 years I’ve been turning messy, manual business processes into clean, automated systems that save time, reduce errors, and give teams the visibility they need to make better decisions. SharePains is not just any blog run by a Microsoft MVP. Have you ever used Try-Catch in Power Automate? The original post about Try-Catch in Power Automate can still be found on this site, https://staging.sharepains.com/2018/02/07/try-catch-finally-in-power-automate-flow/ Or have you ever used the Pieter’s method to avoid variables and speed up your flows? https://staging.sharepains.com/2020/03/11/pieters-method-for-advanced-in-flows/ You can contact me using contact@staging.sharepains.com

One response to “The object is used in the context different from the one associated with the object”

  1. westerdaled Avatar

    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

Leave a Reply

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