Microsoft 365 : SharePoint Online – Add Events in a SharePoint online site through PowerShell.

.
“There is a kind of beauty in imperfection.”
.
Hello Everyone,
Hope you all are doing well.
In this article we are going to discuss how to add events in a SharePoint online site from CSV file through PowerShell.
We have good list of articles on SharePoint Online using PowerShell, please have a look.
So without getting late, let’s get started.
Background
In our organization, one of our project requirement is to add an event on every month/week/year basis. Too many events to add manually and also time taking process. So we are using PowerShell script to add all the events at a time from a csv file.
Details
- We need to prepare the events list in a CSV format and saved in a preferred location.
- We can use Microsoft Excel , to prepare the events list, then save as CSV.
- CSV file with headings and details to add the events.

- Open PowerShell ISE.
- Now ran the PowerShell with the following cmdlet to add the events.
Note: The line starting with “#” are just description heading or comment.
Prerequisites
- Install PowerShell ISE / PowerShell / Visual Studio Code if its not installed.
- Install SharePoint Online Management Shell if its not installed.
- Prepare CSV file with required details to create site columns.
#Install SharePoint Online Management Shell if its not installed
Install-Module -Name Microsoft.Online.SharePoint.PowerShell
Detailed Steps – PowerShell script
- Import SharePoint online Client and Client Runtime libraries and update path for the respective libraries.
#Import SharePoint Client and Client Runtime libraries - Update your path for the respective libraries
Import-Module 'C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll'
Import-Module 'C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll'
- Give the URL of site where site columns to be create.
#Site collection URL where we need to create the site columns
$siteurl = "https://osinfotech.sharepoint.com/sites/Forms" # site collection URL
- Give the credentials of Office 365 / Microsoft 365.
#Give the User name and Passwords
$userName ="khasims@osinfotech.onmicrosoft.com" # user name
$password ="XXXXXXXXXX" # Password
- Create the site URL object.
#Create the site url object
[Microsoft.SharePoint.Client.ClientContext]$cliCnt = New-Object Microsoft.SharePoint.Client.ClientContext($siteurl)
- Convert the password into secure string.
#convert password into secured string
$securedpw = ConvertTo-SecureString $password -AsPlainText -Force
- Create the credentials object.
#Create the credentials object
$clientContext.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $securedpw)
- Use a variable for the events list
#Variable for Events list
$CalendarName = "Events"
- Import the CSV file of events.
#Import the CSV file
$ImportFile ="E:\PowerShellScripts\addevent.csv"
- Getting the Events list from the SharePoint Online
#Get the Events list from SharePoint Online
$CalendarList = $Ctx.web.lists.GetByTitle($CalendarName)
- Loading the events list from SharePoint
#Load the events list
$Ctx.load($CalendarList)
- Executing the query
#Execute the query
$Ctx.ExecuteQuery()
- Use a variable for CSV file data
#Variable for CSV file data
$data = Import-Csv $ImportFile
- Apply for each field in the CSV file.
#Apply on each row of the CSV file
Foreach ($row in $data)
{
- We need to find any errors happened while adding the events on SharePoint Online Site.
- So we are applying loop try to add the events on the respective site.
#Add the respective events
Try{
- Adding event list information in a variable
#Adding event list information in a variable
$ListCreationInfo = New-object Microsoft.SharePoint.Client.ListItemCreationInformation
- Variable for the list item.
#Variable for event details
$ListItem = $CalendarList.AddItem($ListCreationInfo)
- Adding the event details on SharePoint.
#Add the event details
$ListItem["Title"] = $row.Title
$ListItem["Description"] = $row.Description
$ListItem["Location"] = $row.Location
$ListItem["EventDate"] = $row.EventDate
$ListItem["EndDate"] = $row.EndDate
$ListItem["Category"] = $row.Category
$ListItem["fAllDayEvent"] = $row.fAllDayEvent
- Update the list item on SharePoint.
#Update the events list item
$ListItem.Update()
- Executive the query on SharePoint.
#Execute the query on events list
$Ctx.ExecuteQuery()
- Display the details of events created successfully.
#Print the event success details
Write-host "Calendar Event Added Successfully!" -f Green
- Here we end the try loop.
- Now we need to identify the errors.
- So we apply the catch loop.
#Catch the errors
Catch {
- Display the errors.
#Print the error details
write-host -f Red "Error Adding Calendar Item!" $_.Exception.Message
- Save the errors in a csv file and continue
#Add the error details in a CSV file and continue adding another event.
Add-Content -Path "E:\PowerShellScripts\errorslog.csv" -Value $_
continue
Complete PowerShell Script
<#
===================================================================================================================================================
Name: Add bulk events in a SharePoint online site through PowerShell
Description: This script helps to add bulk events in SharePoint online site in an Organization through PowerShell
Version: 1.0
===================================================================================================================================================
#>
#Install SharePoint Online Management Shell if its not installed
#Install-Module -Name Microsoft.Online.SharePoint.PowerShell
#Import SharePoint Client and Client Runtime libraries - Update your path for the respective libraries
Import-Module 'C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll'
Import-Module 'C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll'
#Site collection URL where we need to create the site columns
$siteurl = "https://osinfotech.sharepoint.com/sites/Forms" # site collection URL
#Give the User name and Passwords
$userName ="khasims@osinfotech.onmicrosoft.com" # user name
$password ="xxxxxxxxxx" # Password
#Create the site url object
[Microsoft.SharePoint.Client.ClientContext]$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteurl)
#Convert password into secured string
$securedpw = ConvertTo-SecureString $password -AsPlainText -Force
#Create the credentials object
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $securedpw)
#Variable for Events list
$CalendarName = "Events"
#Import the CSV file
of events
$ImportFile ="E:\PowerShellScripts\addevent.csv"
#Get the Events list from SharePoint Online
$CalendarList = $Ctx.web.lists.GetByTitle($CalendarName)
#Load the events list
$Ctx.load($CalendarList)
#Execute the query
$Ctx.ExecuteQuery()
#Get the Data from CSV and Add to SharePoint List
$data = Import-Csv $ImportFile
#Apply on each row of the CSV file
Foreach ($row in $data)
{
#Add the respective events
Try{
#Adding event list information in a variable
$ListCreationInfo = New-object Microsoft.SharePoint.Client.ListItemCreationInformation
#Variable for event details
$ListItem = $CalendarList.AddItem($ListCreationInfo)
#Adding the event details
$ListItem["Title"] = $row.Title
$ListItem["Description"] = $row.Description
$ListItem["Location"] = $row.Location
$ListItem["EventDate"] = $row.EventDate
$ListItem["EndDate"] = $row.EndDate
$ListItem["Category"] = $row.Category
$ListItem["fAllDayEvent"] = $row.fAllDayEvent
#Update the events list item
$ListItem.Update()
#Execute the query on events list
$Ctx.ExecuteQuery()
#Print the event success details
Write-host "Calendar Event Added Successfully!" -f Green
}#try
#Catch the errors
Catch {
#Print the error details
write-host -f Red "Error Adding Calendar Item!" $_.Exception.Message
#Add the error details in a CSV file and continue adding another event.
Add-Content -Path "E:\PowerShellScripts\errorslog.csv" -Value $_
continue
}#catch
}
#foreach
.
Hope this article will help us to add events in a SharePoint Online site from CSV file through PowerShell.
Also get my article updates on my social media handles.
LinkedIn – https://www.linkedin.com/in/khasim-shaik-8784a1232/
Twitter – https://twitter.com/KhasimShaik2009
Facebook – https://www.facebook.com/profile.php?id=100078255554660
Thank you for your support, will catch up with new article soon.
Keep learning and keep smiling.
Thanks.
You must log in to post a comment.