locked
How to delete a calendar appointment RRS feed

  • Question

  • I try to delete appointment from public folder calendar with ews.

    Param ([string]$calInputFile = "c:\powershell\rapril.rpt" )
    # Load EWS Managed API library
    Import-Module -Name "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"
    # Load all calendar Entries
    $calEntries = Import-Csv $calInputFile
    $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService -ArgumentList ([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::$ExchangeVersion) 
    $service.Url = new-object System.Uri("https://degd-ms03.driestar.nl/ews/exchange.asmx")
    $service.Credentials = new-object Microsoft.Exchange.WebServices.Data.WebCredentials("centric1","centric.","driestar")
    
    function FolderIdFromPath{
    	param (
    	        $FolderPath = "$( throw 'Folder Path is a mandatory Parameter' )"
    
    		  )
    	process{
    		## Find and Bind to Folder based on Path  
    		#Define the path to search should be seperated with \  
    		#Bind to the MSGFolder Root  
    		$folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::PublicFoldersRoot)   
    		$tfTargetFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)  
    		#Split the Search path into an array  
    		$fldArray = $FolderPath.Split("\") 
    		 #Loop through the Split Array and do a Search for each level of folder 
    		for ($lint = 1; $lint -lt $fldArray.Length; $lint++) { 
    	        #Perform search based on the displayname of each folder level 
    	        $fvFolderView = new-object Microsoft.Exchange.WebServices.Data.FolderView(1) 
    	        $SfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName,$fldArray[$lint]) 
    	        $findFolderResults = $service.FindFolders($tfTargetFolder.Id,$SfSearchFilter,$fvFolderView) 
    	        if ($findFolderResults.TotalCount -gt 0){ 
    	            foreach($folder in $findFolderResults.Folders){ 
    	                $tfTargetFolder = $folder                
    	            } 
    	        } 
    	        else{ 
    	            "Error Folder Not Found"  
    	            $tfTargetFolder = $null  
    	            break  
    	        }     
    	    }  
    		if($tfTargetFolder -ne $null){
    			return $tfTargetFolder.Id.UniqueId.ToString()
    		}
    	}
    }
    $fldId = FolderIdFromPath -FolderPath "\algemene_agenda"
    
    if($fldId -ne "Error Folder Not Found"){
    	$SubFolderId =  new-object Microsoft.Exchange.WebServices.Data.FolderId($fldId)
    	$service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress,"PF-mailbox@driestar-educatief.nl") 
    	$SubFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$SubFolderId)
    }
    
    foreach ($calendaritem in $calEntries )
    	{
    	$cvCalendarview = new-object Microsoft.Exchange.WebServices.Data.CalendarView([System.DateTime]("04/01/2014"),([System.DateTime]("04/01/2014")))
        $cvCalendarview.PropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
        $frCalendarResult = $SubFolder.FindAppointments($cvCalendarview)
    
    	$entryFound = $False
    	foreach ($appointment in $frCalendarResult.Items)
          	
    		 {
                if($appointment.Subject -eq $calendaritem.onderwerp)
    			{
    				$appointment.delete($SubFolder,[Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete)
    				$entryFound = $True
             	}
    		 }
             if($entryFound){$entryFound =$false}
             else # Create the appointment object and save it to the users calendar
             {
    		 	if ($appointment.Subject = $calendaritem.onderwerp)
                {
    			$appointment.Delete($SubFolder,[Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete)
    			}
             }
    }
    
    

    Can someone help me????




    Thursday, April 24, 2014 7:37 AM

All replies

  • If you want to delete an appointment then the Syntax you would use would be

    $Appointment.Delete(([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete,[Microsoft.Exchange.WebServices.Data.SendCancellationsMode]::SendToNone);

    Your code has other problems eg what are you trying to do here ??

    appt=$CalendarFolder.FindAppointments($appointment.Id,$cvCalendarview)

    This line wouldn't work as FindAppoitnment takes a FolderId why are you passing in an ItemId ?

    Your then calling Delete on the Collection

    $appt.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete)

    Which won't work either you need to call Delete on the Appointment Item you want to delete.

    Cheers
    Glen

    Friday, April 25, 2014 5:45 AM
  • Thanks Glen for your answer.

    I try to delete a calendaritem in the public mailbox

    the code is:

    Param ([string]$calInputFile = "c:\powershell\rapril.rpt" )
    # Load EWS Managed API library
    Import-Module -Name "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"
    # Load all calendar Entries
    $calEntries = Import-Csv $calInputFile
    $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService -ArgumentList ([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::$ExchangeVersion)
    $service.Url = new-object System.Uri("https://*****.driestar.nl/ews/exchange.asmx")
    $service.Credentials = new-object Microsoft.Exchange.WebServices.Data.WebCredentials(*****)
    function FolderIdFromPath{
           param (
                  $FolderPath = "$( throw 'Folder Path is a mandatory Parameter' )"
                   )
           process{
                 ## Find and Bind to Folder based on Path 
                 #Define the path to search should be seperated with \ 
                 #Bind to the MSGFolder Root 
                 $folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::PublicFoldersRoot)  
                 $tfTargetFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid) 
                 #Split the Search path into an array 
                 $fldArray = $FolderPath.Split("\")
                  #Loop through the Split Array and do a Search for each level of folder
                 for ($lint = 1; $lint -lt $fldArray.Length; $lint++) {
                   #Perform search based on the displayname of each folder level
                   $fvFolderView = new-object Microsoft.Exchange.WebServices.Data.FolderView(1)
                   $SfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName,$fldArray[$lint])
                   $findFolderResults = $service.FindFolders($tfTargetFolder.Id,$SfSearchFilter,$fvFolderView)
                   if ($findFolderResults.TotalCount -gt 0){
                       foreach($folder in $findFolderResults.Folders){
                           $tfTargetFolder = $folder               
                       }
                   }
                  else{
                       "Error Folder Not Found" 
                       $tfTargetFolder = $null 
                       break 
                   }    
               } 
                 if($tfTargetFolder -ne $null){
                        return $tfTargetFolder.Id.UniqueId.ToString()
                 }
           }
    }
    $fldId = FolderIdFromPath -FolderPath "\algemene_agenda"
    if($fldId -ne "Error Folder Not Found"){
           $SubFolderId =  new-object Microsoft.Exchange.WebServices.Data.FolderId($fldId)
           $SubFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$SubFolderId)
    }
           #check of item exist
           foreach ($calendaritem in $calEntries )
           {
           $service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress,"PF-mailbox@driestar-educatief.nl")
           $cvCalendarview = new-object Microsoft.Exchange.WebServices.Data.CalendarView([System.DateTime]("04/01/2014"),([System.DateTime]("04/30/2014")))
        $cvCalendarview.PropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
        $frCalendarResult = $Subfolder.FindAppointments($cvCalendarview)
           $entryFound = $False
             foreach ($appointment in $frCalendarResult.Items)
                  {
                if($appointment.Subject -eq $calendaritem.onderwerp)
                        {
                               $appointment.Delete(([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete,([Microsoft.Exchange.WebServices.Data.SendCancellationsMode]::SendToNone)))
                               $entryFound = $True
                 }
                  }
             if($entryFound){$entryFound =$false}
             else # Create the appointment object and save it to the users calendar
             {
                        if ($appointment.Subject = $calendaritem.onderwerp)
                {
                        $appointment.Delete(([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete,([Microsoft.Exchange.WebServices.Data.SendCancellationsMode]::SendToNone)))
                        }
             }
            }
    Now I get an error:
    Cannot find an overload for "Delete" and the argument count: "2".
    At C:\powershell\add to algemene Agenda eruit.ps1:64 char:5
    +                 $appointment.Delete($SubFolder.Item,([Microsoft.Exchange.WebServices.Data.De
    ...
    +    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodException
        + FullyQualifiedErrorId : MethodCountCouldNotFindBest
    Cannot find an overload for "Delete" and the argument count: "2".
    At C:\powershell\add to algemene Agenda eruit.ps1:64 char:5
    +                 $appointment.Delete($SubFolder.Item,([Microsoft.Exchange.WebServices.Data.De
    ...
    +    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodException
        + FullyQualifiedErrorId : MethodCountCouldNotFindBest
    Cannot find an overload for "Delete" and the argument count: "2".
    At C:\powershell\add to algemene Agenda eruit.ps1:64 char:5
    +                 $appointment.Delete($SubFolder.Item,([Microsoft.Exchange.WebServices.Data.De
    ...
    +    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodException
        + FullyQualifiedErrorId : MethodCountCouldNotFindBest

    Anyone an idea what is wrong???





    Friday, April 25, 2014 11:25 AM
  • Your code still doesn't make sense to me. If your just trying to delete Items from a Calendar Public folder then why do you have multiple FindAppointments ?? You just need one for the Folder your working with to get a collection of Items in that Folder. also

    $appointment.Delete($SubFolder.Item,([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete,([Microsoft.Exchange.WebServices.Data.SendCancellationsMode]::SendToNone)))

    This method will never be valid why have got $SubFolder.Item in there it has no meaning.

    Delete is a valid method for a MailboxItem/PublicFolderItem but just that Item your bond to in this instance $Appontment.

    Your Flow should be

    BindTo or Get the Folder Id for the Folder you want to work with

    Use FindAppointment on the folders to get a Collection of Appointments

    Enumerate that Collection of Items and decide what to do with the Items

    If you want to delete an Item Call the delete method on that Item.

    Cheers
    Glen

    Monday, April 28, 2014 6:55 AM
  •  Hallo Glen,

    I have almost the same syntax used for delete appointments in the calendar of users.

    Param([string]$calInputFile = "c:\powershell\agenda.txt" )
    $calMailbox=Get-Content "c:\powershell\gebruikers.txt"
    
    # Load EWS Managed API library
    Import-Module -Name "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"
    # Load all Entries
    $calagenda = Import-Csv $calInputFile
    #Setup the service for connection
    $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService -ArgumentList ([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::$ExchangeVersion) 
    $service.Url = new-object System.Uri("https://*******/ews/exchange.asmx")
    $service.Credentials = new-object Microsoft.Exchange.WebServices.Data.WebCredentials("******)
    # Identify the folder to save our appointments into
    $folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Calendar)
    
    #Perform for each mailbox
    foreach ($mailbox in $calMailbox)
    {
    	$exchangeUsers = Get-recipient  $mailbox  | Select-Object PrimarySmtpAddress
    	# Identify user to which appointment will be added
    	$MailboxName = $exchangeUsers.PrimarySmtpAddress
    	# Instruct service to use impersonation
    	$iUserID = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress,$MailboxName)
    	$service.ImpersonatedUserId = $iUserID
    	$CalendarFolder =[Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid)
    	#check of item exist
    	foreach ($calendaritem in $calagenda )
    	{
    	$cvCalendarview = new-object Microsoft.Exchange.WebServices.Data.CalendarView([System.DateTime]("04/01/2014"),([System.DateTime]("04/30/2014")))
        $cvCalendarview.PropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
        $frCalendarResult = $CalendarFolder.FindAppointments($cvCalendarview)
    	$entryFound = $False
             foreach ($appointment in $frCalendarResult.Items)
          	
    		 {
                if($appointment.Subject -eq $calendaritem.onderwerp)
    			{
    				$appointment.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete)
    				$entryFound = $True
             	}
    		 }
             if($entryFound){$entryFound =$false}
             else # Create the appointment object and save it to the users calendar
             {
    		 	if ($appointment.Subject = $calendaritem.onderwerp)
                {
    			$appointment.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete)
    			}
             }
    	 }

    Can you help me to resolve the problem with the public folder???

    I do not know how to do this. Can you give me some code?

    Cheers,

    Aart



    Wednesday, April 30, 2014 7:48 AM
  • Hallo Glen,

    I have resolved the problem.

    by:

    $appointment.Delete(([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete),([Microsoft.Exchange.WebServices.Data.SendCancellationsMode]::SendToNone))

    That was the solution

    Wednesday, April 30, 2014 12:09 PM