Orphaned shadow fields cause "a duplicate name ... was found" - PowerShell solution

When you create a site column of TaxonomyField type, SharePoint creates a "shadow" hidden field with the same name plus a trailing underscore and number. When you delete the main taxonomy field, SharePoint does NOT delete the shadow field. If you
are creating custom fields programmatically (or by installing a feature), you will get an error complaining of a duplicate field name. You cannot resolve this through the UI, but PowerShell can help. After a day of pulling my hair, I wrote the following PS
script. Please comment and improve: I did some testing, but hardly rigorous.
# SiteColumnDuplicateKill
# Run in PowerShell IDE
################## Constant Declaration ##########################################
$siteUrl = "https://your.sitecollection.com"
<#
    This script removes site columns that have been orphaned when a custom site column
    of type TaxonomyFieldType (or TaxonomyFieldTypeMulti) has been deleted. When such a column
    is created, SP creates a "shadow" field with the same name appended with an underscore
    and a number. For example, if you create a TaxonomyFieldType named "Foo", SharePoint
    creates a hidden column called "Foo_0". If you delete the Foo column, SharePoint 
    *DOES NOT* delete the shadow column! If you create another metadata column called
    "Foo", SharePoint will create a new hidden column called "Foo_1", because "Foo_0" already
    exists. As long as you create columns with the SP user interface, this bit of poor
    housekeeping causes no problems. 
    BUT -- if you create a site column by installing a feature, either via a manual wsp install
    or via Visual Studio's deploy function, there is a problem. SharePoint will look at the
    metadata column name in your feature and check to see if there is an existing shadow column
    corresponding to that name. E.g. if your feature includes a column called "Foo", SharePoint
    will look for a hidden column called "Foo_0". If it finds it, SharePoint throws an error
    complaining of a duplicate field name. This is clearly a bug: it ought to behave like the
    UI does: increment the tail number until it gets a unique name. Better still, the bug
    shouldn't exist at all: SharePoint shouldn't keep those shadow columns. But the bug is there,
    and this script solves the problem. It finds orphaned columns and removes them.
    Run this script whenever you delete a custom metadata site column -- either via the UI or
    by de-activating and removing a feature or by using the Visual Studio Retract function.
    NOTE: internally, site columns are called "Fields". I use the terms interchangeably in 
    my comments.
#>
function MakeFieldRef($aFieldTitle) {
    # Creates a PS custom object with information about a shadow field
    # Shadow fields look like this: "My metadata field_0"
    $mainFieldTitle = ""
    # Extract the main field title (the part before the last _)
    $pieces = $aFieldTitle.split("_")
    for($i = 0; $i -lt $pieces.length - 1; $i++) {
        $mainFieldTitle += $pieces[$i] + "_"
    # Remove the extra trailing _
    $mainFieldTitle = $mainFieldTitle.Substring(0, $mainFieldTitle.Length-1)
    # Instantiate the custom object
    $properties = @{'FieldTitle' = $aFieldTitle;
                'RootTitle'=$mainFieldTitle;
                'Index'= 0 + $pieces[$pieces.length - 1]}
    $object = New-Object –TypeName PSObject –Prop $properties
    return $object
###################### Main Processing ##################
clear-host
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
$site = Get-SPSite $siteUrl
$web = $site.RootWeb
#Find all hidden fields that end in an underscore plus one or more digits
$shadowFields = $web.fields | ?{$_.Title -match "_+\d" -and $_.Hidden -eq $true} | Select Title
#$shadowFields
<#
Loop through these shadow fields. For each, determine if there is a corresponding "main" field.
If not, delete the shadow field, because it's not needed. E.g.:
    Shadow field: Foo_0
    If no field called "Foo", delete the shadow field
If there *is* a corresponding main field, we need to preserve the shadow field
that goes along with it. But what if there are several shadow fields? E.g.:
    Shadow fields: Bar_0, Bar_1, Bar_2
    If there is a field called "Bar", we must not delete Bar_2, because it 
    corresponds to that main field. But Bar_0 and Bar_1 are remnants of
    site columns that have been deleted, so we can get rid of them
#>
<# 
    Instantiate an array to hold the field ref objects. A FieldRef has properties FieldTitle, RootTitle and Index.
    RootTitle is the part of the field title preceding the last underscore, and Index
    is the integer value of the number following the last underscore. E.g.:
        shadow field title:  "Silly old Foo_1"
        FieldRef.FieldTitle:  Silly old Foo_1
        FieldRef.RootTitle:   Silly old Foo
        FieldRef.Index:       1
    We need this object to be able to sort the shadow fields in numerical order. Otherwise Foo_10 would sort before F00_9 
#>
$fieldRefs = @()
foreach($field in $shadowFields) {
    # Create a field ref object with properties FieldTitle, RootTitle and Index
    $fieldRef = MakeFieldRef $field.Title    
    $fieldRefs += $fieldRef
# Add a dummy entry to the end. Kluge to avoid the problem of 
# an array of a single element showing a length of nothing
$dummy = MakeFieldRef "zzzzzzDummy_999"
$fieldRefs += $dummy
# Sort the array of field refs
$fieldRefs = $fieldRefs | sort-object RootTitle, Index
<# Now go through the array of objects and determine if there is a corresponding main field#>
for($i=0; $i -lt $fieldRefs.length - 1; $i++){
    $mainField = $web.fields[$fieldRefs[$i].RootTitle]
    # Does this main field exist?
    if(!$mainField) {
        # No - kill this shadow field
        $web.fields.delete($fieldRefs[$i].FieldTitle)
        write-host "Deleted " $fieldRefs[$i].FieldTitle
    } else {
        # Yes - we will kill this field if the *next* field ref has the same RootTitle
        $j = $i + 1
        if ($j -lt $fieldRefs.length -and $fieldRefs[$j].RootTitle -eq $fieldRefs[$i].RootTitle) {
            # The next field ref has the same root title, so we can delete this one
            $web.fields.delete($fieldRefs[$i].FieldTitle)
            write-host "Deleted " $fieldRefs[$i].FieldTitle
Leigh Webber

So can we kill these ones when they are in a collecting state? I thought you could.

Similar Messages

  • Stranger error:Old file not found.However,a file of the same name was found

    Hi All,
    I appologise if its not the right forum...
    I am a bit baffeled here by the following error when I am trying to install Java SDK or just JRE.
    I upgraded my windows 2000 box to win XP home edition, installed the latest updates etc and then tried to install java....
    and got the following error
    bin\appletviewer.exe:
    Old file not found.However, a file of the same name was found.No updates done since file contents does not match.
    I have removed everything from system that I could imagine might be using Java directly or indirectly. No JRE on the system, no old java folder under C: drive etc, But I still get the same error. I have never come accross a stupid error like this before. Any help will be appreciated. Thanks
    Message was edited by:
    Khalid.Ali

    hunmm..thanks for the reply, I will try to find appletviewer.exe, however, my guess is that file should be there cus I am just trying to install it from standard java SDK installation.
    Yes I did not do checkdisk and I will do that and post my findings

  • When i plug in ipod it tells me to rename again and when i do it tells me a duplicate name was made for it then it auto syncs gets to about 200ish songs then crashes

    Please help, my ipod is driving me mad!
    (though possibly my ipod)

    Was is the message you are prompted with when you plug the iPod Classic into your PC?  Let's start there for now.
    B-rock

  • Statechart module: Using a master statechart to launch a slave statechart more than once without getting Error 1100 at Obtain Queue (No object of that name was found)

    Hello,
    I am trying to create a project using the statechart module, and one of the techniques I am trying to use is a "master-slave" statechart structure, illustrated by the code I have attached to this post (you probably need the statechart module installed to look at my example code).  The problem I have recently discovered is that the slave statecharts cannot be launched more than once without raising an Error 1100 at Obtain Queue.  I have gathered from a few other posts that the cause of this error is the fact that the First Call primitive is used by the Statechart module to initialize the statechart when it is first run.  This works fine for the first launch of the slave statechart, but the second launch and subsequent launches of the statechart fail on Error 1100, since the First Call primitive won't let the statechart initialize (the First Call primitive tells the statechart it has already been initialized (this isn't the first call), and the External Trigger function then tries to send an external trigger to a queue that doesn't exist).
    What I can't gather from those posts is the correction I need to make to get this design to work.  I tried to send the slave statecharts a boolean flag for the "Init?" terminal, to force a reinitializing of the slave statechart.  I couldn't get that to work.  I tried to not load the slave sub VI as a slave sub VI, and launch it using some VI Server code, so that maybe the slave sub VI would be loaded and unloaded, therefore causing the statechart to properly initialize, but I couldn't get that to work either.  What correction should I make?  How do I get this to work?
    I really want to use this design pattern, first of all since I wrote a lot of code already with this structure, and second of all, it makes a lot of sense to me.  How do I make it work?  If someone could post a version of my code that functions the same, but without the error, I would appreciate that.  If you don't have time for that, but would be able to outline the steps I need to take to get it to work, that would be helpful, as well.
    Thanks to anyone for any assistance they can provide.  If you need any other information, please let me know.
    Attachments:
    Statechart Sample.zip ‏431 KB

    Thanks, Deborah.  I tried this approach out, and it turns out you can't just wire a TRUE to the "Init?" terminal.  You have to wire a TRUE for the first run of the slave statechart, and then it has to become a FALSE.  If you wire a TRUE, it will constantly try to reset the statechart, which doesn't work.
    The master statechart has to keep track of whether it has called the slave statechart, and communicate that to the slave statechart.  On the very first call of the slave statechart, the "First Call?" function inside of the "Run Statechart" function will give a TRUE, which initializes the statechart properly, and then gives a FALSE from that point after.  After that, the "First Call?" function doesn't work anymore (it will always return FALSE), and an initial TRUE, with subsequent FALSE values, has to be provided manually.
    I attached a version that does this, which appears to work.  Let me know if it doesn't work for someone, or if you have a better idea to implement the correction.
    Thanks to everyone for the replies!
    Attachments:
    Master-Slave Statechart Sample.zip ‏439 KB

  • Why does my iMac, running snow leopard, turn on by itself? I have looked for anything that might be causing this, but haven't found the solution.

    I have an an iMac that turns itself one everyday at the same time. I am trying to stop this but haven't found what's causing it. What is causing this?

    Check the schedule in the Energy Saver pane of System Preferences.
    (74319)

  • Itunes wont sync nano, error The iPod " _ _ _ " cannot be synced. A duplicate file name was specified.

    The iPod “ _ _ _ “ cannot be synced. A duplicate file name was specified.

    The solution i found to this problem is have windows hard drive recovery tool scan and repair the hard drive on you ipod. To do this, you need to configure itunes so it does not open when you plug in you ipod. Next, plug in your ipod, for windows 7 user a pop up should come up asking to scan and fix device. click yes and give it some time. after the tool in complete unplug you ipod, open itunes, plug your ipod in. if you still get the message, click restore, and start this process on more time. If you do not have windows seven or this pop-up does not appear manually go to you hard recover tool and have it scan your ipod. then follow the rest of the process.

  • When synching my ipod 80 gig classic, I get this message " iPod cannot be synched.  A duplicate file name was specified"  And yet, any new music in iTunes is added.  What causes this message?

    When synching my ipod 80 gig classic, I get this message " iPod cannot be synched.  A duplicate file name was specified"  And yet, any new music in iTunes is added.  What causes this message?

    Try deleting the iPod Photo Cache from the nominated folder that you have set up to sync photos to the devices.
    See iTunes: Understanding the iPod Photo Cache folder for more info.
    tt2

  • Every time i try to sync my iphone with my music on itunes, it says, "The iPhone "KORA" cannot be synced. A duplicate file name was specified."

    I need help

    Hi korasaurous,
    Thanks for visiting Apple Support Communities.
    You may find the troubleshooting steps in this article helpful with resolving the error message you're seeing:
    iTunes: Troubleshooting issues with third-party iTunes plug-ins
    http://support.apple.com/kb/ts3430
    There are several ways an add-on can cause iTunes to act erratically, such as those listed below:
    You see this alert while syncing: "... cannot be synced. A duplicate file name was specified."
    Best Regards,
    Jeremy

  • ITunes Library file cannot be saved. A duplicate file name was specified.

    Every time I open up iTunes I get the error 'The iTunes Library file cannot be saved. A duplicate file name was specified.'
    I've had this problem for a couple of months, tried out every solution I can find, changed the library names, recreated the library etc. Recreating the library seemed to work for a few days, but now the problem has returned. Because of this problem I can't add any new songs to my iTunes, and I cannot update any of my apps.
    Any help would be appreciated!

    I also have the same problem as in the duplicate file was specified thing after opening up itunes.
    But for me is the apps. I can't download or update my apps from my comp but i can do that on my iphone. I did tried to transfer purchases but it popped up about something that if i dont authorize this comp my apps will be removed. When i clicked dont authorize, it removed almost all my apps so i stopped in the middle of syncing. I click authorize and after entering my apple ID, another pop up saying that there is an error in transfering the purchases and told me to repair my itunes. I tried that too but it didnt work. I already did authorize my comp long ago. When i clicked cancel, it doesnt sync all my stuff and remains the same cause it always stuck at the apps. I tried to uninstall itunes but it says there's an error in the package thing, error code 2330. Any idea on what should i do? >_<

  • HT1414 the iphone software update server could not be contacted.a duplicate file name was specified , please help as this is driving me nuts,does anyone know which file it could possibly be ?

    Hi could anyone help me out? my iphone 3gs keeps restarting itself every two minutes which i think is some kind of software issue,now there are a number of things i have done starting with putting in a new battery.when i charge the phone it resets itself every two minutes or so and the battery indicator stays at 4% so i decided to restore the phone on itunes (which is the latest edition) and when i connect my phone which i have put into recovery mode using the buttons, i get an error message saying :- the iphone software update server could not be contacted.a duplicate file name was specified. i have tried re-installing i tunes and that doesn't solve the issue and i have trawled through the program files to see if i could spot the duplicate but without success ,has anyone had the same issue and resolved it?

    An iPhone 3G cannot be updated beyond iOS 4.2.1...that's the end of the line for your phone.

  • I am using itunes 10 and trying to consolidate my files.  I keep getting the error "Copying files failed.  The file name was invalid or too long".  How can I indentify what file is causing this problem or resolve this issue?

    I am using itunes 10 and trying to consolidate my files.  I keep getting the error "Copying files failed.  The file name was invalid or too long".  How can I indentify what file is causing this problem or resolve this issue?

    BUMP
    Yes, I just get that message. I don't see how I could investigate this problem.
    I didn't mention that this happened when I was consolidating my library, not copying files to another computer.
    In other words, I'm using a "normal" itunes procedure, itunes won't complete it, and won't tell me exactly why or how to figure out how to fix it...
    Is there at least some easy way to tell which files were successfully copied to my itunes music folder so I can work on moving the uncopied files?
    Can anybody help me?

  • Can't synch iPod classic to iTunes after updating iTunes. a duplicate file name was specified.

    I can't synch my iPod classic. "A duplicate file name was specified." I tried on two different computers on which it had previously worked fine.

    Try deleting the iPod Photo Cache from the nominated folder that you have set up to sync photos to the devices.
    See iTunes: Understanding the iPod Photo Cache folder for more info.
    tt2

  • Error "The IPod cannot be synched. A duplicate file name was specified"

    When I drag a song from my ITunes library to my Nano, an error pops up that says "The IPod cannot be synched. A duplicate file name was specified." I've read on other boards that I should delete the IToner bundle, but there is no IToner bundle in ITunes plug ins, just a Quartz Composer Visualizer. ANy suggestions on how to keep the error message from popping up every time I add a song?

    Hemberger wrote:
    When I drag a song *from my ITunes library* to my Nano,
    Drag the song from Library to the playlist window then drag it to Devices instead of directly from the Library.

  • On windows 8 and itunes 11 i try to sync my iphone and it says a duplicate file name was specified and won't let me sync, what do i do ?

    duplicate file name was specified...... windows 8 and itunes 11...... won't let me sync..... what do i do ?

    OK, read this thread... https://discussions.apple.com/thread/3928453
    tt2

  • I get an error message when trying to sync my reset ipod "The iPod ...cannot be synced. A duplicate file name was specified."

    i get an error message when trying to sync my reset ipod "The iPod ...cannot be synced. A duplicate file name was specified."

    Locate the photos folder which is selected for syncing with the device and delete the folder iPod Photo Cache inside.
    tt2

Maybe you are looking for

  • How can I improve my Macbook wi-fi?

    I have a 2009 Macbook. Is their any device or upgrade I can add to it to improve Facetime video call sessions? I'm not looking for inexpensive solutions. I need it improved for work. Somthing that might help with latency, speed or quality for when I'

  • Arch Linux running on Asus Transformer T100/T100TA... sort of.

    I'm not really asking for help here (can't find an appropriate place to put this post), but more to show off my accomplishment with this tablet. As the thread title says, I've gotten Arch Linux to run on the Asus T100TA which is a quite annoying litt

  • How to hide the Print and Export button in analytics report or tasks pane

    Hi Experts, In BIEE 11g, How to hide the Print and Export button in analytics report or tasks pane ? For example: In console,I have created one userA which is belong to BIConsumer GROUP , when I make use of the highest user 'weblogic' to create one s

  • QT Pro 7.2 not working in Vista Ultimate

    I've installed and registered QT 7.2 Pro, but, Quicktime does not work when clicking on items on the main home screen. I've looked for answers in QT Help to no avail. I've installed and run the QT program as administrator. In Vista control panel the

  • Win 7 on Win 8

    Hi everyone! I am fed up with windows 8 which is the reaso why I would like to install on my machine win 7, but, unfortunately, for me it seems almost impossible. I have got lenovo H520s low profile machine with pre-installed win 8. I can't even get