Import-SPWeb Error with Minor Versions in Document Library

When importing a SharePoint document library using Import-SPWeb, you might get an error message similar to this one:

Verbose: Warning: Cannot import version 0.1 in the current package.
Error: Item does not exist. It may have been deleted by another user.
Debug: at Microsoft.SharePoint.SPListItem.EnsureItemIsValid()
 at Microsoft.SharePoint.SPListItem.PrepareItemForUpdate(SPWeb web, Boolean bMigration, Boolean& bAdd, Boolean& bPublish, Object& objAttachmentNames, Object& objAttachmentContents)
 at Microsoft.SharePoint.Deployment.ListItemSerializer.AddOrUpdateDoclibItemVersion(SerializationInfoHelper infoHelper, SPListItem& listItem, SPLoggerObject loggerObject, SPWeb web, Guid newId, String& listItemServerRelativeUrl, Boolean bIsPublish, Boolean exists, String version, Boolean isFirstVersion, Boolean isLastVersion, StreamingContext context, ISurrogateSelector selector)
 at Microsoft.SharePoint.Deployment.ListItemVersionSerializer.AddListItemVersion(SPWeb web, SPListItem listItem, SPLoggerObject loggerObject, Guid newId, Boolean editHistory, Boolean existsInDb, Boolean isFirst, Boolean isLast, Boolean isDocLib, StreamingContext context, XmlElement listItemData, SerializationInfoHelper listItemInfoHelper, String& listItemServerRelativeUrl, ISurrogateSelector selector)
 at Microsoft.SharePoint.Deployment.ListItemSerializer.UpdateListItemVersionData(SerializationInfoHelper infoHelper, SPWeb web, SPListItem& listItem, Guid newId, Boolean existsInDb, Boolean isDocLib, String& listItemServerRelativeUrl, StreamingContext context, ISurrogateSelector selector)

Explanation

A good explanation for this behavior can be found here: STSADM Import error

The problem arises from exports of libraries with library settings that restricts the number of minor versions. All documents that were not initially published from the 0.1 but from a higher minor version suffer from a bug in SharePoint.
Reason: A library document in object model exists by a File and an Item object. The routine that deletes the minor version when the setup limit is reached correctly deletes the file object but does forget about the item object. This results in a warning like ‘Verbose: Warning: Cannot import version 0.x …’ for all removed minor versions which isn’t too bad, import continues working. But especially for the minor version 0.1 it results in the discussed Exception ‘Error: Item does not exist. It may have been deleted’.
We found no easy way out here since this document’s data set is corrupted. So we wrote a Fix-Up program using the SP API that seeks for such corrupted data sets in the library and removes the invalid item sets.

As the problem resides within the backup or export file, respectively, you need to fix the error prior to exporting the library.

Solution

Here is a small PowerShell code example that will delete all draft versions of older major versions.

Afterwards, export the library again and the next import will (hopefully) not contain any errors.

Like what you read? Give Markus Kolbeck a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.