SharePoint ListItem disappears after moving it to another list using .MoveTo

655 Views Asked by At

So im trying to move a SPListItem from a list to another archive List. Here is my code

$Query = New-Object Microsoft.SharePoint.SPQuery;
$Query.Folder = $FolderToMoveFrom

ForEach ($Item in $ItemList.GetItems($Query))
{
    $a = $web.Url;
    $b = $FolderToMoveTo.Url;
    $c = $a + "/" + $b;
    $Name = $Item.Name;
    write-host "Copy Item " $Name "to" $c;

    $Web.GetFile($Item.Url).MoveTo([System.String]::format("{0}/{1}_.000",$FolderToMoveTo.Url,$Item.ID.ToString())); 
}

My Problem

After running the script, the Items just disappears. My Script doesnt throw an exception. I think the problem might be, that the list has to be updated, after adding new items to it. But if I add $Item.Update(); in the ForEach, I get the excpetion "Entry does not exist" (which is understandable, because I just moved the Item).

So my question is, how can I update the archive list correctly, so my list shows the Items I just moved?

1

There are 1 best solutions below

0
ronx On

I recently ran into the same problem. I don't think you included your complete code snippet since it isn't clear where FolderToMoveTo comes from (different list?), but since I experienced similar symptoms (list items disappearing when using MoveTo), I figured I'd post my experience and solution in case it helps anyone else or gets others to notice and post better solutions.

I have a custom list with thousands of list items organized into folders. However, for reporting purposes, users end up creating flat views and filtering on lookup columns, so the organizing into folders does not help get around the list item threshold for these reports. I wanted to archive list items meeting certain criteria (in my case, they were already organized into subfolders /Topic/Year, so I decided to write some PowerShell to iterate through the SPList.Folders collection of the source list and, for testing, move any folder named "2013" to the same folder structure in the target list.

This blog post ultimately led me to the solution. When I tried to use MoveTo to move the item from the source list to the target list, the item disappeared (orphaned? I couldn't seem to find it even with SPM). I ended up having to create a new item in the target list, copy all writable field values from the source item and call $item.Update(), move the new item into the correct location with MoveTo (no Update, SystemUpdate or any other call required for this), and then delete the source item.

Let me know if code sample would help, but I think the link above should cover it all. All credit to the blog author, Stian Kirkeberg, and the sources he references. I'm just increasing exposure since I found this question on SO before finding his solution.