Friday, July 21, 2017

Automatically add entries to a SharePoint calendar

I created a pretty cool script today to prebook slots in a SharePoint calendar.

This could be handy if you want to use a SharePoint calendar for a booking system, like appointments or room bookings.


#################################################################
#
#
#     Sharepoint-AddCalendarBookings.ps1
#
#
#     This script will add a set of calendar entries to a sharepoint calendar
#     with a status of Free. The intention is to use this calendar as a booking system
#     for appointments
#
#     It will, by default, add it for next month. Change the addmonths variable to
#     a bigger number to affect a different month in the future (next month = this month + 1)
#
#     It will, by default, book 15 minute slots. Change the $a variable to increase this value
#
#################################################################

#Variables
$webURL = "http://sharepointwebsiteaddresshere" 
$listName = "ListNamehere" 

$addmonths = 3 # number of months to add to calculation

$a = 15 # duration of the appointment

$StartTime = "09:00:00" # time of first appointment
$NumAppt = "27" # number of appt for day (9-4 is 8 hours, 8*60/15 = 28, less one slot is 27) if you change length of appt you will need to change this value

# end of variables

# there should be no need to update the script beyond this point


#Add SharePoint PowerShell Snapin which adds SharePoint specific cmdlets 
Add-PSSnapin Microsoft.SharePoint.PowerShell
  
#Get the SPWeb object and save it to a variable 
$web = Get-SPWeb $webURL 

#Get the SPList object to retrieve the "Demo List" 
$list = $web.Lists[$listName]

# get today's date
$today = get-date

# get next month
$nextmonth = $today.AddMonths($addmonths)

# get the month and year value for next month
$month = ($nextmonth).Month
$year = ($nextmonth).Year


# now get the number of days in the month

$eom  = [datetime]::DaysInMonth($year,$month)


# now, lets loop through the number of days in the month
foreach ($n in 1..$eom) {

$date = get-date "$month/$n/$year $StartTime"

$day = ($date).DayOfWeek.value__

# make sure we don't book week-ends
if ($day -ne 0 -and $day -ne 6){

foreach ($q in 0..$NumAppt) {
   $i = $a*$q

$timeslot = (get-date $date).AddMinutes($i)


#Create a new item 
$newItem = $list.Items.Add() 
 
#Add properties to this list item 
$newItem["Title"] = "Appointment Open Slot"
$newItem["Category"] = "Appointment"
$newItem["Start Time"] = $timeslot
$newItem["End Time"] = ($timeslot).AddMinutes($a)
$newItem["Status"] = "Free"

 
#Update the object so it gets saved to the list 
$newItem.Update() 

write-host "Entry added for $timeslot"

}

}

}


I’ve been helping a customer build a booking system for one of their processes using Service Manager, Orchestrator and Sharepoint, which is why this script was born.

No comments:

Related Posts with Thumbnails