0

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 when all of the column names may not be known?

1

Assuming you have same destination fields with same type as in source list following PowerShell will solve the purpose...

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}
        $sourceSPFieldCollection = $sList.Fields;

        foreach($item in $spSourceItems){
            if($dList)
            {
                $newSPListItem = $dList.AddItem(); 
                foreach($spField in $sourceSPFieldCollection) {      
                # At first check fields that are not read only and attachments      
                    if ($spField.ReadOnlyField -ne $True -and  $spField.InternalName -ne "Attachments") {
                        # Store value in new SPListItem object        
                        $newSPListItem[$($spField.InternalName)] = $item[$($spField.InternalName)];     
                    } 
                } 
                # Handle Attachments 
                foreach($leafName in $item.Attachments) {   
                    $spFile = $sList.ParentWeb.GetFile($($item.Attachments.UrlPrefix + $leafName));  
                    $newSPListItem.Attachments.Add($leafName, $spFile.OpenBinary()); 
                } 
                    # Update Item 
                    $newSPListItem.Update(); 
            }
            # 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
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.