1

How to deploy a remote event receiver without handling the app installed, I know that this can be done with powershell but I already tried it, the RER is reflecting in the list, but the program itself is not working, should I edit something in my solution for this to work?

$receiverName = "ItemDeletingRER"
$receiverClassName = "ItemDeletingRER"
$receiverUrl = "https://pbpotest.azurewebsites.net/Services/ItemDeletingRER.svc"
$receiverAssemblyName = "ResourceListRERWeb, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"

$newRER = New-Object Microsoft.SharePoint.Client.EventReceiverDefinitionCreationInformation
$newRER.EventType = [Microsoft.SharePoint.Client.EventReceiverType]::ItemDeleting
$newRER.ReceiverName = $receiverName
$newRER.ReceiverClass = $receiverClassName
$newRER.ReceiverAssembly = $receiverAssemblyName
$newRER.ReceiverUrl = $receiverUrl
$newRER.SequenceNumber = 15001
$newRER.Synchronization = [Microsoft.SharePoint.Client.EventReceiverSynchronization]::Synchronous

what my event receiver do is that it prevents an item from being deleted if the value of a column is "yes", thats the part that is not working.

Here is a snippet of my event receiver:

public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties)
{
    using (ClientContext clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(properties))
    {
        SPRemoteEventResult result = new SPRemoteEventResult();

        if (properties.EventType == SPRemoteEventType.ItemDeleting)
        {
            List list = clientContext.Web.Lists.GetByTitle(properties.ItemEventProperties.ListTitle);
            ListItem fieldItem = list.GetItemById(properties.ItemEventProperties.ListItemId);

            clientContext.Load(fieldItem);
            clientContext.ExecuteQuery();

            if ((bool)fieldItem["Promoted_x0020_To_x0020_Resource_x003f_"])
            {
                result.Status = SPRemoteEventServiceStatus.CancelWithError;
                result.ErrorMessage = "Unable to delete document as it is currently used as a Resource. Please de-promote prior to deleting.";
            }
            else
            {
                result.Status = SPRemoteEventServiceStatus.Continue;
            }
        }

        return result;
    }
}

$username = "[email protected]"
$password = Read-Host -Prompt "Enter password" -AsSecureString 

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
#$context = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL2)

$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password) 
$ctx.Credentials = $credentials
8
  • Could you share a PowerShell code? Could you also please elaborate on what does the program itself is not working mean? Sep 18, 2018 at 10:59
  • Hi I updated my question, also I am not sure if the value of my receiver assemly is correct, I just copied it from another document library where the event receiver is working Sep 18, 2018 at 11:20
  • Could you also share the code from receiver ? Sep 18, 2018 at 11:21
  • There I updated my question Sep 18, 2018 at 11:24
  • 1
    not sure if this is what youre looking for Sep 18, 2018 at 11:27

1 Answer 1

2

Thank for providing the full picture.

You definitely can create an event receiver outside AppInstalled, however will your RER code work or not will depend heavily on the way you added those event receivers and your actual RER code.

In your code you use TokenHelper.CreateRemoteEventReceiverClientContext(properties). This code expects, that properties contains something called context object (properties.ContextToken). If you have created your RER with PowerShell with your user credentials, ContextToken will be always null. In other words, your context knows nothing about your add-in (client id and client secret) That's why it doesn't work in your case.

To make it work, you should add RER using ClientContext created with help of your add-in credentials (ClientId, ClientSecret). I haven't found corresponding PS code, you can use .net console appliaction.
You should create new console project in VS, add PnP Core nuget package.

Then your code will be:

AuthenticationManager authManager = new AuthenticationManager();  
ClientContext context = authManager.GetAppOnlyAuthenticatedContext(siteUrl, clientId, clientSecret);


EventReceiverDefinitionCreationInformation eventReceiver = new EventReceiverDefinitionCreationInformation();
eventReceiver.EventType = EventReceiverType.ItemAdding;
eventReceiver.ReceiverAssembly = Assembly.GetExecutingAssembly().FullName;
eventReceiver.ReceiverClass = "HTools_EventReceiver";
eventReceiver.ReceiverName = "HTools_EventReceiver";
eventReceiver.ReceiverUrl = "http://hh-wks-robb:2145/HTools_EventReceiver.svc";
eventReceiver.SequenceNumber = 1000;

List targetList = context.web.Lists.GetByTitle("your list");
targetList.EventReceivers.Add(eventReceiver);

context.ExecuteQuery();

You should use the same ClientId and Secret you used when creating your app.

4
  • so this means that the object creating the context is wrong? ClientContext clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(properties) Sep 19, 2018 at 5:33
  • Most likely it throws an exceptions, that's why it doesn't work Sep 19, 2018 at 5:34
  • I see, Ill try to attach the event receiver using pnp also with powershell Sep 19, 2018 at 8:02
  • Tried this method still the RER wont fire Sep 19, 2018 at 9:43

Your Answer

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

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