Exception calling "ExecuteQuery" with "0" argument(s): "The node to be inserted is from a different

Steel Contributor

Last week i was able to hide 2 site columns from New list form using this PnP script (the fields were of type Single line of text and Date/Time):-

 

$User = "admin@****.onmicrosoft.com"
$SiteURL = "https://****.sharepoint.com"
#Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$Password = Read-Host -Prompt "enterpassword" -AsSecureString
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)

#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds

$fieldTitle = "ProjectNumber"
$customfield = $Context.Site.RootWeb.Fields.GetByInternalNameOrTitle($fieldTitle)
$customfield.SetShowInEditForm($true)
$customfield.SetShowInNewForm($false)
$customfield.UpdateAndPushChanges($true)

$Context.ExecuteQuery()

 

but today when i try the above script to hide a site column of type drop-down on the same site collection>> i got this error on the $Context.ExecuteQuery(), as follow-

 

PS C:\windows\system32> $Context.ExecuteQuery()
Exception calling "ExecuteQuery" with "0" argument(s): "The node to be inserted is from a different document context."
At line:1 char:1
+ $Context.ExecuteQuery()
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ServerException

 

errrrrrrrrrrrrrrrrro2.png

now i do not think the issue is with the field type, as i usually hide drop-down column using this PnP script without any issue.. but not sure what is going on, as this script was working for me last week (4-5 days ago!!).

Thanks in advance for any help

4 Replies
Could you try to see what happens if you separate the changes of SetShowInEditForm and SetShowInNewForm? Changing your code as follows:

$customfield.SetShowInEditForm($true)
$customfield.UpdateAndPushChanges($true)
$Context.ExecuteQuery()

$customfield.SetShowInNewForm($false)
$customfield.UpdateAndPushChanges($true)
$Context.ExecuteQuery()

@Paul Paschayes this worked for me!! not sure what is going on? any idea?

I don't know exactly what's going on, but I can imagine executing both operations in a single call to executequery results in some kind of conflict internally.
Hi John
,I have tried same code. But still I am getting error . My field Order Status is choice field. Please suggest if I am missing something.

$User = "xyz.onmicrosoft.com"
$SiteURL = "https://xyz/sites/SPTesting"
#Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$Password = Read-Host -Prompt "enterpassword" -AsSecureString
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)

#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds

$fieldTitle = "Order Status"
$customfield = $Context.Site.RootWeb.Fields.GetByInternalNameOrTitle($fieldTitle)

$Context.Load($customfield)
$Context.ExecuteQuery()

$customfield.SetShowInEditForm($False)
$customfield.UpdateAndPushChanges($true)
$Context.ExecuteQuery()