1

I've been playing around with adding fields to Sharepoint via PowerShell using XML.

When I execute the code below it creates the field under the group but kicks out an error each and every time. Yet the new column is created in the identified group. I started with field names equal to MyTest1, 2, etc. Each time I receive the error below.

Ideally I want to use this code in combination with Excel to create the fields vs. using the GUI but this error is driving me nuts.

Error: Exception calling "ExecuteQuery" with "0" argument(s): "The object has been updated by another user since it was last fetched."

<# Associated DLLs loaded #>
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"      

function LoadColumns($mycredpath)
    {
        $creds=Import-Clixml -Path $mycredpath
        #Create powershell credential object that can be used to login using connect-pnponline -credentials parm
        $Creds = New-Object System.Management.Automation.PSCredential ($creds.UserName, $creds.Password)
        
        $Mycreds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($creds.UserName,$creds.Password)  
        $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)  
        $ctx.credentials = $Mycreds 
        try
        {
            #Get all Site columns from the site
            $Fields = $Ctx.web.Fields
            $Ctx.Load($Fields)
            $Ctx.executeQuery()
        }
        catch
        {
            Write-Host "auth fail"
        }
    
       $testval=
       '<Field' +
       ' Type='+'"'+'Text'+'"'+
       ' DisplayName='+'"'+'MyTest9'+'"'+
       ' StaticName='+'"'+'MyTest9'+'"'+
       ' Name='+'"'+'MyTest9'+'"'+
       ' Indexed="FALSE"'+
       ' MaxLength="255"'+
       ' Group='+'"'+'_TESTGroup'+'"'+ 
       ' Version="1"' +
       ' AllowDeletion="TRUE"' +
       ' Required="False"'+
       ' EnforceUniqueValues="FALSE"'+
       ' />'
    
       write-host 'test value ' $testval

        $NewField = $Fields.AddFieldAsXml($testval,$False,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldCheckDisplayName)
    
        try
        {
            $ctx.ExecuteQuery()
            write-host "Success"
        }
        catch
        {
            Write-Host "error number is " $_ "ctx execute query"
        }
    }

When I replaced $testval with the following for a choice field xml template it worked perfectly.

<# $testval=
        '<Field'+
        ' Type="Choice"'+ 
        ' DisplayName="PDR122" '+
        ' Required="FALSE" '+
        ' EnforceUniqueValues="FALSE" '+
        ' Indexed="FALSE" '+
        ' Format="Dropdown" '+
        ' FillInChoice="FALSE" '+
        ' Group="_TESTGroup" '+
        ' StaticName="PDR123"'+
        ' Name="PDR123" '+
        ' AllowDeletion="TRUE" '+
        ' CustomFormatter=""'+
        '>'+
        '<CHOICES>'+
        '<CHOICE>Yes</CHOICE><CHOICE>No</CHOICE><CHOICE>N/A</CHOICE></CHOICES>'+
        '<Validation Message="Checking N/A in this column will set the other related PDR columns to N/A." '+
        '/></Field>' #>

1 Answer 1

1

Everything looks ok in the code you have shared... Except for the site column XML. Remove the Version property from the Site column definition XML and execute the PowerShell script again it should work.

Remove - ' Version="1"'+

$testval=
   '<Field'+
   ' Type='+'"'+'Text'+'"'+
   ' DisplayName='+'"'+'MyTest9'+'"'+
   ' StaticName='+'"'+'MyTest9'+'"'+
   ' Name='+'"'+'MyTest9'+'"'+
   ' Indexed="FALSE"'+
   ' MaxLength="255"'+
   ' Group='+'"'+'_TESTGroup'+'"'+ 
   ' AllowDeletion="TRUE"'+
   ' Required="False"'+
   ' EnforceUniqueValues="FALSE"'+
   ' />'

Explanation - At the time of creation/update a site column SharePoint expects that the version number of this site column object increments after the update. By setting the static value of version property of site column via Schema and forcing the version to be fixed, that number doesn't increment.

1
  • Thanks so much and that was an easy fix and it worked. What a silly error on my part but having this is going to save me a lot of time. Thanks again.
    – Pablo
    Aug 30, 2021 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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