Blogging about daily challenges in SharePoint, PowerShell and Infopath
mandag den 15. juli 2013
Export/Copy list to another Farm/Site collection/Site with Powershell
We needed to publish 3 daily teasers from a member restricted area to a public site. The following powershell script exports a sharepoint list to a csv file:
cls
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}
$MyWeb = Get-SPWeb "Site"
$MyWeb.Lists["Name"].Items |
ForEach-Object{
New-Object PSObject -Property @{
Emne=$_["Emne"]
Kategori=$_["Kategori"]
Tekst=$_["Tekst"]
}
} | Export-Csv -path 'c:\file.csv' -NoTypeInformation -Encoding unicode
echo "Exporting done"
Now we have the exported items in a file, which we can import in to any other sharepoint location, where we have a list. The import list need to have the same columns as the exported list. Here is the import script:
clear
# Import the .csv file, and specify manually the headers, without column name in the file
$contents = Import-CSV ‘c:\file.csv' -header("Kategori", "Tekst","Emne")
# Web URL
$webURL = “Site”
$web = Get-SPWeb -Identity $webURL
$listName = "Name"
############ Delete all list items ################
$itemArray = $web.Lists["$listName"].Items | % { $_ }
$itemArray | % { $_.Delete() }
###################################################
Echo $listName
$list= $web.Lists["$listName"]
$i = 1
# Iterate for each list column
foreach ($row in $contents )
{
$item = $list.Items.Add();
$item["Emne"] = $row.Emne
$item["Kategori"] = $row.Kategori
$item["Tekst"] = $row.Tekst
$item["IdSort"] = $i++
$item["URL"] = "Link"
$item.Update()
}
Write-Host -ForegroundColor green "List Updated Successfully"
$web.Dispose()
IdSort and URL are added during the import, as we needed to control sorting and, where the items links.
Wrapped in a scheduled job, we now have "live update" from a member restricted area. This could also be done with a timer job or a custom webpart, but this powershell option is quick and cost effective.
Abonner på:
Kommentarer til indlægget (Atom)
Ingen kommentarer:
Send en kommentar