0

I have a remote event receiver which is attached to a list. so i want to remove it, but when i try this PnP script using the Office 365 admin:-

Remove-PnPEventReceiver -List "Our Project"  -Identity "ProjectRER"

I got the following exception:-

Remove-PnPEventReceiver : Access denied. You do not have permission to perform this action or access this resource.

also based on this link https://techcommunity.microsoft.com/t5/sharepoint-developer/problem-in-deleting-remote-event-receiver-in-sharepoint/m-p/31583 i tried the following powershel:-

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="Our Project"
$EventReceiverGUID="8ded46d9-6b87-4dac-9532-64d013122e92"

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

but i got this error also:-

Exception calling "ExecuteQuery" with "0" argument(s): "Access denied. You do not have permission to perform this
action or access this resource."
At line:42 char:1
+ $ctx.ExecuteQuery()
+ ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ServerUnauthorizedAccessException

So can anyone advice on this please?

  • Remember, a user that has the SharePoint Admin role does not automatically have any permissions to any of the sites. You may need to explicitly make that account a site collection admin for the site you are trying to manage RER's. – willman Feb 12 at 11:39
  • @willman the office 365 admin is explicitly define as the site collection admin – SharePoint TestDev Feb 12 at 13:14
  • Of the S046WW16776 site? – willman Feb 12 at 15:12
  • Instead of setting credentials on a new context, try executing Connect-SPOService with the credentials parameter, the call $ctx = get-spocontext to get the authenticated context. – willman Feb 12 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.