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 = "http://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.

Chrissy is a Cloud and Datacenter Management & Data Platform MVP who has worked in IT for over 20 years. She is the creator of the popular SQL PowerShell module dbatools, and holds a number of certifications, including those relating to SQL Server, Linux, SharePoint and network security. You can follow her on Twitter at @cl.

Posted in PowerShell, SharePoint
2 comments on “SharePoint 2010 + PowerShell: Export/Import Calendar Items
  1. Jim says:

    You're only hope is to import list items if you want full 2010 functionality after importing a 2007 calendar BECAUSE – 2010 does not support 2007 templates. Period. Try the above mentioned cab to stp hack to create a calendar in a 2010 site and then attempt to make a group calendar out of it. It will create the calendar with the content, everything will look good, then you try to navigate off the settings page – Ka-BOOM! Import the list items or upgrade the site. Those are your options.

  2. Collabr8 says:

    Right! Not so simple – I thought the same!

Leave a Reply

Your email address will not be published. Required fields are marked *

*