lundi 12 janvier 2015

How To Copy List Item Object Using Powershell



Given the powershell snippet below I am trying to complete the last part which should copy the list item object from the source list to the destination list. This is for archiving purposes and powershell is the only option in my case.



try
{
# OUTPUT TO SCREEN THE CURRENT SPWEB.
write-host "Working on Web: "$_.Title -ForegroundColor Green

$numDaysToExpire = $ExpireDays
$expireDate = Get-Date
$expireDate = $expireDate.AddDays(-$numDaysToExpire)

# FIND THE LIST MATCHING THE $LISTNAME
$sList = $spweb.GetList($spweb.Url + "/Lists/" + $SourceList)
$dList = $spweb.GetList($spweb.Url + "/Lists/" + $DestList)

#CHECK IF THE LIST OBJECT EXISTS
if ($sList)
{
# OUTPUT TO SCREEN THE CURRENT LIST.
write-host " Working on List: "$sList.Title -ForegroundColor Cyan
#LOOP THROUGH EACH ITEM IN SOURCE LIST
$spSourceItems = $sList.Items | where {$_['Created'] -lt $expireDate}

foreach($item in $spSourceItems){
if($dList)
{
# OUTPUT TO SCREEN THE CURRENT ITEM.
write-host " Copy $($item["Name"]) with created date $($item["Created"])"
# COPY THE ITEM TO THE DESTINATION ARCHIVE LIST AND DELETE THE EXISTING ITEM IN THE SOURCE LIST.

}
}

}
}
catch [Exception]
{
write-host (" Error: " + $_.Exception.ToString()) -foregroundcolor red
}


I have seen multiple examples where the columns like 'Title' and 'Created' are specifically referenced when copying a list item but how can I copy the full list item object all of the column names may not be known?








0 commentaires:

Enregistrer un commentaire