vendredi 19 décembre 2014

how to archive sharepoint 2013 list items using powershell



I have a list where user drop there request for new site within my farm. Request goes through workflow where i have 2 outcomes approved and rejected. If item is approved it goes though other approval process, but once it gets rejected approval status is updated to rejected. So i have powershell scripts running in background that kicks in every 15 minutes to see if there are any stuff whose approval status is rejected. if it is it copies that item into another list( archivedlist) and deletes the existing item. Powershell script works partially, where i need your help. At this point i can only copy one item at a time. Here is my script:



Remove-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
try{
# Source Initialization
$srcListSiteUrl = ".../sites/spservices"
$SourceListName = "Site Requests"
# Destination Initialization
$dstListSiteUrl = ".../sites/spservices"
$DestinationListName = "Sprequests_Archived"
$sourceListWeb = Get-SPWeb -identity $srcListSiteUrl
$sourceListUrl = $sourceListWeb.ServerRelativeUrl + "/lists/" + $SourceListName;
$dstListWeb = Get-SPWeb -identity $dstListSiteUrl
$destinationListUrl = $dstListWeb.ServerRelativeUrl + "/lists/" + $DestinationListName;
$SourceList = $sourceListWeb.GetList($sourceListUrl);
$DestinationList = $dstListWeb.GetList($destinationListUrl);
#Select Only Whose Approval Status is Rejected
$camlQuery =
"<Where>
<Eq>
<FieldRef Name='Approval_x0020_Status' />
<Value Type='Choice'>Rejected</Value>
</Eq>
</Where>"
$spQuery = new-object Microsoft.SharePoint.SPQuery
$spQuery.Query = $camlQuery
$sourceSPListItemCollection = $SourceList.GetItems($spQuery);
Write-Host "Archiving " $sourceSPListItemCollection.Count "Many Items"
#Loop Through List Item Collection
foreach ($srcListItem in $sourceSPListItemCollection)
{
write-host "copying Rejected item"
$newSPListItem = $DestinationList.AddItem();
$newSPListItem["Site Url"]=$srcListItem["Site Url"]
$newSPListItem["Site Type"]=$srcListItem["Site Type"]
$newSPListItem["Site Purpose"]=$srcListItem["Site Purpose"]
$newSPListItem["RC Number"]=$srcListItem["RC Number"]
$newSPListItem["Business Owner"]=$srcListItem["Business Owner"]
$newSPListItem["Primary Site Collection Administrator"]=$srcListItem["Primary Site Collection Administrator"]
$newSPListItem["Secondary Site Collection Administrator"]=$srcListItem["Secondary Site Collection Administrator"]
$newSPListItem["Approval Status"]=$srcListItem["Approval Status"]
$newSPListItem["ExpirationDate"]=$srcListItem["ExpirationDate"]
$newSPListItem["CompleteURl"]=$srcListItem["CompleteURl"]
write-host " updating the items"
$newSPListItem.Update()
write-host " Deleting from original list"
$srcListItem.Delete()
}
}
catch
{
write-host $_.exception
}
finally
{
if($sourceListWeb -ne $null){$sourceListWeb.Dispose()}
if($dstListWeb -ne $null){$dstListWeb.Dispose()}
}







0 commentaires:

Enregistrer un commentaire