#Purpose : Script to Populate Workflow history Column for an Item from the hidden workflow history list.
#Enviornmnets : SharePoint 2010, SharePoint 2013 (Change the Add-Type -Path to point to 15 folder).
#Author : Isha Kapoor
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\14\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\14\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\14\ISAPI\Microsoft.Online.SharePoint.Client.Tenant.dll"
$BatchSize="1000"
# Add path for your SP2010 site.
$siteUrl = "{yoursiteurl}"
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$ctx.Load($ctx.Web.Lists)
$ctx.Load($ctx.Web)
try
{
$ctx.ExecuteQuery()
$_lists = $ctx.Web.Lists
#Get Items from the Document Library. Change the library name below if running on another library.
$_list = $_lists.GetByTitle("Documents")
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = @"
$BatchSize
"@
Do
{
$items = $_list.GetItems($Query);
$ctx.Load($_list);
$ctx.Load($items);
$ctx.ExecuteQuery()
Write-Host "Retreving Number of Items : " $items.Count + "from Source List with Id : " + $_list.Id -foregroundcolor green;
foreach($listItem in $items)
{
#Run a query to get WF items related to the above List ID and Item Id
$_WFlist = $_lists.GetByTitle("Workflow history");
$_listID = $_list.Id.ToString().TrimEnd();
$ItemID = $listItem["ID"];
Write-Host "Retriving Workflow History for Item ID : " $ItemID;
$Query1 = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query1.ViewXml = @"
$_listID
$ItemID
"@
$wfitems = $_WFlist.GetItems($Query1)
#Load the Workflow list items in current context
$ctx.Load($_WFlist);
$ctx.Load($wfitems);
$ctx.ExecuteQuery()
Write-Host "Retrived Number of Matching Items (by List Id) from WF history list : " $wfitems.Count -foregroundcolor green
Write-Host "Generating Workflow History Array";
$WFvariable = "";
foreach($WflistItem in $wfitems)
{
Write-Host "Updating value for Workflow History field";
$WFvariable = "OutCome = " $WflistItem["Outcome"] + "Desciption : " + $WflistItem["Description"] + "\n";
}
#Updating Original Item with WF history
$listItem["WorkflowHistory"] = " ";
$listItem["WorkflowHistory"] = $WFvariable;
$listItem.Update();
$Ctx.ExecuteQuery();
Write-Host "Item Updated."
}
$Query.ListItemCollectionPosition = $items.ListItemCollectionPosition
}
While($Query.ListItemCollectionPosition -ne $null)
}
catch
{
$ErrorMessage = $_.Exception.Message
write-host "Error : " $ErrorMessage -foregroundcolor red
}