SharePoint 2010 + PowerShell: Export/Import Calendar Items
Today has been miserable. All I wanted to do was import a SharePoint 2007 calendar into SharePoint 2010 to use as sample data for a prototype site. I was caaarraaazzy enough to believe it would be a simple import/export, but no. There's no such thing. Maybe I could use Outlook calendaring or idq files as a medium? No. So I decided to do the good ol "Save this as a template and include content" workaround. Then I followed Tom's awesome instructions on how to hack the SharePoint 2007 template to get SharePoint 2010 to use it.
That worked! It imported over 3000 calendar items, but then the library itself was broken. When I'd make modifications (such as checking the box to make the calendar a resource), it would throw unknown errors IN MY FACE. So I decided I would just create a fresh SharePoint 2010 Calendar, then import the data using a PowerShell script which would just copy list item data from one calendar to another. This should work for almost any list in SharePoint. Here's how I did it, y'all:
First, create a new Calendar using the regular Calendar template. Then, run this script after changing $siteURL, $sourceListName and $destListName to suit your environment.
$siteURL = "https://sharepoint/" $sourceListName = "Corporate Calendar" $destListName= "Prototype Calendar"
$site = Get-SPSite $siteURL $web = $site.RootWeb $sourceList = $web.Lists[$sourceListName] $destList = $web.Lists[$destListName]
$columns = $sourceList.Fields; $sourceItems = $sourceList.GetItems();
foreach($sourceItem in $sourceItems) { $newItem = $destList.AddItem(); foreach($column in $columns) { if ($column.ReadOnlyField -eq $False -and $column.InternalName -ne "Attachments") { $newItem[$($column.InternalName)] = $sourceItem[$($column.InternalName)]; } } $newItem.Update(); }
$web.dispose() $site.dispose() Note that I didn't include support for attachments at this time; 8 hours after starting on this Copy Calendar Events journey began, I'm just too beat! Perhaps I'll add it later.
Update: My awesome friend Trevor shared this link with me: Create random or demo SharePoint Content with PowerShell Cool.