1

I am following the steps mentioned in this link to create a Remote Event Receiver inside our SharePoint online site collection @ https://www.enjoysharepoint.com/create-remote-event-receiver-sharepoint-online/

and inside the AppEventReceiver.svc i define to attach the app on ItemUpdated and ItemUpdating as follow:-

public class AppEventReceiver : IRemoteEventService
    {
        /// <summary>
        /// Handles app events that occur after the app is installed or upgraded, or when app is being uninstalled.
        /// </summary>
        /// <param name=”properties”>Holds information about the app event.</param>
        /// <returns>Holds information returned from the app event.</returns>

        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 = "https://tpusrer.azurewebsites.net/Services/TPUSRemoteEventReceiver.svc";
                                                  //Create the remote event receiver definition
                        EventReceiverDefinitionCreationInformation newEventReceiver = new EventReceiverDefinitionCreationInformation()
                        {
                            EventType = EventReceiverType.ItemUpdated,
                            ReceiverAssembly = Assembly.GetExecutingAssembly().FullName,
                            ReceiverName = "TPUSRemoteEventReceiver",
                            ReceiverClass = "TPUSRemoteEventReceiver",
                            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 = "TPUSRemoteEventReceiver2",
                            ReceiverClass = "TPUSRemoteEventReceiver2",
                            ReceiverUrl = remoteUrl,
                            SequenceNumber = 15001
                        };
                        //Add the remote event receiver to the host web list
                        documentsList.EventReceivers.Add(newEventReceiver2);
                        clientContext.ExecuteQuery();
                    }
                }
            }
            else if (properties.EventType == SPRemoteEventType.AppUninstalling)
            {
                using (ClientContext clientContext = TokenHelper.CreateAppEventClientContext(properties, false))
                {
                    var list = clientContext.Web.Lists.GetByTitle("Project Update System");
                    clientContext.Load(list);
                    clientContext.ExecuteQuery();
                    EventReceiverDefinitionCollection erdc = list.EventReceivers;
                    clientContext.Load(erdc);
                    clientContext.ExecuteQuery();
                    List<EventReceiverDefinition> toDelete = new List<EventReceiverDefinition>();
                    foreach (EventReceiverDefinition erd in erdc)
                    {
                        if (erd.ReceiverName == "TPUSRemoteEventReceiver")
{
                            toDelete.Add(erd);
                        }
                    }

                    //Delete the remote event receiver from the list, when the app gets uninstalled

                    foreach (EventReceiverDefinition item in toDelete)
                    {
                        item.DeleteObject();
                        clientContext.ExecuteQuery();
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// This method is a required placeholder, but is not used by app events.
        /// </summary>
        /// <param name=”properties”>Unused.</param>

        public void ProcessOneWayEvent(SPRemoteEventProperties properties)
        {
            throw new NotImplementedException();
        }
    }

and when i check the remote event receivers attached to the list using this PnP script,i got the 2 RERs,:-

Get-PnPEventReceiver -List "Project Update System"

But when i update a list item, the ItemUpdated RER will work well, but the ItemUpdating will not have any effect. here is the code for the ItemUpdating RER:-

public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties)
{
    int listItemID = properties.ItemEventProperties.ListItemId;
    string siteUrl = properties.ItemEventProperties.WebUrl;

    SPRemoteEventResult result = new SPRemoteEventResult();
    string errormessage = "";

    using (ClientContext context = TokenHelper.CreateRemoteEventReceiverClientContext(properties))
    {
        CamlQuery camlQuery = new CamlQuery();
        camlQuery.ViewXml = string.Format("<View Scope=\"RecursiveAll\"><Query><Where><Eq><FieldRef Name='ID' /><Value Type='Number'>{0}</Value></Eq></Where></Query></View>", listItemID);
        ListItemCollection collListItem = context.Web.Lists.GetByTitle("Project Update System").GetItems(camlQuery);
        context.Load(collListItem);
        context.Load(collListItem,
              items => items.Include(
                  item => item.Id,
                  item => item["ProjectDescription"],
                  item => item["ProjectOMSNumber"],
                  item => item["ProjectDateSinged"],
                  item => item["ProjectManHoursEstimated"],
                  item => item["ProjectStage"],
                  item => item["ProjectApproveDelay"],
                  item => item["ProjectClosureSummary"],
                  item => item["ProjectHasBeenDelayed"],
                  item => item["ProjectDateSinged"],

                  item => item["ProjectHasBeenDelayed"],
                  item => item["ProjectStatusFlag"]
                  ));
        context.ExecuteQuery();
        context.Load(collListItem);

        foreach (ListItem i in collListItem)
        {
            //This for all the orders regardless if there are created from OMS or Manully7
            FieldUrlValue currenturl = i["ProjectLinkToOMS"] != null ? (FieldUrlValue)i["ProjectLinkToOMS"] : null;
            //var fff = properties.ItemEventProperties.AfterProperties["ProjectLinkToOMS"];
            if (i["ProjectOMSID"] != null && properties.ItemEventProperties.AfterProperties["ProjectOMSID"] != null && (i["ProjectOMSID"].ToString() != properties.ItemEventProperties.AfterProperties["ProjectOMSID"].ToString()))
            //||
            //(i["ProjectLinkToOMS"] != null && properties.ItemEventProperties.AfterProperties["ProjectLinkToOMS"] != null && (!properties.ItemEventProperties.AfterProperties["ProjectLinkToOMS"].ToString().Contains(currenturl.Url+","+currenturl.Url))))
            {

                errormessage = errormessage + "Unaithorized access";
            }


            if (i["ProjectOMSID"] != null && !string.IsNullOrEmpty(i["ProjectOMSID"].ToString()))//item created from OMS!
            {
                TaxonomyFieldValue currentcustomer = (TaxonomyFieldValue)i["ProjectCustomer"];

                if (
                   properties.ItemEventProperties.AfterProperties.ContainsKey("ProjectDescription") && i["ProjectDescription"] != null && properties.ItemEventProperties.AfterProperties["ProjectDescription"] != null &&
                    (
                    Regex.Replace(Regex.Replace(i["ProjectDescription"].ToString(), "<.*?>", String.Empty), @"\p{C}+", String.Empty)
                    != Regex.Replace(Regex.Replace(properties.ItemEventProperties.AfterProperties["ProjectDescription"].ToString(), "<.*?>", String.Empty), @"\p{C}+", String.Empty)
                    )
                    )
                { errormessage = errormessage + "You are not authorized to update Description."; }

            }

            var vv = properties.ItemEventProperties.AfterProperties["ProjectCustomer"];
            if (properties.ItemEventProperties.AfterProperties["ProjectStage"] != null && properties.ItemEventProperties.AfterProperties["ProjectStage"].ToString() == "Closed" && (properties.ItemEventProperties.AfterProperties["ProjectManHoursUsed"] == null || properties.ItemEventProperties.AfterProperties["ProjectManHoursUsed"].ToString() == string.Empty))
            {
                errormessage = errormessage + "You must enter Man Hour used before closing the project";
            }

            if (!string.IsNullOrEmpty(errormessage))
            {


                result.Status = SPRemoteEventServiceStatus.CancelWithError;
                result.ErrorMessage = errormessage;
            }

        }
    }

    return result;}

So can anyone advice why the ItemUpdating RER is not firing? although seems it is registered and attached to the list.

2
  • can you post the results you get when running Get-PnPEventReceiver?
    – willman
    Feb 17, 2020 at 22:21
  • Have you tried just wrapping the entire using statement in a try..catch to see if some error is prohibiting the handler from responding?
    – willman
    Feb 17, 2020 at 22:26

1 Answer 1

0

Just for troubleshooting, I would start by adding a try..catch and using that to cancel the event if something goes wrong. Let us know what result you get for trying something like this:

public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties)
{
    int listItemID = properties.ItemEventProperties.ListItemId;
    string siteUrl = properties.ItemEventProperties.WebUrl;

    SPRemoteEventResult result = new SPRemoteEventResult();
    string errormessage = "";

    try
    {
        using (ClientContext context = TokenHelper.CreateRemoteEventReceiverClientContext(properties))
        {
            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml = string.Format("<View Scope=\"RecursiveAll\"><Query><Where><Eq><FieldRef Name='ID' /><Value Type='Number'>{0}</Value></Eq></Where></Query></View>", listItemID);
            ListItemCollection collListItem = context.Web.Lists.GetByTitle("Project Update System").GetItems(camlQuery);
            context.Load(collListItem);
            context.Load(collListItem,
                  items => items.Include(
                      item => item.Id,
                      item => item["ProjectDescription"],
                      item => item["ProjectOMSNumber"],
                      item => item["ProjectDateSinged"],
                      item => item["ProjectManHoursEstimated"],
                      item => item["ProjectStage"],
                      item => item["ProjectApproveDelay"],
                      item => item["ProjectClosureSummary"],
                      item => item["ProjectHasBeenDelayed"],
                      item => item["ProjectDateSinged"],

                      item => item["ProjectHasBeenDelayed"],
                      item => item["ProjectStatusFlag"]
                      ));
            context.ExecuteQuery();
            context.Load(collListItem);

            foreach (ListItem i in collListItem)
            {
                //This for all the orders regardless if there are created from OMS or Manully7
                FieldUrlValue currenturl = i["ProjectLinkToOMS"] != null ? (FieldUrlValue)i["ProjectLinkToOMS"] : null;
                //var fff = properties.ItemEventProperties.AfterProperties["ProjectLinkToOMS"];
                if (i["ProjectOMSID"] != null && properties.ItemEventProperties.AfterProperties["ProjectOMSID"] != null && (i["ProjectOMSID"].ToString() != properties.ItemEventProperties.AfterProperties["ProjectOMSID"].ToString()))
                //||
                //(i["ProjectLinkToOMS"] != null && properties.ItemEventProperties.AfterProperties["ProjectLinkToOMS"] != null && (!properties.ItemEventProperties.AfterProperties["ProjectLinkToOMS"].ToString().Contains(currenturl.Url+","+currenturl.Url))))
                {

                    errormessage = errormessage + "Unaithorized access";
                }


                if (i["ProjectOMSID"] != null && !string.IsNullOrEmpty(i["ProjectOMSID"].ToString()))//item created from OMS!
                {
                    TaxonomyFieldValue currentcustomer = (TaxonomyFieldValue)i["ProjectCustomer"];

                    if (
                       properties.ItemEventProperties.AfterProperties.ContainsKey("ProjectDescription") && i["ProjectDescription"] != null && properties.ItemEventProperties.AfterProperties["ProjectDescription"] != null &&
                        (
                        Regex.Replace(Regex.Replace(i["ProjectDescription"].ToString(), "<.*?>", String.Empty), @"\p{C}+", String.Empty)
                        != Regex.Replace(Regex.Replace(properties.ItemEventProperties.AfterProperties["ProjectDescription"].ToString(), "<.*?>", String.Empty), @"\p{C}+", String.Empty)
                        )
                        )
                    { errormessage = errormessage + "You are not authorized to update Description."; }

                }

                var vv = properties.ItemEventProperties.AfterProperties["ProjectCustomer"];
                if (properties.ItemEventProperties.AfterProperties["ProjectStage"] != null && properties.ItemEventProperties.AfterProperties["ProjectStage"].ToString() == "Closed" && (properties.ItemEventProperties.AfterProperties["ProjectManHoursUsed"] == null || properties.ItemEventProperties.AfterProperties["ProjectManHoursUsed"].ToString() == string.Empty))
                {
                    errormessage = errormessage + "You must enter Man Hour used before closing the project";
                }

                if (!string.IsNullOrEmpty(errormessage))
                {


                    result.Status = SPRemoteEventServiceStatus.CancelWithError;
                    result.ErrorMessage = errormessage;
                }

            }
        }
    }
    catch (Exception ex)
    {
        result.Status = SPRemoteEventServiceStatus.CancelWithError;
        result.ErrorMessage = ex.ToString();
    }

    return result;
}

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.