We had sharepoint list with 10,000+ items. We need to move (or) archive items to another list which are created by one year back. Means items need to move if created date is less than (or) equal to 30/04/2018. So Archive list have last year items data and current list should have items which are created after 01\05\2018 to present date. I am new to SharePoint, could anyone please suggest or share powershell script (or) SharePoint designer to achieve my request.
2 Answers
I can share with you powershell script that moves listitems created more than one week ago to another list.
You need to correct variables and $filterQuery to make query based on specific date.
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
try
{
$srcListSiteUrl = "http://WEB_URL"
$SourceListName = "SourceListName"
$dstListSiteUrl = "http://WEB_URL"
$DestinationListName = "DestinationListName"
$keyColumnInternalName = "ID"
$sourceListWeb = Get-SPWeb -identity $srcListSiteUrl
$sourceListUrl = $sourceListWeb.ServerRelativeUrl + "/lists/" + $SourceListName;
$dstListWeb = Get-SPWeb -identity $dstListSiteUrl
$destinationListUrl = $dstListWeb.ServerRelativeUrl + "/lists/" + $DestinationListName;
Write-Host getting source list
$SourceList = $sourceListWeb.GetList($sourceListUrl);
Write-Host getting destination list
$DestinationList = $dstListWeb.GetList($destinationListUrl);
#Filtering Items based on the Date as we do not want to archive all items in the list
$filterQuery = '<Where>
<Leq>
<FieldRef Name = "Created" /> <Value IncludeTimeValue="TRUE" Type="DateTime">' +(Get-Date).AddDays(-7).ToString('yyyy-MM-dd')+ '</Value>
</Leq></Where>'
$CategoryQuery = new-object Microsoft.SharePoint.SPQuery
$CategoryQuery.Query = $filterQuery
$sourceSPListItemCollection = $SourceList.GetItems($CategoryQuery);
Write-Host Total Items to Copy : $sourceSPListItemCollection.Count
foreach($srcListItem in $sourceSPListItemCollection)
{
$keyValue = $srcListItem[$keyColumnInternalName]
Write-Host $keyValue
write-host "adding new item"
$newSPListItem = $DestinationList.AddItem();
foreach($spField in $DestinationList.Fields)
{
if ($spField.ReadOnlyField -ne $True -and $spField.InternalName -ne "Attachments" )
{
$newSPListItem[$spField.InternalName] = $srcListItem[$spField.InternalName];
}
}
# Handle Attachments
foreach($leafName in $srcListItem.Attachments)
{
$spFile = $SourceList.ParentWeb.GetFile($srcListItem.Attachments.UrlPrefix + $leafName)
$newSPListItem.Attachments.Add($leafName, $spFile.OpenBinary());
}
$newSPListItem.Update()
write-host "deleting old item"
$listItemToDelete = $SourceList.GetItemById($srcListItem.ID)
$listItemToDelete.Delete()
}
}
catch
{
write-host $_.exception
}
finally
{
if($sourceListWeb -ne $null){$sourceListWeb.Dispose()}
if($dstListWeb -ne $null){$dstListWeb.Dispose()}
}
-
thanks Raf, i tried your code. Items are archiving to destination list. But Title column, getting update with number as 1_, 2_, 3_. I think its fetching (or) updating ID number. Could you please suggest me. Note: My title column was empty for some items on the list. May 6, 2019 at 8:29
-
Please check title column settings. Be sure that parameter 'Enforce unique values' is set to 'No'. And parameter 'Require that this column contains information' is set to 'No' too. In both(source and destination lists).– RafMay 6, 2019 at 9:24
-
both option set NO already. Still same issue coming. Title column updating with number as 1_,2_,3_ etc. Please suggest May 6, 2019 at 12:20
-
-
one more issue coming, after archiving, we are unable to edit items Destination list. Getting error some thing went wrong message. May 6, 2019 at 12:35
You can create a SharePoint Designer workflow to copy item data from a list to another then delete the item in the original list.
Then use Information Management Policy-retention policy to trigger the workflow after [Created]+ 1 years. How to trigger a workflow on a timer basis: Information management policies