Monday, November 30, 2015

Update the Document library item from CSV using PowerShell

Below script will update a particular item in the page library. In this example the created date will be updated from the source csv, if the filename matches in both CSV and the page library

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) { 
Add-PSSnapin "Microsoft.SharePoint.PowerShell" 

$web = get-spweb http://mydomain.com/authoring
$list = $web.Lists["Pages"] -as [Microsoft.SharePoint.SPDocumentLibrary]

$delimeter = "~"
$filePath = "C:\source.csv"
$exceldatas = Import-Csv $filePath -Delimiter $delimeter
$cnt = 0
foreach ($item in $list.Items)
{
    $url = [String]::Format("{0}{1}",$web.Url + "/", $item.File.Url)
    $file = $web.GetFile($url)   
    foreach($exceldata in $exceldatas) 
If($item.Name -eq $exceldata.Name)
$file.CheckOut()
$fileItem = $file.Item
$fileItem["Created"] = $exceldata.Created
$fileItem.Update()
$file.CheckIn("Updated Created Date")
$file.Publish("Publish through Powershell")
$cnt = $cnt + 1
Write-Host "Please wait item " $item.Name " is getting updated...."
}
    }
}
Write-Host "Update compelted for " $cnt " Items....."

No comments:

Post a Comment