Problem in Deleting Remote Event Receiver in Sharepoint

Copper Contributor

Hi ,

 

I have attached Remote event Receiver to a document library using Provider hosted app.Now i am trying to delete (Provider hosted app) the same remote event Receiver  but i am getting "Microsoft.SharePoint.Client.ServerUnauthorizedAccessException: Access denied. You do not have permission to perform this action or access this resource." error.

Is there any other way apart from running powershell script to delete the attached remote event Receiver

8 Replies

You need to remove the remote event reciever using the same ClientID that created it. So if your provider hosted app with a specific clientid was used to create the event reciever you will get access denied if you try to delete it with powershell using your credentials. However i do believe that the pnp powershell Connect-SPOnline has some parameters to connect with clientid and secret. You should be able to delete it then

I do

You can do this first by searching it in the SharePoint ClientBrowser and then use a powershell script to delete it.

function Get-SPOListEventreceivers
{
param (
  [Parameter(Mandatory=$true,Position=1)]
		[string]$Username,
		[Parameter(Mandatory=$true,Position=2)]
		$AdminPassword,
        [Parameter(Mandatory=$true,Position=3)]
		[string]$Url,
        [Parameter(Mandatory=$true,Position=4)]
		[string]$ListTitle,
        [Parameter(Mandatory=$true,Position=5)]
		[GUID]$EventReceiverGUID
)

  $ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
  $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $AdminPassword)

try
{
$ctx.ExecuteQuery()
} catch [Net.WebException] 
        {
            
            Write-Host $Url " failed to connect to the site" $_.Exception.Message.ToString() -ForegroundColor Red
}

 $ctx.Load($ctx.Site)

  $lista=$ctx.Web.Lists.GetByTitle($ListTitle)
 $ctx.Load($lista)
  $ctx.ExecuteQuery()
 $recevery=$lista.EventReceivers
 $ctx.Load($recevery)
 $ctx.ExecuteQuery()
 Write-Host "Found " $recevery.Count " receivers in " $lista.Title

$recevery.GetById($EventReceiverGUID).DeleteObject()

try
{
$ctx.ExecuteQuery()
Write-Host "receiver removed"
}

 catch [Net.WebException] 
  {
  Write-Host "Failed to delete the receiver" $_.Exception.Message.ToString() -ForegroundColor Red
  }

}


# Paths to SDK. Please verify location on your computer.
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll" 
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" 

# Insert the credentials and the name of the admin site
$Username="spadmin@xxx.onmicrosoft.com" 
$password = "$xxxx" 
$url = "https://xxxx.sharepoint.com/sites/S046WW16776"
$AdminPassword=Read-Host -Prompt "Password" -AsSecureString
$Url="https://xxx.sharepoint.com/sites/S046WW16776"
$ListTitle="Project Metadata"
$EventReceiverGUID="8ded46d9-6b87-4dac-9532-64d013122e92"

Get-SPOListEventreceivers -Username $Username -AdminPassword $AdminPassword -Url $Url -ListTitle $ListTitle -EventReceiverGUID $EventreceiverGUID

yeah i think now powershell is the only option .

Because the issue was not with the appid change but the farm id change because of which the entire AppId was not matching and hence giving access denied error.

 

Thanks for replies :)

Powershell method gives access denied error. Is there any way we can deleted the event receiver without deleting the list

@Prerak DesaiI am facing the exact same issue.. unable to remove the RER using PnP either

@john john 

 

Hi john may I know if you are using sharepoint online version or on premise version??

 

 

As  RER can be removed by the app that registered it. In our scenario farm realm id got changed because of which even the same app was not able to delete the remote event receiver.

 

In order to get rid of the remote event receiver we used brute force . Instead of client side code we wrote server side PowerShell script to delete the  remote event receiver which worked for us.

 

 

 

@bhartisemwal1990 Thanks, We were facing the same issue. Your suggestion helped

Unfortunately, at the moment it is impossible to remove the remote event receiver that are added. An error «Access Denied» occurs. The only way is to delete the list. Receivers, written by published service (in Azure) can be removed properly