3

I am trying to update a site column on my content. When I save the changes, I get the error "The object has been updated by another user since it was last fetched." When I searched the issue, I found a lot of references to an issue with the schemaXML property of the column having a version number in. When I checked the column, it did indeed have a version attribute. The posts further said to remove the version attribute and then redeploy the column. This is where I'm struggling. I am running the following powershell script

$web = get-spweb http://site.domain.com

$column = $web.Fields["Escrow"];
$xml = $column.schemaXML;
$xml = $xml -replace " Version=`"1`"","";
$column.schemaXML = $xml;
$xml = $column.schemaXMLWithResourceTokens;
$xml = $xml -replace " Version=`"1`"","";
$column.schemaXMLWithResourceTokens = $xml;
$column.update(); 

The script immediately spits out the same error. I tried what this post suggested, but got the same error. How do I resolve this?

6

Yes, the short answer is you can't do anything to the field if you accidently deployed it with a version attribute. Whoops.

As this is something I've done a few times - ow - and I needed to fix, I've finally gotten around to writing a script to fix it. The script needs to remove the current field from all content types, then delete the field. Then remove the version attribute, then add the field and then finally add it back to the content types. So simple, much pleasure.

It takes the url to the web to update the field in, and the name of the field to 'fix'. It grabs an array of the content types that it needs to work with ($contenttypenames) and uses that at the end to put the field back. I've tested it lightly, so try it on a dev server first.

[CmdletBinding()]
Param(
    [Parameter(Mandatory=$True,Position=1)][string]$url,
    [Parameter(Mandatory=$True,Position=2)][string]$name
)
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction:SilentlyContinue
Set-Location (Split-Path -Path:$MyInvocation.MyCommand.Path -Parent:$true)
Write-Host ("Remove Version flag from '{0}' in '{1}'" -f $name, $url)

$web = Get-SPWeb $url
$web = $web.site.RootWeb
$field = $web.Fields.GetFieldByInternalName($name)
if (!$field) { 
    Write-Host "> Can't find field!" -ForegroundColor:Red
    break
}
[xml]$xml = $field.SchemaXml
$contenttypenames = $web.ContentTypes | ? { $_.FieldLinks | ? { $_.Id -eq $field.ID } } | ? { $_.Parent.Fields.ContainsFieldWithStaticName($field.InternalName) -eq $false } | % { Write-Output $_.Name }

$contenttypenames | % {
    $contenttypename = $_
    write-host ("> Remove field link from {0}..." -f $contenttypename) -ForegroundColor:Green
    $ct = $web.ContentTypes[$contenttypename]
    $ct.FieldLinks.Delete($name)
    $ct.UpdateIncludingSealedAndReadOnly($true)
}

$field = $web.Fields.GetFieldByInternalName($name)
if ($field) { 
    Write-Host "> Deleting existing field..." -ForegroundColor:Green
    $web.fields.Delete($name)
}

Write-Host "> Adding field (version removed)..." -ForegroundColor:Green
$xml.Field.RemoveAttribute("Version")
$web.Fields.AddFieldAsXml($xml.OuterXml) | Out-Null

$contenttypenames | % {
    $contenttypename = $_
    Write-Host ("> Adding field link to {0}..." -f $contenttypename) -ForegroundColor:Green
    $field = $web.Fields.GetFieldByInternalName($name)
    $fieldlink = New-Object Microsoft.SharePoint.SPFieldLink($field)
    $ct.FieldLinks.Add($fieldlink)
    $ct.Update($true)
}

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.