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

He was running the following command

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

and got the following error:

Add-SPOListItem : 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

I noticed that the -web switch was missing

Add-SPOListItem -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-SPOListitem will run on the rootsite while the list specified lives in a sub site.

Advertisements