Monday, November 2, 2015

SharePoint 2013 TIP: updating list columns and showing progress

Here's a quick bit of PowerShell that saves the Created and Created By columns to CreatedArchive and CreatedByArchive and also at the same time presented to you a progress bar.  I'm saving these columns data because:

  1. The Created column is overwritten when you perform an export followed by an import and 
  2. The Created By column is overwritten by your username (assuming you are the one performing this import) if the particular username being imported is no longer in Active Directory.  

The loss of that data presents a problem when trying to archive lists for which it is important to preserve these column data, such as in employee time and issue tracking lists.  These lists can be thousands of items long, and it can take several minutes or more to update them and thus it's helpful to have a progress bar.  Using a progress bar gives me instant feedback on how my PowerShell script is progressing.


$SourceWebURL = "http:/[yourfarmURL]/yoursite" $SourceListName = "Time log" $SourceWeb = Get-SPWeb $SourceWebURL $SourceList = $SourceWeb.Lists[$SourceListName] $total=$SourceList.Items.count $count=1 $Updated = $FALSE $ItemsUpdated = 0 ForEach ($Item in $SourceList.Items) { $percent = [math]::Round(($count/$total)*100,2); Write-Progress -Activity 'Updating Electronic Attendance Log ArchivedUsername field' -Status "$percent percent" -PercentComplete $percent; $cb = $Item["Created By"]; $cba = $Item["CreatedByArchive"]; $c = $Item["Created"]; $ca = $Item["CreatedArchive"]; If (($cba -eq "") -OR ($cba -eq $NULL)) { $cb = $cb.SubString($cb.IndexOf("#")+1); $Item["CreatedByArchive"] = $cb; $Item.Update(); $Updated = $TRUE; }; If (($ca -eq "") -OR ($ca -eq $NULL)) { $Item["CreatedArchive"] = $c; $Item.Update(); $Updated = $TRUE; }; If ($Updated) { $ItemsUpdated = $ItemsUpdated + 1; $Updated = $FALSE; }; $count = $count+1; }; $count=1; Write-Host $ItemsUpdated

No comments: