1

I am getting error message like : Exception calling "ExecuteQuery" with "0" argument(s): "The node to be inserted is from a different document context."

Can anyone suggest what I am missing here...please fine below code.

#Load SharePoint CSOM Assemblies
    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"
       
    #Config Parameters
    $SiteURL= "SITEURL"
    $ListName="TestBest1"
    $FieldName = "DocumentImportStatus" #Display Name
     
    #Setup Credentials to connect
    $Cred = Get-Credential
    $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
     
    Try {
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Cred
       
        #Get the web, List and Field objects
        $Web=$Ctx.Web
        $List=$web.Lists.GetByTitle($ListName)
        $Field = $List.Fields.GetByTitle($FieldName)
     
        #Hide the column from New & Edit forms
        $Field.SetShowInEditForm($False) 
        $Field.SetShowInNewForm($False) 
        $Field.UpdateAndPushChanges($True)
        $Ctx.ExecuteQuery()
          
        Write-host -f Green "List Field hidden Successfully!"
    }
    Catch {
        write-host -f Red "Error hiding List Column: " $_.Exception.Message
    }
0

It looks like you're missing a couple of calls to Load method... try changing the following part of your script:

    $Web=$Ctx.Web
    $List=$web.Lists.GetByTitle($ListName)
    $Field = $List.Fields.GetByTitle($FieldName)

to this:

$Web=$Ctx.Web
$Ctx.Load($Web)
$List=$web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.ExecuteQuery()
$Field = $List.Fields.GetByTitle($FieldName)
$Ctx.Load($Field)
$Ctx.ExecuteQuery()
0

The issue is resolved now find below code for the same.

$ColumnName="DocumentImportStatus"
#Get all Site columns from the site
$Fields = $Ctx.web.Fields
$Field = $Fields.GetByTitle($ColumnName)
$Ctx.Load($Field)

#Hide the column from New & Edit forms
$Field.SetShowInEditForm($False)
$Field.SetShowInNewForm($False)
$Field.UpdateAndPushChanges($True)
$Ctx.ExecuteQuery()

Write-host -f Green "Column DocumentImportStatus hidden on Edit form Successfully!"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.