65.9K
CodeProject is changing. Read more.
Home

Powershell to create calendar in SharePoint with categorized overlays

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.20/5 (2 votes)

Apr 7, 2014

CPOL
viewsIcon

12500

Creating calendars in SharePoint 2010, changing default categories and then creating view and overlay for each category can take a lot of clicks.

Creating calendars in SharePoint 2010, changing default categories and then creating view and overlay for each category can take a lot of clicks. To save time and effort I created a powershell script which can be used as follows

& ‘.\Calendar Overlay.ps1′ -webUrl {SITEURL} -listName “{LISTNAME}” -categories “{CommaSeparatedCategories}”

It takes three command line parameters which are

-webUrl – The site url where you want to create the calendar

-listName – The name of calendar list

-categories – Comma separated values for categories

Please click here to download the script

For example if you would like to create a calendar named Yearly events on http://myspsite/marketing with road show, marketing boot camp 1 and digital campaign 1. You can pass in the following arguments

& ‘.\Calendar Overlay.ps1′ -webUrl http://myspsite/marketing -listName “Yearly Events” -categories “Road show, Marketing Boot camp 1, Digital Campaign 1″

The resulting calendar would look like

Calendar overlays created with Powershell

Source for the script is as follows:

###############################################################################
# Creating Calendar overlays in SharePoint 2010 using Powershell
# Version          : 1.0
# Url              : http://khurrampunjwani.wordpress.com
# Creator          : Khurram Punjwani, http://twitter.com/khurrampunjwani
###############################################################################

param (
    [string]$webUrl = $(throw "-webUrl is required."),
	[string]$listName = $(throw "-listName is required."),
    [string]$categories = $(throw "-categories is required.")
 )

[array]$categoriesArray = $categories -split ","

Function Get-SPCalendarWeb(){

	#Clear screen first
	Clear-Host
	
	#Load web where Calendar is supposed to be created
	$web = Get-SPWeb -Identity $webUrl
	Write-Host "SP Web Loaded " $web.Title
	
	#Create new calendar
	New-SPCalendar

	$web.Dispose()
}

Function Add-TestEntries($cal){
	$listItem = $cal.AddItem()
	$listItem["Title"] = "Test 1"
	$listItem["Category"] = $categoriesArray[0]
	$listItem["EventDate"] = [System.DateTime]::Now
	$listItem["EndDate"] = [System.DateTime]::Now.AddHours(4)
	$listItem.Update()
	
	$listItem = $cal.AddItem()
	$listItem["Title"] = "Test 2"
	$listItem["Category"] = $categoriesArray[1]
	$listItem["EventDate"] = [System.DateTime]::Now
	$listItem["EndDate"] = [System.DateTime]::Now.AddHours(4)
	$listItem.Update()
}

Function New-SPCalendar(){
	$web.Lists.Add($listName,"",[Microsoft.SharePoint.SPListTemplateType]::Events)
	$cal = $web.Lists.TryGetList($listName);
	
	Write-Host "Calendar created"
	
	#Delete default categories and add the ones specified in command line
	Add-CalendarCategories $cal
	
	#Add Views with Filters
	Add-SPCalendarViews $cal

	#Add test entries
	Add-TestEntries $cal
}

Function Add-SPCalendarViews($cal){
	$view = $cal.Views["Calendar"]
	$viewFields = $view.ViewFields.ToStringCollection()
	$viewData = "<FieldRef Name='Title' Type='CalendarMonthTitle'/>"
    $viewData += "<FieldRef Name='Title' Type='CalendarWeekTitle'/>"
    $viewData += "<FieldRef Name='Location' Type='CalendarWeekLocation'/>"
    $viewData += "<FieldRef Name='Title' Type='CalendarDayTitle'/>"
    $viewData += "<FieldRef Name='Location' Type='CalendarDayLocation'/>"
	#XML for calendar overlays
	$calendarSettings = " <AggregationCalendars> "
	#To show different color for category views
	$colorIndex = 1 
	
	#Create views for categories so there will be one view for each category
	foreach($category in $categoriesArray){
		#Create filter for the view to show events from the respective category
		$viewQuery = "<Where>"
		$viewQuery += "<And>"	
		$viewQuery += "<DateRangesOverlap>"
		$viewQuery += "<FieldRef Name='EventDate' />"
		$viewQuery += "<FieldRef Name='EndDate' />"
		$viewQuery += "<FieldRef Name='RecurrenceID' />"
		$viewQuery += "<Value Type='DateTime'><Month /></Value>"
		$viewQuery += "</DateRangesOverlap>"
		$viewQuery += "<Eq>"
		$viewQuery += "<FieldRef Name='Category'/>"
		$viewQuery += %{"<Value Type='Text'>{0}</Value>" -f $category}
		$viewQuery += "</Eq>"
		$viewQuery += "</And>"
		$viewQuery += "</Where>"
		
		$newView = $cal.Views.Add($category,$viewFields,$viewQuery,3,$false,
			$false,"CALENDAR",$false)
		$newView.ViewData = $viewData
		$newView.Update()

		%{"View created for {0} category" -f $category} | Write-Host
		
		#Update XML for Calendar Overlay, each view would be added as an overlay
		$calendarSettings += %{" <AggregationCalendar Id='{0}'" -f [Guid]::NewGuid()}
		$calendarSettings += " Type='SharePoint' "
		$calendarSettings += %{" Name='{0}'" -f $category }
		$calendarSettings += " Description=''"
		$calendarSettings += %{" Color='{0}' " -f $colorIndex}
		$calendarSettings += " AlwaysShow='True' "
		$calendarSettings += %{" CalendarUrl='{0}.aspx'> " -f $category}
		$calendarSettings += %{" <Settings WebUrl='{0}' " -f $webUrl}
		$calendarSettings += %{" ListId='{0}'" -f $cal.ID}
		$calendarSettings += %{" ViewId='{0}'" -f $newView.ID}
		$calendarSettings += " ListFormUrl='DispForm.aspx' /> "
		$calendarSettings += " </AggregationCalendar> "
		
		#Change color for the next category
		$colorIndex += 1 
	}
	
	$calendarSettings += " </AggregationCalendars> "		
	Write-Verbose "Calendar Settings $calendarSettings"
	
	#Change the default view to show events which have no category associated
	#Otherwise it would show duplicates because of overlays
	$viewQuery = "<Where>"
	$viewQuery += "<IsNull>"
	$viewQuery += "<FieldRef Name='Category'/>"
	$viewQuery += "</IsNull>"
	$viewQuery += "</Where>"
	
	$view.CalendarSettings = $calendarSettings
	$view.Query = $viewQuery
	$view.Update()
		
	Write-Host "Calendar overlays created"	
	$cal.Update()
}

Function Add-CalendarCategories($cal){

	#Remove default out of the box categories
	$categoryField = $cal.Fields["Category"]
	$categoryField.Choices.Clear()
	
	#Add categories specified in command line
	$categoryField.Choices.AddRange($categoriesArray);
	$categoryField.Update();
	
	Write-Host "Category choices updated with" $categoryField.Choices
}

Get-SPCalendarWeb