SharePoint Online PnP – The object is used in the context different from the one associated with the object.

Today  one of my colleagues was trying to create a list item in SharePoint Online using the PnP PowerShell.

Adding an item to a list

He was running the following command

Add-PnPListItem -List $dlist -Values @{"Title" = "$webTitle"}

and got the following error:

Add-PnPListItem : The object is used in the context different from the one associated with the object.
At line:4 char:5
+ Add-SPOListItem -List $dlist -Values @{"Title" = "$webTitle"}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (:) [Add-SPOListItem], InvalidOperationException
+ FullyQualifiedErrorId : EXCEPTION,SharePointPnP.PowerShell.Commands.Lists.AddListItem

<h3>The -web switch</h3>
I noticed that the -web switch was missing

Add-PnPListItem -List $dlist -Values @{"Title" = "$webTitle"} -Web $web

So what happened?

When -Web is missing in PnP PowerShell Cmdlets the root site will be assumed as the site to be used.

Therefore Add-PnPListitem will run on the root site while the list specified lives in a sub site. This problem can also be avoid by running the connect cmdlet and specify the sub site as your connected site.

See also

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

Advertisements

One thought on “SharePoint Online PnP – The object is used in the context different from the one associated with the object.

Leave a Reply to Bastiaan 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.