I have been given a task to make a script in CSOM. I am very new to Powershell so any help would be appreciated.
I have a site collection with multiple calendars within the sites. I have to create a script that will look for new events in multiple calendars and copy the events to a single target calendar. I can copy from a source calendar to a target calendar but cannot seem to get multiple calendars sources.
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"
Function Copy-ListItems()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $SourceListName,
[Parameter(Mandatory=$true)] [string] $TargetListName
)
Try {
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred
#Get the Source List and Target Lists
$SourceList = $Ctx.Web.Lists.GetByTitle($SourceListName)
$TargetList = $Ctx.Web.Lists.GetByTitle($TargetListName)
#Get All Items from Source List
$SourceListItems = $SourceList.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$Ctx.Load($SourceListItems)
$Ctx.ExecuteQuery()
#Get each column value from source list and add them to target
ForEach($SourceItem in $SourceListItems)
{
$NewItem =New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$ListItem = $TargetList.AddItem($NewItem)
#Map each field from source list to target list - INTERNAL NAMES
$ListItem["Title"] = $SourceItem["Title"]
$ListItem["Location"] = $SourceItem["Location"]
$ListItem["EventDate"] = $SourceItem["EventDate"]
$ListItem["EndDate"] = $SourceItem["EndDate"]
$ListItem["Description"] = $SourceItem["Description"]
$ListItem["Category"] = $SourceItem["Category"]
$ListItem["RecurrenceData"] = $SourceItem["RecurrenceData"]
$ListItem["fRecurrence"] = $SourceItem["fRecurrence"]
$ListItem["TimeZone"] = $SourceItem["TimeZone"]
$ListItem["XMLTZone"] = $SourceItem["XMLTZone"]
$ListItem["UID"] = $SourceItem["UID"]
$ListItem["Duration"] = $SourceItem["Duration"]
$ListItem["EventType"] = $SourceItem["EventType"]
$ListItem.update()
}
$Ctx.ExecuteQuery()
write-host -f Green "Total List Items Copied from '$SourceListName' to '$TargetListName' : $($SourceListItems.count)"
}
Catch {
write-host -f Red "Error Copying List Items!" $_.Exception.Message
}
}
#Set Parameters
$SiteURL= "https://site.sharepoint.com/teams/site/"
$SourceListName="Test Calendar"
$TargetListName="Destination Calendar"
#Call the function to copy list items
Copy-ListItems -siteURL $SiteURL -SourceListName $SourceListName -TargetListName $TargetListName
I have to copy from multiple sources and skip if duplicate.
Thank you in advance.