SharePoint Online: Add Calendar Item using PowerShell
Requirement: Add Calendar event in SharePoint Online using PowerShell.
PowerShell to Add New Item in SharePoint Online Calendar
PowerShell is a powerful scripting language that can automate many tasks in SharePoint Online. Let’s add events to the SharePoint Online calendar using PowerShell:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing/"
$CalendarName = "Team Calendar"
Try {
#Setup Credentials to connect
$Cred= Get-Credential
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Get the Calendar list
$CalendarList = $Ctx.web.lists.GetByTitle($CalendarName)
$Ctx.load($CalendarList)
$Ctx.ExecuteQuery()
#Add Calendar Item
$ListCreationInfo = New-object Microsoft.SharePoint.Client.ListItemCreationInformation
$ListItem = $CalendarList.AddItem($ListCreationInfo)
$ListItem.ParseAndSetFieldValue("Title", "Townhall Meeting")
$ListItem.ParseAndSetFieldValue("Description", "Global Townhall Meetings - Yearly schedule")
$ListItem.ParseAndSetFieldValue("Location", "Ballroom")
$ListItem.ParseAndSetFieldValue("EventDate", "01/01/2019 09:00 AM")
$ListItem.ParseAndSetFieldValue("EndDate", "01/01/2019 06:00 PM")
$ListItem.Update()
$Ctx.ExecuteQuery()
Write-host "Calendar Event Added Successfully!" -f Green
}
Catch {
write-host -f Red "Error Adding Calendar Item!" $_.Exception.Message
}
To Set “All Day Event”, set flag “fAllDayEvent” = $true;
PnP PowerShell to Add Calendar Event in SharePoint Online
PowerShell can be a great option if you are looking for a way to add events to your SharePoint online calendar quickly! Here is the PnP PowerShell version to add an entry to a Calendar:
#Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing/"
$CalendarName = "Team Calendar"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)
#Add Event to Calendar
Add-PnPListItem -List $CalendarName -Values @{"Title" = "Team Meeting"; "Description"= "Monthly Team Meeting"; "Location"= "Ballroom"; "EventDate" = [datetime]"01/01/2019 8AM"; "EndDate"=[datetime]"01/01/2019 6PM";}
Hello!
I am trying to retrieve all meetings from the calendar and it shows me the wrong time (2 hours off) while web version shows the right time. Any ideas?
thanks!
That is because, SharePoint stores Date-Time values in UTC format, and it applies the site’s timezone on top of it to adjust and display when you view it in the browser.