1

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 i try this :-

Connect-PnPOnline -Url *** -AppId *** -AppSecret ***>
Remove-PnPEventReceiver -Identity "ProjectRER"

but i got the same exception. here is the code which have attached the RER inside our provider hosted app:-

public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties)
        {
            SPRemoteEventResult result = new SPRemoteEventResult();
            if (properties.EventType == SPRemoteEventType.AppInstalled)
            {
                using (ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties, false))
                {
                    if (clientContext != null)
                    {
                        //Get reference to the host web list with name Feedback
                        var documentsList = clientContext.Web.Lists.GetByTitle("Project Update System");
                        clientContext.Load(documentsList);
                        clientContext.ExecuteQuery();
                        string remoteUrl = "****.svc";
                                                  //Create the remote event receiver definition
                        EventReceiverDefinitionCreationInformation newEventReceiver = new EventReceiverDefinitionCreationInformation()
                        {
                            EventType = EventReceiverType.ItemUpdated,
                            ReceiverAssembly = Assembly.GetExecutingAssembly().FullName,
                            ReceiverName = "***",
                            ReceiverClass = "***",
                            ReceiverUrl = remoteUrl,
                            SequenceNumber = 15002
                        };
                        //Add the remote event receiver to the host web list
                        documentsList.EventReceivers.Add(newEventReceiver);
                        EventReceiverDefinitionCreationInformation newEventReceiver2 = new EventReceiverDefinitionCreationInformation()
                        {
                            EventType = EventReceiverType.ItemUpdating,
                            ReceiverAssembly = Assembly.GetExecutingAssembly().FullName,
                            ReceiverName = "***2",
                            ReceiverClass = "**2",
                            ReceiverUrl = remoteUrl,
                            SequenceNumber = 15001
                        };
                        //Add the remote event receiver to the host web list
                        documentsList.EventReceivers.Add(newEventReceiver2);
                        clientContext.ExecuteQuery();
                    }
                }
            } 
1
  • Hi John, Were you able to solve this issue? Commented Jan 12, 2021 at 16:34

1 Answer 1

0

Could you please try the below code,

# replace these details (also consider using Get-Credential to enter password securely as script runs).. 
$username = "[email protected]" 
$password = "SomePassword" 
$url = "https://SomeSite.sharepoint.com"

$securePassword = ConvertTo-SecureString $Password -AsPlainText -Force 

# the path here may need to change if you used e.g. C:\Lib.. 
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" 
# note that you might need some other references (depending on what your script does) for example:
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll" 

# connect/authenticate to SharePoint Online and get ClientContext object.. 
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url) 
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword) 
$clientContext.Credentials = $credentials 

if (!$clientContext.ServerObjectIsNull.Value) 
{ 
    Write-Host "Connected to SharePoint Online site: '$Url'" -ForegroundColor Green 
} 

function deleteListItemRemoteEventReceiver([Microsoft.SharePoint.Client.ClientContext]$context, [string]$listName, [string]$receiverName)
{
    Write-Host "Attempting to delete RER with name '$receiverName' on list '$listName'"

    $list = $context.Web.Lists.GetByTitle($listName)
    $context.Load($list)
    $eventReceivers = $list.EventReceivers
    $context.Load($eventReceivers)
    $context.ExecuteQuery()

    $specifiedEr = $null
    try
    {
        foreach ($er in $eventReceivers)
        {
            if ($er.ReceiverName -eq $receiverName)
            {
                $specifiedEr = $er
            }
        }

        if ($specifiedEr -ne $null -and !$specifiedEr.ServerObjectIsNull.Value)
        {
            $specifiedEr.DeleteObject()
            $context.ExecuteQuery()
            Write-Host "Receiver deleted successfully"
        }
        else
        {
            Write-Error "Failed to find receiver with name '$receiverName'"
        }
    }
    catch
    {
        Write-Error "Failed to delete receiver - error:  $_"
    }
}

# now call function..
deleteListItemRemoteEventReceiver $clientContext $listName $receiverName

for more information, refer the below link,

https://www.sharepointnutsandbolts.com/2014/01/add-delete-and-list-remote-event-receivers.html

1
  • when i try running this script i got this exception "Access denied. You do not have permission to perform this action or access this resource."
    – John John
    Commented Feb 26, 2020 at 1:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.