A client migrated from 2007 to 2010 doing a database detach/reattach without checking the database for orphaned objects first. I whipped up a PowerShell script to programmatically remove orphaned WebParts that generate "Missing server side dependencies" errors in CA Health Analysis like:

[MissingWebPart] WebPart class [GUID] is referenced [1] times in the database [ContentDBName], but is not installed on the current farm. Please install any feature/solution which contains this web part. One or more web parts are referenced in the database [ContentDBName], but are not installed on the current farm. Please install any feature or solution which contains these web parts.

When I try to remove a WebPart programmatically it tells me:

Exception calling "DeleteWebPart" with "1" argument(s): "The file is not checked out. You must first check out this document before making changes."
At C:\RemoveWebParts.ps1:47 char:31
+ $spWpManager.DeleteWebPart <<<< ($spWpManager.Webparts[$webpartId])
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException

So I implemented code to check out the page and check it back in after it's removed (if I checked it out) and now it's telling me:

Which obviously is an issue. When it's not checked out it says it has to be checked out, and when it is checked out it says it's checked out by another user (even though the SharePoint 2010 Management Shell is running as my user, and I can verify my user is the user the page is checked out to).

Are you running this code elevated? Is the same user used to check out as you use to delete? (yeah share code)
–
Anders RaskAug 6 '12 at 22:12

1

I edited the post to add the PowerShell code
–
RJ CuthbertsonAug 7 '12 at 12:58

@AndersRask I'm not running the code elevated (danlarson.com/…). And yes it is the same user. I verified that the PowerShell process is running as the same user that the page was successfully checked out to. The page was successfully checked in (probably should use Publish()) following the run. The WebPart doesn't get removed, it throw the errors above on $spWpManager.DeleteWebPart().
–
RJ CuthbertsonAug 7 '12 at 13:05

1

I usually check $file.CheckedOutByUser.Id and see if this matches $file.Web.CurrentUser.Id and throw an error ("not checked out by me"). If you need to Publish() or Approve() depends on what kind of publishing you are using. If you have enabled minor versions you need to publish ($list.EnableMinorVersions > $file.Publish("")) if you have enabled moderation you need to approve ($list.EnableModeration > $file.Approve(""))
–
Anders RaskAug 13 '12 at 13:51

@rjcup3 You're welcome! All search strings I added I always ended up here - so when my similar problem was solved, I just had to answer this question. Hopefully it'll help others stuck in the same problem we had.
–
Benny Skogberg♦Feb 7 '13 at 16:32